Permalink
Browse files

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...
aaugustin committed Jan 29, 2012
1 parent d9061c0 commit 123f567093eb3bd2f9cb295f4553eb62433c2962
@@ -37,9 +37,7 @@ def tearDown(self):
def test_simple_sitemap_index(self):
"A simple sitemap index can be rendered"
# Retrieve the sitemap.
response = self.client.get('/simple/index.xml')
# Check for all the important bits:
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/simple/sitemap-simple.xml</loc></sitemap>
@@ -48,9 +46,7 @@ def test_simple_sitemap_index(self):
def test_simple_sitemap_custom_index(self):
"A simple sitemap index can be rendered with a custom template"
# Retrieve the sitemap.
response = self.client.get('/simple/custom-index.xml')
# Check for all the important bits:
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a customised template -->
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@@ -60,9 +56,7 @@ def test_simple_sitemap_custom_index(self):
def test_simple_sitemap(self):
"A simple sitemap can be rendered"
# Retrieve the sitemap.
response = self.client.get('/simple/sitemap.xml')
# Check for all the important bits:
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<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):
def test_simple_custom_sitemap(self):
"A simple sitemap can be rendered with a custom template"
# Retrieve the sitemap.
response = self.client.get('/simple/custom-sitemap.xml')
# Check for all the important bits:
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a customised template -->
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@@ -98,13 +90,10 @@ def test_localized_priority(self):
def test_generic_sitemap(self):
"A minimal generic sitemap can be rendered"
# Retrieve the sitemap.
response = self.client.get('/generic/sitemap.xml')
expected = ''
for username in User.objects.values_list("username", flat=True):
expected += "<url><loc>%s/users/%s/</loc></url>" % (self.base_url, username)
# Check for all the important bits:
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
%s
@@ -145,9 +134,7 @@ def test_requestsite_sitemap(self):
# Make sure hitting the flatpages sitemap without the sites framework
# installed doesn't raise an exception
Site._meta.installed = False
# Retrieve the sitemap.
response = self.client.get('/simple/sitemap.xml')
# Check for all the important bits:
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<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):
return isinstance(url['item'], User)
item_in_url_info = all(map(is_user, user_sitemap.get_urls()))
self.assertTrue(item_in_url_info)
def test_cached_sitemap_index(self):
"""
Check that a cached sitemap index can be rendered (#2713).
"""
response = self.client.get('/cached/index.xml')
self.assertEqual(response.content, """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>%s/cached/sitemap-simple.xml</loc></sitemap>
</sitemapindex>
""" % self.base_url)
@@ -1,7 +1,8 @@
from datetime import datetime
from django.conf.urls import patterns
from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap
from django.conf.urls import patterns, url
from django.contrib.sitemaps import Sitemap, GenericSitemap, FlatPageSitemap, views
from django.contrib.auth.models import User
from django.views.decorators.cache import cache_page
class SimpleSitemap(Sitemap):
changefreq = "never"
@@ -28,10 +29,17 @@ def items(self):
urlpatterns = patterns('django.contrib.sitemaps.views',
(r'^simple/index\.xml$', 'index', {'sitemaps': simple_sitemaps}),
(r'^simple/custom-index\.xml$', 'index', {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}),
(r'^simple/sitemap-(?P<section>.+)\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
(r'^simple/custom-index\.xml$', 'index',
{'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap_index.xml'}),
(r'^simple/sitemap-(?P<section>.+)\.xml$', 'sitemap',
{'sitemaps': simple_sitemaps}),
(r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
(r'^simple/custom-sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}),
(r'^simple/custom-sitemap\.xml$', 'sitemap',
{'sitemaps': simple_sitemaps, 'template_name': 'custom_sitemap.xml'}),
(r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}),
(r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}),
url(r'^cached/index\.xml$', cache_page(1)(views.index),
{'sitemaps': simple_sitemaps, 'sitemap_url_name': 'cached_sitemap'}),
url(r'^cached/sitemap-(?P<section>.+)\.xml', cache_page(1)(views.sitemap),
{'sitemaps': simple_sitemaps}, name='cached_sitemap')
)
@@ -4,7 +4,9 @@
from django.http import Http404
from django.template.response import TemplateResponse
def index(request, sitemaps, template_name='sitemap_index.xml', mimetype='application/xml'):
def index(request, sitemaps,
template_name='sitemap_index.xml', mimetype='application/xml',
sitemap_url_name='django.contrib.sitemaps.views.sitemap'):
current_site = get_current_site(request)
sites = []
protocol = request.is_secure() and 'https' or 'http'
@@ -14,14 +16,15 @@ def index(request, sitemaps, template_name='sitemap_index.xml', mimetype='applic
pages = site().paginator.num_pages
else:
pages = site.paginator.num_pages
sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': section})
sitemap_url = urlresolvers.reverse(sitemap_url_name, kwargs={'section': section})
sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url))
if pages > 1:
for page in range(2, pages+1):
sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page))
return TemplateResponse(request, template_name, {'sitemaps': sites}, content_type=mimetype)
def sitemap(request, sitemaps, section=None, template_name='sitemap.xml', mimetype='application/xml'):
def sitemap(request, sitemaps, section=None,
template_name='sitemap.xml', mimetype='application/xml'):
maps, urls = [], []
if section is not None:
if section not in sitemaps:
@@ -293,11 +293,30 @@ You should create an index file if one of your sitemaps has more than 50,000
URLs. In this case, Django will automatically paginate the sitemap, and the
index will reflect that.
.. versionadded:: 1.3
.. versionadded:: 1.4
If you are not using the vanilla sitemap view -- for example, if it is wrapped
with a caching decorator -- you must name your sitemap view and pass
``sitemap_url_name`` to the index view::
from django.contrib.sitemaps import views as sitemaps_views
from django.views.decorators.cache import cache_page
urlpatterns = patterns('',
url(r'^sitemap.xml$',
cache_page(86400)(sitemaps_views.index),
{'sitemaps': sitemaps, 'sitemap_url_name': 'sitemaps'}),
url(r'^sitemap-(?P<section>.+)\.xml$',
cache_page(86400)(sitemaps_views.sitemap),
{'sitemaps': sitemaps}, name='sitemaps'),
)
Template customization
======================
.. versionadded:: 1.3
If you wish to use a different template for each sitemap or sitemap index
available on your site, you may specify it by passing a ``template_name``
parameter to the ``sitemap`` and ``index`` views via the URLconf::

0 comments on commit 123f567

Please sign in to comment.