Ticket #3766: site_cache.diff
File site_cache.diff, 1.5 KB (added by , 17 years ago) |
---|
-
django/trunk/django/contrib/sites/models.py
1 1 from django.db import models 2 2 from django.utils.translation import ugettext_lazy as _ 3 3 4 SITE_CACHE = {} 5 4 6 class SiteManager(models.Manager): 5 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 """ 6 13 from django.conf import settings 7 14 try: 8 15 sid = settings.SITE_ID 9 16 except AttributeError: 10 17 from django.core.exceptions import ImproperlyConfigured 11 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.") 12 return self.get(pk=sid) 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 13 25 26 def clear_cache(self): 27 """Clears the ``Site`` object cache.""" 28 global SITE_CACHE 29 SITE_CACHE = {} 30 14 31 class Site(models.Model): 15 32 domain = models.CharField(_('domain name'), max_length=100) 16 33 name = models.CharField(_('display name'), max_length=50)