From dcd5750d7ac6692428e431010772f1941406d576 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 12 Jul 2007 05:28:04 +0000 Subject: [PATCH] Added RequestSite class to sites framework git-svn-id: http://code.djangoproject.com/svn/django/trunk@5653 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/sites/models.py | 17 +++++++++++++++++ docs/sites.txt | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py index 1b71601899..9dbe1f45f8 100644 --- a/django/contrib/sites/models.py +++ b/django/contrib/sites/models.py @@ -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.') diff --git a/docs/sites.txt b/docs/sites.txt index 12259b04c3..e9982f745a 100644 --- a/docs/sites.txt +++ b/docs/sites.txt @@ -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``.