1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #17005 -- Added CurrentSiteMiddleware to set the current site on each request.

Thanks jordan at aace.org for the suggestion.
This commit is contained in:
Christopher Medrela
2013-11-18 21:16:09 +01:00
committed by Tim Graham
parent c43c469a2e
commit b22d6c47a7
5 changed files with 60 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
from .models import Site
class CurrentSiteMiddleware(object):
"""
Middleware that sets `site` attribute to request object.
"""
def process_request(self, request):
request.site = Site.objects.get_current()

View File

@@ -5,6 +5,7 @@ from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.http import HttpRequest
from django.test import TestCase, modify_settings, override_settings
from .middleware import CurrentSiteMiddleware
from .models import Site
from .requests import RequestSite
from .shortcuts import get_current_site
@@ -79,3 +80,13 @@ class SitesFrameworkTests(TestCase):
self.assertRaises(ValidationError, site.full_clean)
site.domain = "test\ntest"
self.assertRaises(ValidationError, site.full_clean)
class MiddlewareTest(TestCase):
def test_request(self):
""" Makes sure that the request has correct `site` attribute. """
middleware = CurrentSiteMiddleware()
request = HttpRequest()
middleware.process_request(request)
self.assertEqual(request.site.id, settings.SITE_ID)