2013-02-25 01:35:08 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-02-24 22:54:09 +08:00
|
|
|
import string
|
2014-01-26 04:54:25 +08:00
|
|
|
import warnings
|
2013-02-24 22:54:09 +08:00
|
|
|
|
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
|
2013-02-24 02:28:17 +08:00
|
|
|
from django.db.models.signals import pre_save, pre_delete
|
2014-02-27 05:48:20 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango19Warning
|
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
|
|
|
|
2014-01-26 04:54:25 +08:00
|
|
|
from .requests import RequestSite as RealRequestSite
|
|
|
|
from .shortcuts import get_current_site as real_get_current_site
|
|
|
|
|
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
|
|
|
|
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):
|
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()
|
|
|
|
if host not in SITE_CACHE:
|
|
|
|
site = self.get(domain__iexact=host)
|
|
|
|
SITE_CACHE[host] = site
|
|
|
|
return SITE_CACHE[host]
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2013-02-24 22:54:09 +08:00
|
|
|
domain = models.CharField(_('domain name'), max_length=100,
|
|
|
|
validators=[_simple_domain_name_validator])
|
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
|
|
|
|
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-01-26 04:54:25 +08:00
|
|
|
class RequestSite(RealRequestSite):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
warnings.warn(
|
|
|
|
"Please import RequestSite from django.contrib.sites.requests.",
|
2014-02-27 05:48:20 +08:00
|
|
|
RemovedInDjango19Warning, stacklevel=2)
|
2014-01-26 04:54:25 +08:00
|
|
|
super(RequestSite, self).__init__(*args, **kwargs)
|
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
|
|
|
|
|
|
|
|
|
|
|
def get_current_site(request):
|
2014-01-26 04:54:25 +08:00
|
|
|
warnings.warn(
|
|
|
|
"Please import get_current_site from django.contrib.sites.shortcuts.",
|
2014-02-27 05:48:20 +08:00
|
|
|
RemovedInDjango19Warning, stacklevel=2)
|
2014-01-26 04:54:25 +08:00
|
|
|
return real_get_current_site(request)
|
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']
|
|
|
|
try:
|
|
|
|
del SITE_CACHE[instance.pk]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-10-01 01:15:59 +08:00
|
|
|
try:
|
|
|
|
del SITE_CACHE[Site.objects.get(pk=instance.pk).domain]
|
|
|
|
except (KeyError, Site.DoesNotExist):
|
|
|
|
pass
|
2013-02-24 02:28:17 +08:00
|
|
|
pre_save.connect(clear_site_cache, sender=Site)
|
|
|
|
pre_delete.connect(clear_site_cache, sender=Site)
|