Loading django/contrib/sitemaps/tests/basic.py +41 −0 Original line number Diff line number Diff line import os from datetime import date from django.conf import settings from django.contrib.auth.models import User Loading @@ -16,12 +17,40 @@ class SitemapTests(TestCase): def setUp(self): self.old_USE_L10N = settings.USE_L10N self.old_Site_meta_installed = Site._meta.installed self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS settings.TEMPLATE_DIRS = ( os.path.join(os.path.dirname(__file__), 'templates'), ) # Create a user that will double as sitemap content User.objects.create_user('testuser', 'test@example.com', 's3krit') def tearDown(self): settings.USE_L10N = self.old_USE_L10N Site._meta.installed = self.old_Site_meta_installed settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS 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.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap><loc>http://example.com/simple/sitemap-simple.xml</loc></sitemap> </sitemapindex> """) 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.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> <!-- This is a customised template --> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap><loc>http://example.com/simple/sitemap-simple.xml</loc></sitemap> </sitemapindex> """) def test_simple_sitemap(self): "A simple sitemap can be rendered" Loading @@ -32,6 +61,18 @@ class SitemapTests(TestCase): <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> </urlset> """ % date.today().strftime('%Y-%m-%d')) 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.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> <!-- This is a customised template --> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> </urlset> """ % date.today().strftime('%Y-%m-%d')) @skipUnless(settings.USE_I18N, "Internationalization is not enabled") Loading django/contrib/sitemaps/tests/templates/custom_sitemap.xml 0 → 100644 +14 −0 Original line number Diff line number Diff line <?xml version="1.0" encoding="UTF-8"?> <!-- This is a customised template --> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> {% spaceless %} {% for url in urlset %} <url> <loc>{{ url.location }}</loc> {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %} {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %} {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %} </url> {% endfor %} {% endspaceless %} </urlset> django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml 0 → 100644 +5 −0 Original line number Diff line number Diff line <?xml version="1.0" encoding="UTF-8"?> <!-- This is a customised template --> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> {% for location in sitemaps %}<sitemap><loc>{{ location }}</loc></sitemap>{% endfor %} </sitemapindex> django/contrib/sitemaps/tests/urls.py +4 −0 Original line number Diff line number Diff line Loading @@ -27,7 +27,11 @@ 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/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), (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}), ) django/contrib/sitemaps/views.py +4 −4 Original line number Diff line number Diff line Loading @@ -5,7 +5,7 @@ from django.core import urlresolvers from django.utils.encoding import smart_str from django.core.paginator import EmptyPage, PageNotAnInteger def index(request, sitemaps): def index(request, sitemaps, template_name='sitemap_index.xml'): current_site = get_current_site(request) sites = [] protocol = request.is_secure() and 'https' or 'http' Loading @@ -20,10 +20,10 @@ def index(request, sitemaps): if pages > 1: for page in range(2, pages+1): sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page)) xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) xml = loader.render_to_string(template_name, {'sitemaps': sites}) return HttpResponse(xml, mimetype='application/xml') def sitemap(request, sitemaps, section=None): def sitemap(request, sitemaps, section=None, template_name='sitemap.xml'): maps, urls = [], [] if section is not None: if section not in sitemaps: Loading @@ -43,5 +43,5 @@ def sitemap(request, sitemaps, section=None): raise Http404("Page %s empty" % page) except PageNotAnInteger: raise Http404("No page '%s'" % page) xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls})) xml = smart_str(loader.render_to_string(template_name, {'urlset': urls})) return HttpResponse(xml, mimetype='application/xml') Loading
django/contrib/sitemaps/tests/basic.py +41 −0 Original line number Diff line number Diff line import os from datetime import date from django.conf import settings from django.contrib.auth.models import User Loading @@ -16,12 +17,40 @@ class SitemapTests(TestCase): def setUp(self): self.old_USE_L10N = settings.USE_L10N self.old_Site_meta_installed = Site._meta.installed self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS settings.TEMPLATE_DIRS = ( os.path.join(os.path.dirname(__file__), 'templates'), ) # Create a user that will double as sitemap content User.objects.create_user('testuser', 'test@example.com', 's3krit') def tearDown(self): settings.USE_L10N = self.old_USE_L10N Site._meta.installed = self.old_Site_meta_installed settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS 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.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap><loc>http://example.com/simple/sitemap-simple.xml</loc></sitemap> </sitemapindex> """) 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.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> <!-- This is a customised template --> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <sitemap><loc>http://example.com/simple/sitemap-simple.xml</loc></sitemap> </sitemapindex> """) def test_simple_sitemap(self): "A simple sitemap can be rendered" Loading @@ -32,6 +61,18 @@ class SitemapTests(TestCase): <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> </urlset> """ % date.today().strftime('%Y-%m-%d')) 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.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?> <!-- This is a customised template --> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url> </urlset> """ % date.today().strftime('%Y-%m-%d')) @skipUnless(settings.USE_I18N, "Internationalization is not enabled") Loading
django/contrib/sitemaps/tests/templates/custom_sitemap.xml 0 → 100644 +14 −0 Original line number Diff line number Diff line <?xml version="1.0" encoding="UTF-8"?> <!-- This is a customised template --> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> {% spaceless %} {% for url in urlset %} <url> <loc>{{ url.location }}</loc> {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %} {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %} {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %} </url> {% endfor %} {% endspaceless %} </urlset>
django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml 0 → 100644 +5 −0 Original line number Diff line number Diff line <?xml version="1.0" encoding="UTF-8"?> <!-- This is a customised template --> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> {% for location in sitemaps %}<sitemap><loc>{{ location }}</loc></sitemap>{% endfor %} </sitemapindex>
django/contrib/sitemaps/tests/urls.py +4 −0 Original line number Diff line number Diff line Loading @@ -27,7 +27,11 @@ 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/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}), (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}), )
django/contrib/sitemaps/views.py +4 −4 Original line number Diff line number Diff line Loading @@ -5,7 +5,7 @@ from django.core import urlresolvers from django.utils.encoding import smart_str from django.core.paginator import EmptyPage, PageNotAnInteger def index(request, sitemaps): def index(request, sitemaps, template_name='sitemap_index.xml'): current_site = get_current_site(request) sites = [] protocol = request.is_secure() and 'https' or 'http' Loading @@ -20,10 +20,10 @@ def index(request, sitemaps): if pages > 1: for page in range(2, pages+1): sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page)) xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) xml = loader.render_to_string(template_name, {'sitemaps': sites}) return HttpResponse(xml, mimetype='application/xml') def sitemap(request, sitemaps, section=None): def sitemap(request, sitemaps, section=None, template_name='sitemap.xml'): maps, urls = [], [] if section is not None: if section not in sitemaps: Loading @@ -43,5 +43,5 @@ def sitemap(request, sitemaps, section=None): raise Http404("Page %s empty" % page) except PageNotAnInteger: raise Http404("No page '%s'" % page) xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls})) xml = smart_str(loader.render_to_string(template_name, {'urlset': urls})) return HttpResponse(xml, mimetype='application/xml')