From a4904110e180bcd6ba188c92db832c018b8f6c7d Mon Sep 17 00:00:00 2001
From: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
Date: Thu, 3 Apr 2014 20:15:53 +0200
Subject: [PATCH] By using the request to do the Site matching
Signed-off-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
---
django/contrib/sites/models.py | 49 +++++++++++++++++++++++++--------------
django/contrib/sites/shortcuts.py | 4 +++-
django/contrib/sites/tests.py | 11 +++++++++
docs/ref/contrib/sites.txt | 16 ++++++++++---
4 files changed, 59 insertions(+), 21 deletions(-)
diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py
index 0e80d91..6ff6465 100644
a
|
b
|
def _simple_domain_name_validator(value):
|
34 | 34 | |
35 | 35 | class SiteManager(models.Manager): |
36 | 36 | |
37 | | def get_current(self): |
| 37 | def _get_site_by_id(self, sid): |
| 38 | if not sid in SITE_CACHE: |
| 39 | site = self.get(pk=sid) |
| 40 | SITE_CACHE[sid] = site |
| 41 | return SITE_CACHE[sid] |
| 42 | |
| 43 | def _get_site_by_request(self, request): |
| 44 | host = request.get_host().lower() |
| 45 | if ':' in host: |
| 46 | host = host.split(':', 1)[0] |
| 47 | if not host in SITE_CACHE: |
| 48 | site = self.get(domain__iexact=host) |
| 49 | SITE_CACHE[host] = site |
| 50 | return SITE_CACHE[host] |
| 51 | |
| 52 | def get_current(self, request=None): |
38 | 53 | """ |
39 | 54 | Returns the current ``Site`` based on the SITE_ID in the |
40 | | project's settings. The ``Site`` object is cached the first |
41 | | time it's retrieved from the database. |
| 55 | project's settings if request is None. Otherwise return the |
| 56 | the current ``Site`` matching its domain with request's host |
| 57 | The ``Site`` object is cached the first time it's retrieved |
| 58 | from the database. |
42 | 59 | """ |
43 | | from django.conf import settings |
44 | | try: |
45 | | sid = settings.SITE_ID |
46 | | except AttributeError: |
47 | | raise ImproperlyConfigured( |
48 | | "You're using the Django \"sites framework\" without having " |
49 | | "set the SITE_ID setting. Create a site in your database and " |
50 | | "set the SITE_ID setting to fix this error.") |
51 | | try: |
52 | | current_site = SITE_CACHE[sid] |
53 | | except KeyError: |
54 | | current_site = self.get(pk=sid) |
55 | | SITE_CACHE[sid] = current_site |
56 | | return current_site |
| 60 | if request: |
| 61 | return self._get_site_by_request(request) |
| 62 | else: |
| 63 | from django.conf import settings |
| 64 | try: |
| 65 | sid = settings.SITE_ID |
| 66 | except AttributeError: |
| 67 | raise ImproperlyConfigured( |
| 68 | "You're using the Django \"sites framework\" without having " |
| 69 | "set the SITE_ID setting. Create a site in your database and " |
| 70 | "set the SITE_ID setting to fix this error.") |
| 71 | return self._get_site_by_id(sid) |
57 | 72 | |
58 | 73 | def clear_cache(self): |
59 | 74 | """Clears the ``Site`` object cache.""" |
diff --git a/django/contrib/sites/shortcuts.py b/django/contrib/sites/shortcuts.py
index c525948..ddff468 100644
a
|
b
|
def get_current_site(request):
|
12 | 12 | # the Site models when django.contrib.sites isn't installed. |
13 | 13 | if apps.is_installed('django.contrib.sites'): |
14 | 14 | from .models import Site |
15 | | return Site.objects.get_current() |
| 15 | from django.conf import settings |
| 16 | req = None if hasattr(settings, 'SITE_ID') else request |
| 17 | return Site.objects.get_current(req) |
16 | 18 | else: |
17 | 19 | from .requests import RequestSite |
18 | 20 | return RequestSite(request) |
diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py
index 8f80087..66c47e2 100644
a
|
b
|
class SitesFrameworkTests(TestCase):
|
71 | 71 | self.assertTrue(isinstance(site, RequestSite)) |
72 | 72 | self.assertEqual(site.name, "example.com") |
73 | 73 | |
| 74 | @override_settings(SITE_ID='', ALLOWED_HOSTS=['example.com']) |
| 75 | def test_get_current_site_no_site_id(self): |
| 76 | request = HttpRequest() |
| 77 | request.META = { |
| 78 | "SERVER_NAME": "example.com", |
| 79 | "SERVER_PORT": "80", |
| 80 | } |
| 81 | del settings.SITE_ID |
| 82 | site = get_current_site(request) |
| 83 | self.assertEqual(site.name, "example.com") |
| 84 | |
74 | 85 | def test_domain_name_with_whitespaces(self): |
75 | 86 | # Regression for #17320 |
76 | 87 | # Domain names are not allowed contain whitespace characters |
diff --git a/docs/ref/contrib/sites.txt b/docs/ref/contrib/sites.txt
index 3e330a0..390a76f 100644
a
|
b
|
The sites framework is mainly based on a simple model:
|
18 | 18 | .. class:: models.Site |
19 | 19 | |
20 | 20 | A model for storing the ``domain`` and ``name`` attributes of a Web site. |
21 | | The :setting:`SITE_ID` setting specifies the database ID of the |
22 | | :class:`~django.contrib.sites.models.Site` object associated with that |
23 | | particular settings file. |
24 | 21 | |
25 | 22 | .. attribute:: domain |
26 | 23 | |
… |
… |
The sites framework is mainly based on a simple model:
|
30 | 27 | |
31 | 28 | A human-readable "verbose" name for the Web site. |
32 | 29 | |
| 30 | The :setting:`SITE_ID` setting specifies the database ID of the |
| 31 | :class:`~django.contrib.sites.models.Site` object associated with that |
| 32 | particular settings file. It is possible to omit it, but then the |
| 33 | :func:`~django.contrib.sites.shortcuts.get_current_site` function will always |
| 34 | try to get the current site by comparing the |
| 35 | :attr:`~django.contrib.sites.models.Site.domain` with the host name from |
| 36 | the :meth:`~django.http.HttpRequest.get_host` method. |
| 37 | |
33 | 38 | How you use this is up to you, but Django uses it in a couple of ways |
34 | 39 | automatically via simple conventions. |
35 | 40 | |
… |
… |
model(s). It's a model :doc:`manager </topics/db/managers>` that
|
302 | 307 | automatically filters its queries to include only objects associated |
303 | 308 | with the current :class:`~django.contrib.sites.models.Site`. |
304 | 309 | |
| 310 | .. admonition:: Mandatory SITE_ID |
| 311 | |
| 312 | The ``CurrentSiteManager`` is only usable when the :setting:`SITE_ID` |
| 313 | setting is defined in your settings. |
| 314 | |
305 | 315 | Use :class:`~django.contrib.sites.managers.CurrentSiteManager` by adding it to |
306 | 316 | your model explicitly. For example:: |
307 | 317 | |