Created
September 20, 2013 11:59
-
-
Save jakul/6636439 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @python_2_unicode_compatible | |
| class RequestSite(object): | |
| """ | |
| A class that shares the primary interface of Site (i.e., it has | |
| ``domain`` and ``name`` attributes) but gets its data from a Django | |
| HttpRequest object rather than from a database. | |
| The save() and delete() methods raise NotImplementedError. | |
| """ | |
| def __init__(self, request): | |
| self.domain = self.name = request.get_host() | |
| def __str__(self): | |
| return self.domain | |
| def save(self, force_insert=False, force_update=False): | |
| raise NotImplementedError('RequestSite cannot be saved.') | |
| def delete(self): | |
| raise NotImplementedError('RequestSite cannot be deleted.') | |
| def get_current_site(request): | |
| """ | |
| Checks if contrib.sites is installed and returns either the current | |
| ``Site`` object or a ``RequestSite`` object based on the request. | |
| """ | |
| if Site._meta.installed: | |
| current_site = Site.objects.get_current() | |
| else: | |
| current_site = RequestSite(request) | |
| return current_site |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment