Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #14198 -- Corrected rendering of generic sitemaps when …

…no priority is specified. Thanks to palkeo for the report.

Backport of r13676 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13677 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4d8af2faa3bde14d2f3a5a30b0772b554c8af245 1 parent 5295d67
Russell Keith-Magee authored August 30, 2010
3  django/contrib/sitemaps/__init__.py
@@ -65,11 +65,12 @@ def get_urls(self, page=1):
65 65
         urls = []
66 66
         for item in self.paginator.page(page).object_list:
67 67
             loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
  68
+            priority = self.__get('priority', item, None)
68 69
             url_info = {
69 70
                 'location':   loc,
70 71
                 'lastmod':    self.__get('lastmod', item, None),
71 72
                 'changefreq': self.__get('changefreq', item, None),
72  
-                'priority':   str(self.__get('priority', item, ''))
  73
+                'priority':   str(priority is not None and priority or '')
73 74
             }
74 75
             urls.append(url_info)
75 76
         return urls
20  django/contrib/sitemaps/tests/basic.py
... ...
@@ -1,5 +1,6 @@
1 1
 from datetime import date
2 2
 from django.conf import settings
  3
+from django.contrib.auth.models import User
3 4
 from django.test import TestCase
4 5
 from django.utils.formats import localize
5 6
 from django.utils.translation import activate
@@ -10,6 +11,8 @@ class SitemapTests(TestCase):
10 11
 
11 12
     def setUp(self):
12 13
         self.old_USE_L10N = settings.USE_L10N
  14
+        # Create a user that will double as sitemap content
  15
+        User.objects.create_user('testuser', 'test@example.com', 's3krit')
13 16
 
14 17
     def tearDown(self):
15 18
         settings.USE_L10N = self.old_USE_L10N
@@ -17,11 +20,11 @@ def tearDown(self):
17 20
     def test_simple_sitemap(self):
18 21
         "A simple sitemap can be rendered"
19 22
         # Retrieve the sitemap.
20  
-        response = self.client.get('/sitemaps/sitemap.xml')
  23
+        response = self.client.get('/simple/sitemap.xml')
21 24
         # Check for all the important bits:
22 25
         self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?>
23 26
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
24  
-<url><loc>http://example.com/ticket14164</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
  27
+<url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
25 28
 </urlset>
26 29
 """ % date.today().strftime('%Y-%m-%d'))
27 30
 
@@ -34,6 +37,17 @@ def test_localized_priority(self):
34 37
 
35 38
         # Retrieve the sitemap. Check that priorities
36 39
         # haven't been rendered in localized format
37  
-        response = self.client.get('/sitemaps/sitemap.xml')
  40
+        response = self.client.get('/simple/sitemap.xml')
38 41
         self.assertContains(response, '<priority>0.5</priority>')
39 42
         self.assertContains(response, '<lastmod>%s</lastmod>' % date.today().strftime('%Y-%m-%d'))
  43
+
  44
+    def test_generic_sitemap(self):
  45
+        "A minimal generic sitemap can be rendered"
  46
+        # Retrieve the sitemap.
  47
+        response = self.client.get('/generic/sitemap.xml')
  48
+        # Check for all the important bits:
  49
+        self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?>
  50
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  51
+<url><loc>http://example.com/users/testuser/</loc></url>
  52
+</urlset>
  53
+""")
16  django/contrib/sitemaps/tests/urls.py
... ...
@@ -1,20 +1,28 @@
1 1
 from datetime import datetime
2 2
 from django.conf.urls.defaults import *
3  
-from django.contrib.sitemaps import Sitemap
  3
+from django.contrib.sitemaps import Sitemap, GenericSitemap
  4
+from django.contrib.auth.models import User
4 5
 
5 6
 class SimpleSitemap(Sitemap):
6 7
     changefreq = "never"
7 8
     priority = 0.5
8  
-    location = '/ticket14164'
  9
+    location = '/location/'
9 10
     lastmod = datetime.now()
10 11
 
11 12
     def items(self):
12 13
         return [object()]
13 14
 
14  
-sitemaps = {
  15
+simple_sitemaps = {
15 16
     'simple': SimpleSitemap,
16 17
 }
17 18
 
  19
+generic_sitemaps = {
  20
+    'generic': GenericSitemap({
  21
+        'queryset': User.objects.all()
  22
+    }),
  23
+}
  24
+
18 25
 urlpatterns = patterns('django.contrib.sitemaps.views',
19  
-    (r'^sitemaps/sitemap\.xml$', 'sitemap', {'sitemaps': sitemaps}),
  26
+    (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
  27
+    (r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}),
20 28
 )

0 notes on commit 4d8af2f

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