Add Django-1.2.1
[frozenviper.git] / Django-1.2.1 / build / lib.linux-i686-2.6 / django / contrib / sites / models.py
blob9b1697e251ab52a27421cb04b9c8b01b42c1c3fe
1 from django.db import models
2 from django.utils.translation import ugettext_lazy as _
4 SITE_CACHE = {}
6 class SiteManager(models.Manager):
7 def get_current(self):
8 """
9 Returns the current ``Site`` based on the SITE_ID in the
10 project's settings. The ``Site`` object is cached the first
11 time it's retrieved from the database.
12 """
13 from django.conf import settings
14 try:
15 sid = settings.SITE_ID
16 except AttributeError:
17 from django.core.exceptions import ImproperlyConfigured
18 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.")
19 try:
20 current_site = SITE_CACHE[sid]
21 except KeyError:
22 current_site = self.get(pk=sid)
23 SITE_CACHE[sid] = current_site
24 return current_site
26 def clear_cache(self):
27 """Clears the ``Site`` object cache."""
28 global SITE_CACHE
29 SITE_CACHE = {}
31 class Site(models.Model):
32 domain = models.CharField(_('domain name'), max_length=100)
33 name = models.CharField(_('display name'), max_length=50)
34 objects = SiteManager()
36 class Meta:
37 db_table = 'django_site'
38 verbose_name = _('site')
39 verbose_name_plural = _('sites')
40 ordering = ('domain',)
42 def __unicode__(self):
43 return self.domain
45 def save(self, *args, **kwargs):
46 super(Site, self).save(*args, **kwargs)
47 # Cached information will likely be incorrect now.
48 if self.id in SITE_CACHE:
49 del SITE_CACHE[self.id]
51 def delete(self):
52 pk = self.pk
53 super(Site, self).delete()
54 try:
55 del SITE_CACHE[pk]
56 except KeyError:
57 pass
59 class RequestSite(object):
60 """
61 A class that shares the primary interface of Site (i.e., it has
62 ``domain`` and ``name`` attributes) but gets its data from a Django
63 HttpRequest object rather than from a database.
65 The save() and delete() methods raise NotImplementedError.
66 """
67 def __init__(self, request):
68 self.domain = self.name = request.get_host()
70 def __unicode__(self):
71 return self.domain
73 def save(self, force_insert=False, force_update=False):
74 raise NotImplementedError('RequestSite cannot be saved.')
76 def delete(self):
77 raise NotImplementedError('RequestSite cannot be deleted.')