Added RequestSite class to sites framework

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5653 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-07-12 05:28:04 +00:00
parent 06fc225ae6
commit dcd5750d7a
2 changed files with 35 additions and 0 deletions

View File

@ -26,3 +26,20 @@ class Site(models.Model):
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 = request.META['SERVER_NAME']
def save(self):
raise NotImplementedError('RequestSite cannot be saved.')
def delete(self):
raise NotImplementedError('RequestSite cannot be deleted.')

View File

@ -320,3 +320,21 @@ Here's how Django uses the sites framework:
.. _flatpages framework: ../flatpages/
.. _syndication framework: ../syndication/
.. _authentication framework: ../authentication/
``RequestSite`` objects
=======================
**New in Django development version**
Some ``django.contrib`` applications take advantage of the sites framework but
are architected in a way that doesn't *require* the sites framework to be
installed in your database. (Some people don't want to, or just aren't *able*
to install the extra database table that the sites framework requires.) For
those cases, the framework provides a ``RequestSite`` class, which can be used
as a fallback when the database-backed sites framework is not available.
A ``RequestSite`` object has a similar interface to a normal ``Site`` object,
except its ``__init__()`` method takes an ``HttpRequest`` object. It's able to
deduce the ``domain`` and ``name`` by looking at the request's domain. It has
``save()`` and ``delete()`` methods to match the interface of ``Site``, but
the methods raise ``NotImplementedError``.