django1/django/contrib/sites/models.py

50 lines
1.7 KiB
Python
Raw Normal View History

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.http import get_host
class SiteManager(models.Manager):
def get_current(self):
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.")
return self.get(pk=sid)
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',)
class Admin:
list_display = ('domain', 'name')
search_fields = ('domain', 'name')
def __unicode__(self):
return self.domain
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 = get_host(request)
def __unicode__(self):
return self.domain
def save(self):
raise NotImplementedError('RequestSite cannot be saved.')
def delete(self):
raise NotImplementedError('RequestSite cannot be deleted.')