Fixed #14198 -- Corrected rendering of generic sitemaps when no priority is specified. Thanks to palkeo for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13676 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-08-30 15:09:12 +00:00
parent bb79b01774
commit 0de3e7a756
3 changed files with 31 additions and 8 deletions

View File

@ -65,11 +65,12 @@ class Sitemap(object):
urls = [] urls = []
for item in self.paginator.page(page).object_list: for item in self.paginator.page(page).object_list:
loc = "http://%s%s" % (current_site.domain, self.__get('location', item)) loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
priority = self.__get('priority', item, None)
url_info = { url_info = {
'location': loc, 'location': loc,
'lastmod': self.__get('lastmod', item, None), 'lastmod': self.__get('lastmod', item, None),
'changefreq': self.__get('changefreq', item, None), 'changefreq': self.__get('changefreq', item, None),
'priority': str(self.__get('priority', item, '')) 'priority': str(priority is not None and priority or '')
} }
urls.append(url_info) urls.append(url_info)
return urls return urls

View File

@ -1,5 +1,6 @@
from datetime import date from datetime import date
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase from django.test import TestCase
from django.utils.formats import localize from django.utils.formats import localize
from django.utils.translation import activate from django.utils.translation import activate
@ -10,6 +11,8 @@ class SitemapTests(TestCase):
def setUp(self): def setUp(self):
self.old_USE_L10N = settings.USE_L10N self.old_USE_L10N = settings.USE_L10N
# Create a user that will double as sitemap content
User.objects.create_user('testuser', 'test@example.com', 's3krit')
def tearDown(self): def tearDown(self):
settings.USE_L10N = self.old_USE_L10N settings.USE_L10N = self.old_USE_L10N
@ -17,11 +20,11 @@ class SitemapTests(TestCase):
def test_simple_sitemap(self): def test_simple_sitemap(self):
"A simple sitemap can be rendered" "A simple sitemap can be rendered"
# Retrieve the sitemap. # Retrieve the sitemap.
response = self.client.get('/sitemaps/sitemap.xml') response = self.client.get('/simple/sitemap.xml')
# Check for all the important bits: # Check for all the important bits:
self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>http://example.com/ticket14164</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> <url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
</urlset> </urlset>
""" % date.today().strftime('%Y-%m-%d')) """ % date.today().strftime('%Y-%m-%d'))
@ -34,6 +37,17 @@ class SitemapTests(TestCase):
# Retrieve the sitemap. Check that priorities # Retrieve the sitemap. Check that priorities
# haven't been rendered in localized format # haven't been rendered in localized format
response = self.client.get('/sitemaps/sitemap.xml') response = self.client.get('/simple/sitemap.xml')
self.assertContains(response, '<priority>0.5</priority>') self.assertContains(response, '<priority>0.5</priority>')
self.assertContains(response, '<lastmod>%s</lastmod>' % date.today().strftime('%Y-%m-%d')) self.assertContains(response, '<lastmod>%s</lastmod>' % date.today().strftime('%Y-%m-%d'))
def test_generic_sitemap(self):
"A minimal generic sitemap can be rendered"
# Retrieve the sitemap.
response = self.client.get('/generic/sitemap.xml')
# Check for all the important bits:
self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>http://example.com/users/testuser/</loc></url>
</urlset>
""")

View File

@ -1,20 +1,28 @@
from datetime import datetime from datetime import datetime
from django.conf.urls.defaults import * from django.conf.urls.defaults import *
from django.contrib.sitemaps import Sitemap from django.contrib.sitemaps import Sitemap, GenericSitemap
from django.contrib.auth.models import User
class SimpleSitemap(Sitemap): class SimpleSitemap(Sitemap):
changefreq = "never" changefreq = "never"
priority = 0.5 priority = 0.5
location = '/ticket14164' location = '/location/'
lastmod = datetime.now() lastmod = datetime.now()
def items(self): def items(self):
return [object()] return [object()]
sitemaps = { simple_sitemaps = {
'simple': SimpleSitemap, 'simple': SimpleSitemap,
} }
generic_sitemaps = {
'generic': GenericSitemap({
'queryset': User.objects.all()
}),
}
urlpatterns = patterns('django.contrib.sitemaps.views', urlpatterns = patterns('django.contrib.sitemaps.views',
(r'^sitemaps/sitemap\.xml$', 'sitemap', {'sitemaps': sitemaps}), (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
(r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}),
) )