Ticket #17005: 17005.diff

File 17005.diff, 3.7 KB (added by Christopher Medrela, 13 years ago)
  • new file django/contrib/sites/middleware.py

    diff --git a/django/contrib/sites/middleware.py b/django/contrib/sites/middleware.py
    new file mode 100644
    index 0000000..74ce561
    - +  
     1from django.conf import settings
     2from django.contrib.sites.models import Site
     3
     4
     5class SiteMiddleware(object):
     6    """
     7    Middleware that sets `site` attribute to request object.
     8    """
     9
     10    def process_request(self, request):
     11        request.site = Site.objects.get_current()
  • django/contrib/sites/tests.py

    diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py
    index 17ab1f2..3a80ffc 100644
    a b  
    11from django.conf import settings
    22from django.contrib.sites.models import Site, RequestSite, get_current_site
     3from django.contrib.sites.middleware import SiteMiddleware
    34from django.core.exceptions import ObjectDoesNotExist
    45from django.http import HttpRequest
    56from django.test import TestCase
    class SitesFrameworkTests(TestCase):  
    5455        site = get_current_site(request)
    5556        self.assertTrue(isinstance(site, RequestSite))
    5657        self.assertEqual(site.name, u"example.com")
     58
     59class MiddlewareTest(TestCase):
     60
     61    def test_request(self):
     62        """
     63        Makes sure that the request has correct `site` attribute.
     64        """
     65
     66        middleware = SiteMiddleware()
     67        request = HttpRequest()
     68        middleware.process_request(request)
     69        self.assertEqual(request.site.id, settings.SITE_ID)
  • docs/ref/contrib/sites.txt

    diff --git a/docs/ref/contrib/sites.txt b/docs/ref/contrib/sites.txt
    index 8fc434b..7a9f340 100644
    a b fallback for cases where it is not installed.  
    174174.. function:: get_current_site(request)
    175175
    176176    Checks if contrib.sites is installed and returns either the current
    177     :class:`~django.contrib.sites.models.Site` object or a 
     177    :class:`~django.contrib.sites.models.Site` object or a
    178178    :class:`~django.contrib.sites.models.RequestSite` object based on
    179179    the request.
    180180
    fallback when the database-backed sites framework is not available.  
    437437
    438438        Sets the ``name`` and ``domain`` attributes to the value of
    439439        :meth:`~django.http.HttpRequest.get_host`.
    440        
     440
    441441
    442442A :class:`~django.contrib.sites.models.RequestSite` object has a similar
    443443interface to a normal :class:`~django.contrib.sites.models.Site` object, except
    its :meth:`~django.contrib.sites.models.RequestSite.__init__()` method takes an  
    447447and ``delete()`` methods to match the interface of
    448448:class:`~django.contrib.sites.models.Site`, but the methods raise
    449449``NotImplementedError``.
     450
     451``SiteMiddleware``
     452==================
     453
     454If you often use this pattern::
     455
     456    from django.contrib.sites.models import Site
     457
     458    def my_view(request):
     459        site = Site.objects.get_current()
     460        ...
     461
     462there is simple way to avoid repetitions. Add
     463:mod:`django.contrib.site.middleware.SiteMiddleware`
     464to :setting:`MIDDLEWARE_CLASSES`. The middleware sets ``site`` attribute
     465to every incoming request object.
  • docs/ref/middleware.txt

    diff --git a/docs/ref/middleware.txt b/docs/ref/middleware.txt
    index 737e0b2..570a33a 100644
    a b Message middleware  
    151151Enables cookie- and session-based message support. See the
    152152:doc:`messages documentation </ref/contrib/messages>`.
    153153
     154Site middleware
     155----------------
     156
     157.. module:: django.contrib.site.middleware
     158  :synopsis: Site middleware.
     159
     160.. class:: SiteMiddleware
     161
     162.. versionadded:: 1.4
     163
     164Adds the ``site`` attribute, representing the current site, to every incoming
     165``HttpRequest`` object. See the :doc:`sites documentation </ref/contrib/sites>`.
     166
    154167Session middleware
    155168------------------
    156169
Back to Top