Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14433 - replaced a thread-unsafe solution to #10235 introduced…

… in [13980]

This patch also addresses sitemap code found in contrib/gis, which [13980]
did not.

Thanks to gabrielhurley for the initial patch.

Refs #10235, #14386

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14141 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e0ec458360527e07ccd0c0b266b188d9872de252 1 parent 5e319f5
Luke Plant authored October 11, 2010
4  django/contrib/gis/sitemaps/georss.py
@@ -36,12 +36,12 @@ def __init__(self, feed_dict, slug_dict=None):
36 36
             else:
37 37
                 self.locations.append(section)
38 38
  
39  
-    def get_urls(self, page=1):
  39
+    def get_urls(self, page=1, site=None):
40 40
         """
41 41
         This method is overrridden so the appropriate `geo_format` attribute
42 42
         is placed on each URL element.
43 43
         """
44  
-        urls = Sitemap.get_urls(self, page=page)
  44
+        urls = Sitemap.get_urls(self, page=page, site=site)
45 45
         for url in urls: url['geo_format'] = 'georss'
46 46
         return urls
47 47
 
4  django/contrib/gis/sitemaps/kml.py
@@ -40,12 +40,12 @@ def _build_kml_sources(self, sources):
40 40
                 raise TypeError('KML Sources must be a model or a 3-tuple.')
41 41
         return kml_sources
42 42
 
43  
-    def get_urls(self, page=1):
  43
+    def get_urls(self, page=1, site=None):
44 44
         """
45 45
         This method is overrridden so the appropriate `geo_format` attribute
46 46
         is placed on each URL element.
47 47
         """
48  
-        urls = Sitemap.get_urls(self, page=page)
  48
+        urls = Sitemap.get_urls(self, page=page, site=site)
49 49
         for url in urls: url['geo_format'] = self.geo_format
50 50
         return urls
51 51
 
5  django/contrib/gis/sitemaps/views.py
@@ -46,12 +46,13 @@ def sitemap(request, sitemaps, section=None):
46 46
         maps = sitemaps.values()
47 47
 
48 48
     page = request.GET.get("p", 1)
  49
+    current_site = get_current_site(request)
49 50
     for site in maps:
50 51
         try:
51 52
             if callable(site):
52  
-                urls.extend(site().get_urls(page))
  53
+                urls.extend(site().get_urls(page=page, site=current_site))
53 54
             else:
54  
-                urls.extend(site.get_urls(page))
  55
+                urls.extend(site.get_urls(page=page, site=current_site))
55 56
         except EmptyPage:
56 57
             raise Http404("Page %s empty" % page)
57 58
         except PageNotAnInteger:
24  django/contrib/sitemaps/__init__.py
... ...
@@ -1,5 +1,6 @@
1  
-from django.contrib.sites.models import get_current_site
  1
+from django.contrib.sites.models import Site, get_current_site
2 2
 from django.core import urlresolvers, paginator
  3
+from django.core.exceptions import ImproperlyConfigured
3 4
 import urllib
4 5
 
5 6
 PING_URL = "http://www.google.com/webmasters/tools/ping"
@@ -60,11 +61,19 @@ def _get_paginator(self):
60 61
         return self._paginator
61 62
     paginator = property(_get_paginator)
62 63
 
63  
-    def get_urls(self, page=1):
64  
-        current_site = get_current_site(self.request)
  64
+    def get_urls(self, page=1, site=None):
  65
+        if site is None:
  66
+            if Site._meta.installed:
  67
+                try:
  68
+                    site = Site.objects.get_current()
  69
+                except Site.DoesNotExist:
  70
+                    pass
  71
+            if site is None:
  72
+                raise ImproperlyConfigured("In order to use Sitemaps you must either use the sites framework or pass in a Site or RequestSite object in your view code.")
  73
+
65 74
         urls = []
66 75
         for item in self.paginator.page(page).object_list:
67  
-            loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
  76
+            loc = "http://%s%s" % (site.domain, self.__get('location', item))
68 77
             priority = self.__get('priority', item, None)
69 78
             url_info = {
70 79
                 'location':   loc,
@@ -77,11 +86,8 @@ def get_urls(self, page=1):
77 86
 
78 87
 class FlatPageSitemap(Sitemap):
79 88
     def items(self):
80  
-        current_site = get_current_site(self.request)
81  
-        if hasattr(current_site, "flatpage_set"):
82  
-            return current_site.flatpage_set.filter(registration_required=False)
83  
-        else:
84  
-            return ()
  89
+        current_site = Site.objects.get_current()
  90
+        return current_site.flatpage_set.filter(registration_required=False)
85 91
 
86 92
 class GenericSitemap(Sitemap):
87 93
     priority = None
21  django/contrib/sitemaps/tests/basic.py
@@ -2,7 +2,9 @@
2 2
 from django.conf import settings
3 3
 from django.contrib.auth.models import User
4 4
 from django.contrib.flatpages.models import FlatPage
  5
+from django.contrib.sitemaps import Sitemap
5 6
 from django.contrib.sites.models import Site
  7
+from django.core.exceptions import ImproperlyConfigured
6 8
 from django.test import TestCase
7 9
 from django.utils.formats import localize
8 10
 from django.utils.translation import activate, deactivate
@@ -92,3 +94,22 @@ def test_requestsite_sitemap(self):
92 94
 <url><loc>http://testserver/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
93 95
 </urlset>
94 96
 """ % date.today().strftime('%Y-%m-%d'))
  97
+
  98
+    def test_sitemap_get_urls_no_site_1(self):
  99
+        """
  100
+        Check we get ImproperlyConfigured if we don't pass a site object to
  101
+        Sitemap.get_urls and no Site objects exist
  102
+        """
  103
+        Site._meta.installed = True
  104
+        Site.objects.all().delete()
  105
+        self.assertRaises(ImproperlyConfigured, Sitemap().get_urls)
  106
+
  107
+    def test_sitemap_get_urls_no_site_2(self):
  108
+        """
  109
+        Check we get ImproperlyConfigured when we don't pass a site object to
  110
+        Sitemap.get_urls if Site objects exists, but the sites framework is not
  111
+        actually installed.
  112
+        """
  113
+        Site.objects.get_current()
  114
+        Site._meta.installed = False
  115
+        self.assertRaises(ImproperlyConfigured, Sitemap().get_urls)
6  django/contrib/sitemaps/views.py
@@ -32,13 +32,13 @@ def sitemap(request, sitemaps, section=None):
32 32
     else:
33 33
         maps = sitemaps.values()
34 34
     page = request.GET.get("p", 1)
  35
+    current_site = get_current_site(request)
35 36
     for site in maps:
36  
-        site.request = request
37 37
         try:
38 38
             if callable(site):
39  
-                urls.extend(site().get_urls(page))
  39
+                urls.extend(site().get_urls(page=page, site=current_site))
40 40
             else:
41  
-                urls.extend(site.get_urls(page))
  41
+                urls.extend(site.get_urls(page=page, site=current_site))
42 42
         except EmptyPage:
43 43
             raise Http404("Page %s empty" % page)
44 44
         except PageNotAnInteger:

0 notes on commit e0ec458

Please sign in to comment.
Something went wrong with that request. Please try again.