Fixed #18351 -- Added X-Robots-Tag header to sitemaps

Thanks Michael Lissner for the report and initial patch, and
Tom Mortimer-Jones for working on the patch.
This commit is contained in:
Claude Paroz 2013-05-04 12:08:15 +02:00
parent ac9daa0cbd
commit 66c83dce07
2 changed files with 18 additions and 0 deletions

View File

@ -144,3 +144,10 @@ class HTTPSitemapTests(SitemapTestsBase):
</sitemapindex> </sitemapindex>
""" % self.base_url """ % self.base_url
self.assertXMLEqual(response.content.decode('utf-8'), expected_content) self.assertXMLEqual(response.content.decode('utf-8'), expected_content)
def test_x_robots_sitemap(self):
response = self.client.get('/simple/index.xml')
self.assertEqual(response['X-Robots-Tag'], 'noindex, noodp, noarchive')
response = self.client.get('/simple/sitemap.xml')
self.assertEqual(response['X-Robots-Tag'], 'noindex, noodp, noarchive')

View File

@ -6,7 +6,17 @@ from django.core.paginator import EmptyPage, PageNotAnInteger
from django.http import Http404 from django.http import Http404
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.utils import six from django.utils import six
from django.utils.functional import wraps
def x_robots_tag(func):
@wraps(func)
def inner(request, *args, **kwargs):
response = func(request, *args, **kwargs)
response['X-Robots-Tag'] = 'noindex, noodp, noarchive'
return response
return inner
@x_robots_tag
def index(request, sitemaps, def index(request, sitemaps,
template_name='sitemap_index.xml', content_type='application/xml', template_name='sitemap_index.xml', content_type='application/xml',
sitemap_url_name='django.contrib.sitemaps.views.sitemap', sitemap_url_name='django.contrib.sitemaps.views.sitemap',
@ -35,6 +45,7 @@ def index(request, sitemaps,
return TemplateResponse(request, template_name, {'sitemaps': sites}, return TemplateResponse(request, template_name, {'sitemaps': sites},
content_type=content_type) content_type=content_type)
@x_robots_tag
def sitemap(request, sitemaps, section=None, def sitemap(request, sitemaps, section=None,
template_name='sitemap.xml', content_type='application/xml', template_name='sitemap.xml', content_type='application/xml',
mimetype=None): mimetype=None):