Ticket #14041: 14041_sitemap_templates.diff

File 14041_sitemap_templates.diff, 7.7 KB (added by Julien Phalip, 13 years ago)
  • django/contrib/sitemaps/tests/basic.py

     
     1import os
    12from datetime import date
    23from django.conf import settings
    34from django.contrib.auth.models import User
     
    1617    def setUp(self):
    1718        self.old_USE_L10N = settings.USE_L10N
    1819        self.old_Site_meta_installed = Site._meta.installed
     20        self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
     21        settings.TEMPLATE_DIRS = (
     22            os.path.join(os.path.dirname(__file__), 'templates'),
     23        )
    1924        # Create a user that will double as sitemap content
    2025        User.objects.create_user('testuser', 'test@example.com', 's3krit')
    2126
    2227    def tearDown(self):
    2328        settings.USE_L10N = self.old_USE_L10N
    2429        Site._meta.installed = self.old_Site_meta_installed
     30        settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
    2531
     32    def test_simple_sitemap_index(self):
     33        "A simple sitemap index can be rendered"
     34        # Retrieve the sitemap.
     35        response = self.client.get('/simple/index.xml')
     36        # Check for all the important bits:
     37        self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?>
     38<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     39<sitemap><loc>http://example.com/simple/sitemap-simple.xml</loc></sitemap>
     40</sitemapindex>
     41""")
     42   
     43    def test_simple_sitemap_custom_index(self):
     44        "A simple sitemap index can be rendered with a custom template"
     45        # Retrieve the sitemap.
     46        response = self.client.get('/simple/custom-index.xml')
     47        # Check for all the important bits:
     48        self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?>
     49<!-- This is a customised template -->
     50<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     51<sitemap><loc>http://example.com/simple/sitemap-simple.xml</loc></sitemap>
     52</sitemapindex>
     53""")
     54   
    2655    def test_simple_sitemap(self):
    2756        "A simple sitemap can be rendered"
    2857        # Retrieve the sitemap.
     
    3463</urlset>
    3564""" % date.today().strftime('%Y-%m-%d'))
    3665
     66    def test_simple_custom_sitemap(self):
     67        "A simple sitemap can be rendered with a custom template"
     68        # Retrieve the sitemap.
     69        response = self.client.get('/simple/custom-sitemap.xml')
     70        # Check for all the important bits:
     71        self.assertEquals(response.content, """<?xml version="1.0" encoding="UTF-8"?>
     72<!-- This is a customised template -->
     73<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     74<url><loc>http://example.com/location/</loc><lastmod>%s</lastmod><changefreq>never</changefreq><priority>0.5</priority></url>
     75</urlset>
     76""" % date.today().strftime('%Y-%m-%d'))
     77       
    3778    @skipUnless(settings.USE_I18N, "Internationalization is not enabled")
    3879    def test_localized_priority(self):
    3980        "The priority value should not be localized (Refs #14164)"
  • django/contrib/sitemaps/tests/urls.py

     
    2727}
    2828
    2929urlpatterns = patterns('django.contrib.sitemaps.views',
     30    (r'^simple/index\.xml$', 'index', {'sitemaps': simple_sitemaps}),
     31    (r'^simple/custom-index\.xml$', 'index', {'sitemaps': simple_sitemaps, 'template_name': u'custom_sitemap_index.xml'}),
     32    (r'^simple/sitemap-(?P<section>.+)\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
    3033    (r'^simple/sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps}),
     34    (r'^simple/custom-sitemap\.xml$', 'sitemap', {'sitemaps': simple_sitemaps, 'template_name': u'custom_sitemap.xml'}),
    3135    (r'^generic/sitemap\.xml$', 'sitemap', {'sitemaps': generic_sitemaps}),
    3236    (r'^flatpages/sitemap\.xml$', 'sitemap', {'sitemaps': flatpage_sitemaps}),
    3337)
  • django/contrib/sitemaps/tests/templates/custom_sitemap_index.xml

     
     1<?xml version="1.0" encoding="UTF-8"?>
     2<!-- This is a customised template -->
     3<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     4{% for location in sitemaps %}<sitemap><loc>{{ location }}</loc></sitemap>{% endfor %}
     5</sitemapindex>
  • django/contrib/sitemaps/tests/templates/custom_sitemap.xml

     
     1<?xml version="1.0" encoding="UTF-8"?>
     2<!-- This is a customised template -->
     3<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     4{% spaceless %}
     5{% for url in urlset %}
     6  <url>
     7    <loc>{{ url.location }}</loc>
     8    {% if url.lastmod %}<lastmod>{{ url.lastmod|date:"Y-m-d" }}</lastmod>{% endif %}
     9    {% if url.changefreq %}<changefreq>{{ url.changefreq }}</changefreq>{% endif %}
     10    {% if url.priority %}<priority>{{ url.priority }}</priority>{% endif %}
     11  </url>
     12{% endfor %}
     13{% endspaceless %}
     14</urlset>
  • django/contrib/sitemaps/views.py

     
    55from django.utils.encoding import smart_str
    66from django.core.paginator import EmptyPage, PageNotAnInteger
    77
    8 def index(request, sitemaps):
     8def index(request, sitemaps, template_name='sitemap_index.xml'):
    99    current_site = get_current_site(request)
    1010    sites = []
    1111    protocol = request.is_secure() and 'https' or 'http'
     
    2020        if pages > 1:
    2121            for page in range(2, pages+1):
    2222                sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page))
    23     xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
     23    xml = loader.render_to_string(template_name, {'sitemaps': sites})
    2424    return HttpResponse(xml, mimetype='application/xml')
    2525
    26 def sitemap(request, sitemaps, section=None):
     26def sitemap(request, sitemaps, template_name='sitemap.xml', section=None):
    2727    maps, urls = [], []
    2828    if section is not None:
    2929        if section not in sitemaps:
     
    4343            raise Http404("Page %s empty" % page)
    4444        except PageNotAnInteger:
    4545            raise Http404("No page '%s'" % page)
    46     xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls}))
     46    xml = smart_str(loader.render_to_string(template_name, {'urlset': urls}))
    4747    return HttpResponse(xml, mimetype='application/xml')
  • docs/ref/contrib/sitemaps.txt

     
    291291URLs. In this case, Django will automatically paginate the sitemap, and the
    292292index will reflect that.
    293293
     294Template customization
     295======================
     296
     297If you wish to use a different template for each sitemap or sitemap index available on your site,
     298you may specify it by passing a `template_name` parameter to the `sitemap` and `index` views via
     299the URLconf:
     300
     301    (r'^custom-sitemap\.xml$', 'django.contrib.sitemaps.views.index', {'sitemaps': sitemaps, 'template_name': u'custom_sitemap.html'}),
     302    (r'^custom-sitemap-(?P<section>.+)\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps, 'template_name': u'custom_sitemap.html'}),
     303
    294304Pinging Google
    295305==============
    296306
Back to Top