2013-02-25 01:35:08 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-02-24 22:54:09 +08:00
|
|
|
import string
|
|
|
|
|
2013-11-16 18:13:04 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured, ValidationError
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db import models
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models.signals import pre_delete, pre_save
|
2015-05-04 06:08:28 +08:00
|
|
|
from django.http.request import split_domain_port
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2014-02-27 05:48:20 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-09-14 16:37:09 +08:00
|
|
|
SITE_CACHE = {}
|
|
|
|
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
|
2013-02-24 22:54:09 +08:00
|
|
|
def _simple_domain_name_validator(value):
|
|
|
|
"""
|
|
|
|
Validates that the given value contains no whitespaces to prevent common
|
|
|
|
typos.
|
|
|
|
"""
|
|
|
|
if not value:
|
|
|
|
return
|
|
|
|
checks = ((s in value) for s in string.whitespace)
|
|
|
|
if any(checks):
|
|
|
|
raise ValidationError(
|
2013-06-06 02:55:05 +08:00
|
|
|
_("The domain name cannot contain any spaces or tabs."),
|
|
|
|
code='invalid',
|
|
|
|
)
|
2013-02-24 22:54:09 +08:00
|
|
|
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class SiteManager(models.Manager):
|
2014-12-13 06:19:58 +08:00
|
|
|
use_in_migrations = True
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
|
2014-10-01 01:15:59 +08:00
|
|
|
def _get_site_by_id(self, site_id):
|
|
|
|
if site_id not in SITE_CACHE:
|
|
|
|
site = self.get(pk=site_id)
|
|
|
|
SITE_CACHE[site_id] = site
|
|
|
|
return SITE_CACHE[site_id]
|
|
|
|
|
|
|
|
def _get_site_by_request(self, request):
|
|
|
|
host = request.get_host()
|
2015-05-04 06:08:28 +08:00
|
|
|
try:
|
|
|
|
# First attempt to look up the site by host with or without port.
|
|
|
|
if host not in SITE_CACHE:
|
|
|
|
SITE_CACHE[host] = self.get(domain__iexact=host)
|
|
|
|
return SITE_CACHE[host]
|
|
|
|
except Site.DoesNotExist:
|
|
|
|
# Fallback to looking up site after stripping port from the host.
|
|
|
|
domain, port = split_domain_port(host)
|
|
|
|
if domain not in SITE_CACHE:
|
|
|
|
SITE_CACHE[domain] = self.get(domain__iexact=domain)
|
|
|
|
return SITE_CACHE[domain]
|
2014-10-01 01:15:59 +08:00
|
|
|
|
|
|
|
def get_current(self, request=None):
|
2007-09-14 16:37:09 +08:00
|
|
|
"""
|
2014-10-01 01:15:59 +08:00
|
|
|
Returns the current Site based on the SITE_ID in the project's settings.
|
|
|
|
If SITE_ID isn't defined, it returns the site with domain matching
|
|
|
|
request.get_host(). The ``Site`` object is cached the first time it's
|
|
|
|
retrieved from the database.
|
2007-09-14 16:37:09 +08:00
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
2014-10-01 01:15:59 +08:00
|
|
|
if getattr(settings, 'SITE_ID', ''):
|
|
|
|
site_id = settings.SITE_ID
|
|
|
|
return self._get_site_by_id(site_id)
|
|
|
|
elif request:
|
|
|
|
return self._get_site_by_request(request)
|
|
|
|
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"You're using the Django \"sites framework\" without having "
|
|
|
|
"set the SITE_ID setting. Create a site in your database and "
|
|
|
|
"set the SITE_ID setting or pass a request to "
|
|
|
|
"Site.objects.get_current() to fix this error."
|
|
|
|
)
|
2007-09-14 16:37:09 +08:00
|
|
|
|
|
|
|
def clear_cache(self):
|
|
|
|
"""Clears the ``Site`` object cache."""
|
|
|
|
global SITE_CACHE
|
|
|
|
SITE_CACHE = {}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2015-11-07 06:55:44 +08:00
|
|
|
def get_by_natural_key(self, domain):
|
|
|
|
return self.get(domain=domain)
|
|
|
|
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2006-05-02 09:31:56 +08:00
|
|
|
class Site(models.Model):
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
|
2016-03-29 06:33:29 +08:00
|
|
|
domain = models.CharField(
|
|
|
|
_('domain name'),
|
|
|
|
max_length=100,
|
|
|
|
validators=[_simple_domain_name_validator],
|
|
|
|
unique=True,
|
|
|
|
)
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(_('display name'), max_length=50)
|
2006-05-02 09:31:56 +08:00
|
|
|
objects = SiteManager()
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
|
|
|
db_table = 'django_site'
|
|
|
|
verbose_name = _('site')
|
|
|
|
verbose_name_plural = _('sites')
|
|
|
|
ordering = ('domain',)
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.domain
|
2009-02-27 12:57:33 +08:00
|
|
|
|
2015-11-07 06:55:44 +08:00
|
|
|
def natural_key(self):
|
|
|
|
return (self.domain,)
|
|
|
|
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
|
2013-02-24 02:28:17 +08:00
|
|
|
def clear_site_cache(sender, **kwargs):
|
|
|
|
"""
|
|
|
|
Clears the cache (if primed) each time a site is saved or deleted
|
|
|
|
"""
|
|
|
|
instance = kwargs['instance']
|
2015-02-13 02:58:37 +08:00
|
|
|
using = kwargs['using']
|
2013-02-24 02:28:17 +08:00
|
|
|
try:
|
|
|
|
del SITE_CACHE[instance.pk]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-10-01 01:15:59 +08:00
|
|
|
try:
|
2015-02-13 02:58:37 +08:00
|
|
|
del SITE_CACHE[Site.objects.using(using).get(pk=instance.pk).domain]
|
2014-10-01 01:15:59 +08:00
|
|
|
except (KeyError, Site.DoesNotExist):
|
|
|
|
pass
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2013-02-24 02:28:17 +08:00
|
|
|
pre_save.connect(clear_site_cache, sender=Site)
|
|
|
|
pre_delete.connect(clear_site_cache, sender=Site)
|