2006-05-02 09:31:56 +08:00
from django . db import models
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
from django . utils . translation import ugettext_lazy as _
2012-08-12 18:32:08 +08:00
from django . utils . encoding import python_2_unicode_compatible
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
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
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
2006-05-02 09:31:56 +08:00
def get_current ( self ) :
2007-09-14 16:37:09 +08:00
"""
Returns the current ` ` Site ` ` based on the SITE_ID in the
project ' s settings. The ``Site`` object is cached the first
time it ' s retrieved from the database.
"""
2006-05-02 09:31:56 +08:00
from django . conf import settings
2007-07-12 13:23:47 +08:00
try :
sid = settings . SITE_ID
except AttributeError :
from django . core . exceptions import ImproperlyConfigured
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 to fix this error. " )
2007-09-14 16:37:09 +08:00
try :
current_site = SITE_CACHE [ sid ]
except KeyError :
current_site = self . get ( pk = sid )
SITE_CACHE [ sid ] = current_site
return current_site
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
2007-08-05 13:14:46 +08:00
domain = models . CharField ( _ ( ' domain name ' ) , max_length = 100 )
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
def save ( self , * args , * * kwargs ) :
super ( Site , self ) . save ( * args , * * kwargs )
# Cached information will likely be incorrect now.
if self . id in SITE_CACHE :
del SITE_CACHE [ self . id ]
2008-06-22 14:50:25 +08:00
def delete ( self ) :
pk = self . pk
super ( Site , self ) . delete ( )
try :
2009-02-27 12:57:33 +08:00
del SITE_CACHE [ pk ]
2008-06-22 14:50:25 +08:00
except KeyError :
pass
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
2007-07-12 13:28:04 +08:00
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 ) :
2007-09-16 01:46:03 +08:00
self . domain = self . name = request . get_host ( )
2007-07-12 13:28:04 +08:00
2012-08-12 18:32:08 +08:00
def __str__ ( self ) :
2007-09-13 22:40:08 +08:00
return self . domain
2008-08-29 03:28:17 +08:00
def save ( self , force_insert = False , force_update = False ) :
2007-07-12 13:28:04 +08:00
raise NotImplementedError ( ' RequestSite cannot be saved. ' )
def delete ( self ) :
raise NotImplementedError ( ' RequestSite cannot be deleted. ' )
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 ) :
"""
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