| 1 |
from django.http import HttpResponse, Http404 |
|---|
| 2 |
from django.template import loader |
|---|
| 3 |
from django.contrib.sites.models import Site |
|---|
| 4 |
from django.core import urlresolvers |
|---|
| 5 |
from django.utils.encoding import smart_str |
|---|
| 6 |
|
|---|
| 7 |
def index(request, sitemaps): |
|---|
| 8 |
current_site = Site.objects.get_current() |
|---|
| 9 |
sites = [] |
|---|
| 10 |
protocol = request.is_secure() and 'https' or 'http' |
|---|
| 11 |
for section in sitemaps.keys(): |
|---|
| 12 |
sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': section}) |
|---|
| 13 |
sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url)) |
|---|
| 14 |
xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) |
|---|
| 15 |
return HttpResponse(xml, mimetype='application/xml') |
|---|
| 16 |
|
|---|
| 17 |
def sitemap(request, sitemaps, section=None): |
|---|
| 18 |
maps, urls = [], [] |
|---|
| 19 |
if section is not None: |
|---|
| 20 |
if section not in sitemaps: |
|---|
| 21 |
raise Http404("No sitemap available for section: %r" % section) |
|---|
| 22 |
maps.append(sitemaps[section]) |
|---|
| 23 |
else: |
|---|
| 24 |
maps = sitemaps.values() |
|---|
| 25 |
for site in maps: |
|---|
| 26 |
if callable(site): |
|---|
| 27 |
urls.extend(site().get_urls()) |
|---|
| 28 |
else: |
|---|
| 29 |
urls.extend(site.get_urls()) |
|---|
| 30 |
xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls})) |
|---|
| 31 |
return HttpResponse(xml, mimetype='application/xml') |
|---|