~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/contrib/sites/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from django.db import models
2
 
from django.utils.translation import gettext_lazy as _
 
2
from django.utils.translation import ugettext_lazy as _
 
3
 
 
4
SITE_CACHE = {}
3
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
 
        return self.get(pk=settings.SITE_ID)
 
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
 
25
 
 
26
    def clear_cache(self):
 
27
        """Clears the ``Site`` object cache."""
 
28
        global SITE_CACHE
 
29
        SITE_CACHE = {}
8
30
 
9
31
class Site(models.Model):
10
 
    domain = models.CharField(_('domain name'), maxlength=100)
11
 
    name = models.CharField(_('display name'), maxlength=50)
 
32
    domain = models.CharField(_('domain name'), max_length=100)
 
33
    name = models.CharField(_('display name'), max_length=50)
12
34
    objects = SiteManager()
 
35
 
13
36
    class Meta:
14
37
        db_table = 'django_site'
15
38
        verbose_name = _('site')
16
39
        verbose_name_plural = _('sites')
17
40
        ordering = ('domain',)
18
 
    class Admin:
19
 
        list_display = ('domain', 'name')
20
 
        search_fields = ('domain', 'name')
21
 
 
22
 
    def __str__(self):
23
 
        return self.domain
 
41
 
 
42
    def __unicode__(self):
 
43
        return self.domain
 
44
    
 
45
    def delete(self):
 
46
        pk = self.pk
 
47
        super(Site, self).delete()
 
48
        try:
 
49
            del(SITE_CACHE[pk])
 
50
        except KeyError:
 
51
            pass
 
52
 
 
53
class RequestSite(object):
 
54
    """
 
55
    A class that shares the primary interface of Site (i.e., it has
 
56
    ``domain`` and ``name`` attributes) but gets its data from a Django
 
57
    HttpRequest object rather than from a database.
 
58
 
 
59
    The save() and delete() methods raise NotImplementedError.
 
60
    """
 
61
    def __init__(self, request):
 
62
        self.domain = self.name = request.get_host()
 
63
 
 
64
    def __unicode__(self):
 
65
        return self.domain
 
66
 
 
67
    def save(self, force_insert=False, force_update=False):
 
68
        raise NotImplementedError('RequestSite cannot be saved.')
 
69
 
 
70
    def delete(self):
 
71
        raise NotImplementedError('RequestSite cannot be deleted.')