Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2713 -- Made the name of the sitemap view a parameter of the s…

…itemap index view, in order to allow decorators. Also cleaned up code in the area and removed redundant comments from the tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17408 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 123f567093eb3bd2f9cb295f4553eb62433c2962 1 parent d9061c0
Aymeric Augustin authored January 29, 2012
24  django/contrib/sitemaps/tests/basic.py
@@ -37,9 +37,7 @@ def tearDown(self):
37 37
 
38 38
     def test_simple_sitemap_index(self):
39 39
         "A simple sitemap index can be rendered"
40  
-        # Retrieve the sitemap.
41 40
         response = self.client.get('/simple/index.xml')
42  
-        # Check for all the important bits:
43 41
         self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
44 42
 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
45 43
 <sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
@@ -48,9 +46,7 @@ def test_simple_sitemap_index(self):
48 46
 
49 47
     def test_simple_sitemap_custom_index(self):
50 48
         "A simple sitemap index can be rendered with a custom template"
51  
-        # Retrieve the sitemap.
52 49
         response = self.client.get('/simple/custom-index.xml')
53  
-        # Check for all the important bits:
54 50
         self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
55 51
 <!-- This is a customised template -->
56 52
 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@@ -60,9 +56,7 @@ def test_simple_sitemap_custom_index(self):
60 56
 
61 57
     def test_simple_sitemap(self):
62 58
         "A simple sitemap can be rendered"
63  
-        # Retrieve the sitemap.
64 59
         response = self.client.get('/simple/sitemap.xml')
65  
-        # Check for all the important bits:
66 60
         self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
67 61
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
68 62
 <url><loc>%s/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
@@ -71,9 +65,7 @@ def test_simple_sitemap(self):
71 65
 
72 66
     def test_simple_custom_sitemap(self):
73 67
         "A simple sitemap can be rendered with a custom template"
74  
-        # Retrieve the sitemap.
75 68
         response = self.client.get('/simple/custom-sitemap.xml')
76  
-        # Check for all the important bits:
77 69
         self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
78 70
 <!-- This is a customised template -->
79 71
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@@ -98,13 +90,10 @@ def test_localized_priority(self):
98 90
 
99 91
     def test_generic_sitemap(self):
100 92
         "A minimal generic sitemap can be rendered"
101  
-        # Retrieve the sitemap.
102 93
         response = self.client.get('/generic/sitemap.xml')
103  
-
104 94
         expected = ''
105 95
         for username in User.objects.values_list("username", flat=True):
106 96
             expected += "<url><loc>%s/users/%s/</loc></url>" % (self.base_url, username)
107  
-        # Check for all the important bits:
108 97
         self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
109 98
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
110 99
 %s
@@ -145,9 +134,7 @@ def test_requestsite_sitemap(self):
145 134
         # Make sure hitting the flatpages sitemap without the sites framework
146 135
         # installed doesn't raise an exception
147 136
         Site._meta.installed = False
148  
-        # Retrieve the sitemap.
149 137
         response = self.client.get('/simple/sitemap.xml')
150  
-        # Check for all the important bits:
151 138
         self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
152 139
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
153 140
 <url><loc>http://testserver/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
@@ -182,3 +169,14 @@ def is_user(url):
182 169
             return isinstance(url['item'], User)
183 170
         item_in_url_info = all(map(is_user, user_sitemap.get_urls()))
184 171
         self.assertTrue(item_in_url_info)
  172
+
  173
+    def test_cached_sitemap_index(self):
  174
+        """
  175
+        Check that a cached sitemap index can be rendered (#2713).
  176
+        """
  177
+        response = self.client.get('/cached/index.xml')
  178
+        self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
  179
+<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  180
+<sitemap><loc>%s/cached/sitemap-simple.xml</loc></sitemap>
  181
+</sitemapindex>
  182
