OLD | NEW |
(Empty) | |
| 1 from __future__ import unicode_literals |
| 2 |
| 3 import string |
| 4 |
| 5 from django.core.exceptions import ImproperlyConfigured, ValidationError |
| 6 from django.db import models |
| 7 from django.db.models.signals import pre_delete, pre_save |
| 8 from django.http.request import split_domain_port |
| 9 from django.utils.encoding import python_2_unicode_compatible |
| 10 from django.utils.translation import ugettext_lazy as _ |
| 11 |
| 12 SITE_CACHE = {} |
| 13 |
| 14 |
| 15 def _simple_domain_name_validator(value): |
| 16 """ |
| 17 Validates that the given value contains no whitespaces to prevent common |
| 18 typos. |
| 19 """ |
| 20 if not value: |
| 21 return |
| 22 checks = ((s in value) for s in string.whitespace) |
| 23 if any(checks): |
| 24 raise ValidationError( |
| 25 _("The domain name cannot contain any spaces or tabs."), |
| 26 code='invalid', |
| 27 ) |
| 28 |
| 29 |
| 30 class SiteManager(models.Manager): |
| 31 use_in_migrations = True |
| 32 |
| 33 def _get_site_by_id(self, site_id): |
| 34 if site_id not in SITE_CACHE: |
| 35 site = self.get(pk=site_id) |
| 36 SITE_CACHE[site_id] = site |
| 37 return SITE_CACHE[site_id] |
| 38 |
| 39 def _get_site_by_request(self, request): |
| 40 host = request.get_host() |
| 41 try: |
| 42 # First attempt to look up the site by host with or without port. |
| 43 if host not in SITE_CACHE: |
| 44 SITE_CACHE[host] = self.get(domain__iexact=host) |
| 45 return SITE_CACHE[host] |
| 46 except Site.DoesNotExist: |
| 47 # Fallback to looking up site after stripping port from the host. |
| 48 domain, port = split_domain_port(host) |
| 49 if domain not in SITE_CACHE: |
| 50 SITE_CACHE[domain] = self.get(domain__iexact=domain) |
| 51 return SITE_CACHE[domain] |
| 52 |
| 53 def get_current(self, request=None): |
| 54 """ |
| 55 Returns the current Site based on the SITE_ID in the project's settings. |
| 56 If SITE_ID isn't defined, it returns the site with domain matching |
| 57 request.get_host(). The ``Site`` object is cached the first time it's |
| 58 retrieved from the database. |
| 59 """ |
| 60 from django.conf import settings |
| 61 if getattr(settings, 'SITE_ID', ''): |
| 62 site_id = settings.SITE_ID |
| 63 return self._get_site_by_id(site_id) |
| 64 elif request: |
| 65 return self._get_site_by_request(request) |
| 66 |
| 67 raise ImproperlyConfigured( |
| 68 "You're using the Django \"sites framework\" without having " |
| 69 "set the SITE_ID setting. Create a site in your database and " |
| 70 "set the SITE_ID setting or pass a request to " |
| 71 "Site.objects.get_current() to fix this error." |
| 72 ) |
| 73 |
| 74 def clear_cache(self): |
| 75 """Clears the ``Site`` object cache.""" |
| 76 global SITE_CACHE |
| 77 SITE_CACHE = {} |
| 78 |
| 79 def get_by_natural_key(self, domain): |
| 80 return self.get(domain=domain) |
| 81 |
| 82 |
| 83 @python_2_unicode_compatible |
| 84 class Site(models.Model): |
| 85 |
| 86 domain = models.CharField( |
| 87 _('domain name'), |
| 88 max_length=100, |
| 89 validators=[_simple_domain_name_validator], |
| 90 unique=True, |
| 91 ) |
| 92 name = models.CharField(_('display name'), max_length=50) |
| 93 objects = SiteManager() |
| 94 |
| 95 class Meta: |
| 96 db_table = 'django_site' |
| 97 verbose_name = _('site') |
| 98 verbose_name_plural = _('sites') |
| 99 ordering = ('domain',) |
| 100 |
| 101 def __str__(self): |
| 102 return self.domain |
| 103 |
| 104 def natural_key(self): |
| 105 return (self.domain,) |
| 106 |
| 107 |
| 108 def clear_site_cache(sender, **kwargs): |
| 109 """ |
| 110 Clears the cache (if primed) each time a site is saved or deleted |
| 111 """ |
| 112 instance = kwargs['instance'] |
| 113 using = kwargs['using'] |
| 114 try: |
| 115 del SITE_CACHE[instance.pk] |
| 116 except KeyError: |
| 117 pass |
| 118 try: |
| 119 del SITE_CACHE[Site.objects.using(using).get(pk=instance.pk).domain] |
| 120 except (KeyError, Site.DoesNotExist): |
| 121 pass |
| 122 |
| 123 |
| 124 pre_save.connect(clear_site_cache, sender=Site) |
| 125 pre_delete.connect(clear_site_cache, sender=Site) |
OLD | NEW |