summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2012-01-29 09:30:28 +0000
committerAymeric Augustin <aymeric.augustin@m4x.org>2012-01-29 09:30:28 +0000
commit123f567093eb3bd2f9cb295f4553eb62433c2962 (patch)
treeb26414d7b818785cf5ed68ba6b7ad93cfd492aea
parentd9061c01a975c3313a815f793cd773d3e657c3cf (diff)
Fixed #2713 -- Made the name of the sitemap view a parameter of the sitemap 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
-rw-r--r--django/contrib/sitemaps/tests/basic.py24
-rw-r--r--django/contrib/sitemaps/tests/urls.py18
-rw-r--r--django/contrib/sitemaps/views.py9
-rw-r--r--docs/ref/contrib/sitemaps.txt21
4 files changed, 50 insertions, 22 deletions
diff --git a/django/contrib/sitemaps/tests/basic.py b/django/contrib/sitemaps/tests/basic.py
index 775e0d9736..b675c3d6fb 100644
--- a/django/contrib/sitemaps/tests/basic.py
+++ b/django/contrib/sitemaps/tests/basic.py
@@ -37,9 +37,7 @@ class SitemapTests(TestCase):
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 @@ class SitemapTests(TestCase):
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 @@ class SitemapTests(TestCase):
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 @@ class SitemapTests(TestCase):
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 @@ class SitemapTests(TestCase):
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 @@ class SitemapTests(TestCase):
# 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 @@ class SitemapTests(TestCase):
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)
diff --git a/django/contrib/sitemaps/tests/urls.py b/django/contrib/sitemaps/tests/urls.py
index c264f940d2..d9ff7200d3 100644
--- a/django/contrib/sitemaps/tests/urls.py
+++ b/django/contrib/sitemaps/tests/urls.py
@@ -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 @@ flatpage_sitemaps = {
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')
)
diff --git a/django/contrib/sitemaps/views.py b/django/contrib/sitemaps/views.py
index 14af5f1e4c..2760339f8b 100644
--- a/django/contrib/sitemaps/views.py
+++ b/django/contrib/sitemaps/views.py
@@ -4,7 +4,9 @@ from django.core.paginator import EmptyPage, PageNotAnInteger
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:
diff --git a/docs/ref/contrib/sitemaps.txt b/docs/ref/contrib/sitemaps.txt
index f3ca8d1031..f4ae417666 100644
--- a/docs/ref/contrib/sitemaps.txt
+++ b/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
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::