+""" % self.base_url)
18  django/contrib/sitemaps/tests/urls.py
... ...
@@ -1,7 +1,8 @@
1 1
 from datetime import datetime
2  
-from django.conf.urls import patterns
3  
-from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap
  2
+from django.conf.urls import patterns, url
  3
+from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap, views
4 4
 from django.contrib.auth.models import User
  5
+from django.views.decorators.cache import cache_page
5 6
 
6 7
 class SimpleSitemap(Sitemap):
7 8
     changefreq = "never"
@@ -28,10 +29,17 @@ def items(self):
28 29
 
29 30
 urlpatterns = patterns('django.contrib.sitemaps.views',
30 31
     (r'^simple/index\.xml$', 'index', {'sitemaps': simple_sitemaps}),
31  
-    (r'^simple/custom-index\.xml$', 'index', {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}),
32  
-    (r'^simple/sitemap-(?P<section>.+)\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
  32
+    (r'^simple/custom-index\.xml$', 'index',
  33
+        {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}),
  34
+    (r'^simple/sitemap-(?P<section>.+)\.xml$', 'sitemap',
  35
+        {'sitemaps': simple_sitemaps}),
33 36
     (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
34  
-    (r'^simple/custom-sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}),
  37
+    (r'^simple/custom-sitemap\.xml$', 'sitemap',
  38
+        {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}),
35 39
     (r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}),
36 40
     (r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}),
  41
+    url(r'^cached/index\.xml$', cache_page(1)(views.index),
  42
+        {'sitemaps': simple_sitemaps, 'sitemap_url_name': 'cached_sitemap'}),
  43
+    url(r'^cached/sitemap-(?P<section>.+)\.xml', cache_page(1)(views.sitemap),
  44
+        {'sitemaps': simple_sitemaps}, name='cached_sitemap')
37 45
 )
9  django/contrib/sitemaps/views.py
@@ -4,7 +4,9 @@
4 4
 from django.http import Http404
5 5
 from django.template.response import TemplateResponse
6 6
 
7  
-def index(request, sitemaps, template_name='sitemap_index.xml', mimetype='application/xml'):
  7
+def index(request, sitemaps,
  8
+          template_name='sitemap_index.xml', mimetype='application/xml',
  9
+          sitemap_url_name='django.contrib.sitemaps.views.sitemap'):
8 10
     current_site = get_current_site(request)
9 11
     sites = []
10 12
     protocol = request.is_secure() and 'https' or 'http'
@@ -14,14 +16,15 @@ def index(request, sitemaps, template_name='sitemap_index.xml', mimetype='applic
14 16
             pages = site().paginator.num_pages
15 17
         else:
16 18
             pages = site.paginator.num_pages
17  
-        sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': section})
  19
+        sitemap_url = urlresolvers.reverse(sitemap_url_name, kwargs={'section': section})
18 20
         sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url))
19 21
         if pages > 1:
20 22
             for page in range(2, pages+1):
21 23
                 sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page))
22 24
     return TemplateResponse(request, template_name, {'sitemaps': sites}, content_type=mimetype)
23 25
 
24  
-def sitemap(request, sitemaps, section=None, template_name='sitemap.xml', mimetype='application/xml'):
  26
+def sitemap(request, sitemaps, section=None,
  27
+            template_name='sitemap.xml', mimetype='application/xml'):
25 28
     maps, urls = [], []
26 29
     if section is not None:
27 30
         if section not in sitemaps:
21  docs/ref/contrib/sitemaps.txt
@@ -293,11 +293,30 @@ You should create an index file if one of your sitemaps has more than 50,000
293 293
 URLs. In this case, Django will automatically paginate the sitemap, and the
294 294
 index will reflect that.
295 295
 
296  
-.. versionadded:: 1.3
  296
+.. versionadded:: 1.4
  297
+
  298
+If you are not using the vanilla sitemap view -- for example, if it is wrapped
  299
+with a caching decorator -- you must name your sitemap view and pass
  300
+``sitemap_url_name`` to the index view::
  301
+
  302
+    from django.contrib.sitemaps import views as sitemaps_views
  303
+    from django.views.decorators.cache import cache_page
  304
+
  305
+    urlpatterns = patterns('',
  306
+        url(r'^sitemap.xml$',
  307
+            cache_page(86400)(sitemaps_views.index),
  308
+            {'sitemaps': sitemaps, 'sitemap_url_name': 'sitemaps'}),
  309
+        url(r'^sitemap-(?P<section>.+)\.xml$',
  310
+            cache_page(86400)(sitemaps_views.sitemap),
  311
+            {'sitemaps': sitemaps}, name='sitemaps'),
  312
+    )
  313
+
297 314
 
298 315
 Template customization
299 316
 ======================
300 317
 
  318
+.. versionadded:: 1.3
  319
+
301 320
 If you wish to use a different template for each sitemap or sitemap index
302 321
 available on your site, you may specify it by passing a ``template_name``
303 322
 parameter to the ``sitemap`` and ``index`` views via the URLconf::

0 notes on commit 123f567

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