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 _
2006-05-02 09:31:56 +08:00
2007-09-14 16:37:09 +08:00
SITE_CACHE = { }
2006-05-02 09:31:56 +08:00
class SiteManager ( models . Manager ) :
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
class Site ( models . Model ) :
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 ' , )
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
def __unicode__ ( self ) :
2006-05-02 09:31:56 +08:00
return self . domain
2008-07-19 07:54:34 +08:00
2008-06-22 14:50:25 +08:00
def delete ( self ) :
pk = self . pk
super ( Site , self ) . delete ( )
try :
del ( SITE_CACHE [ pk ] )
except KeyError :
pass
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
2007-09-13 22:40:08 +08:00
def __unicode__ ( self ) :
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. ' )