app/django/contrib/sites/models.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sun, 08 Feb 2009 23:55:44 +0000
changeset 1255 9fe8c6c54933
parent 323 ff1a9aa48cfd
permissions -rw-r--r--
Redone the listing in group_app.py. There is now a list_self that only shows the group application of the current user in the given a scope. The redirects point to edit or applicant when necessary. Review_overview will show all the group applications that can be reviewed in the given scope, the redirect points to review for all statusses. And list will just show all the group applications and redirect you to edit. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed

from django.db import models
from django.utils.translation import ugettext_lazy as _

SITE_CACHE = {}

class SiteManager(models.Manager):
    def get_current(self):
        """
        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.
        """
        from django.conf import settings
        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.")
        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 = {}

class Site(models.Model):
    domain = models.CharField(_('domain name'), max_length=100)
    name = models.CharField(_('display name'), max_length=50)
    objects = SiteManager()

    class Meta:
        db_table = 'django_site'
        verbose_name = _('site')
        verbose_name_plural = _('sites')
        ordering = ('domain',)

    def __unicode__(self):
        return self.domain
    
    def delete(self):
        pk = self.pk
        super(Site, self).delete()
        try:
            del(SITE_CACHE[pk])
        except KeyError:
            pass

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):
        self.domain = self.name = request.get_host()

    def __unicode__(self):
        return self.domain

    def save(self, force_insert=False, force_update=False):
        raise NotImplementedError('RequestSite cannot be saved.')

    def delete(self):
        raise NotImplementedError('RequestSite cannot be deleted.')