1 from django.http import HttpResponse, Http404 |
1 from django.http import HttpResponse, Http404 |
2 from django.template import loader |
2 from django.template import loader |
3 from django.contrib.sites.models import Site |
3 from django.contrib.sites.models import Site |
4 from django.core import urlresolvers |
4 from django.core import urlresolvers |
5 from django.utils.encoding import smart_str |
5 from django.utils.encoding import smart_str |
|
6 from django.core.paginator import EmptyPage, PageNotAnInteger |
6 |
7 |
7 def index(request, sitemaps): |
8 def index(request, sitemaps): |
8 current_site = Site.objects.get_current() |
9 current_site = Site.objects.get_current() |
9 sites = [] |
10 sites = [] |
10 protocol = request.is_secure() and 'https' or 'http' |
11 protocol = request.is_secure() and 'https' or 'http' |
11 for section in sitemaps.keys(): |
12 for section, site in sitemaps.items(): |
|
13 if callable(site): |
|
14 pages = site().paginator.num_pages |
|
15 else: |
|
16 pages = site.paginator.num_pages |
12 sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': section}) |
17 sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': section}) |
13 sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url)) |
18 sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url)) |
|
19 if pages > 1: |
|
20 for page in range(2, pages+1): |
|
21 sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page)) |
14 xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) |
22 xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) |
15 return HttpResponse(xml, mimetype='application/xml') |
23 return HttpResponse(xml, mimetype='application/xml') |
16 |
24 |
17 def sitemap(request, sitemaps, section=None): |
25 def sitemap(request, sitemaps, section=None): |
18 maps, urls = [], [] |
26 maps, urls = [], [] |
20 if section not in sitemaps: |
28 if section not in sitemaps: |
21 raise Http404("No sitemap available for section: %r" % section) |
29 raise Http404("No sitemap available for section: %r" % section) |
22 maps.append(sitemaps[section]) |
30 maps.append(sitemaps[section]) |
23 else: |
31 else: |
24 maps = sitemaps.values() |
32 maps = sitemaps.values() |
|
33 page = request.GET.get("p", 1) |
25 for site in maps: |
34 for site in maps: |
26 if callable(site): |
35 try: |
27 urls.extend(site().get_urls()) |
36 if callable(site): |
28 else: |
37 urls.extend(site().get_urls(page)) |
29 urls.extend(site.get_urls()) |
38 else: |
|
39 urls.extend(site.get_urls(page)) |
|
40 except EmptyPage: |
|
41 raise Http404("Page %s empty" % page) |
|
42 except PageNotAnInteger: |
|
43 raise Http404("No page '%s'" % page) |
30 xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls})) |
44 xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls})) |
31 return HttpResponse(xml, mimetype='application/xml') |
45 return HttpResponse(xml, mimetype='application/xml') |