1 from django.contrib.sitemaps import Sitemap
2 from forum.models import Question
3 from django.conf import settings
4 from django.http import HttpResponse, Http404
5 from django.template import loader
6 from django.core import urlresolvers
7 from django.utils.encoding import smart_str
8 from django.core.paginator import EmptyPage, PageNotAnInteger
10 def index(request, sitemaps):
12 for section, site in sitemaps.items():
14 pages = site().paginator.num_pages
16 pages = site.paginator.num_pages
17 sitemap_url = urlresolvers.reverse('forum.sitemap.sitemap', kwargs={'section': section})
18 sites.append('%s%s' % (settings.APP_URL, sitemap_url))
20 for page in range(2, pages+1):
21 sites.append('%s%s?p=%s' % (settings.APP_URL, sitemap_url, page))
22 xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
23 return HttpResponse(xml, mimetype='application/xml')
25 def sitemap(request, sitemaps, section=None):
27 if section is not None:
28 if section not in sitemaps:
29 raise Http404("No sitemap available for section: %r" % section)
30 maps.append(sitemaps[section])
32 maps = sitemaps.values()
33 page = request.GET.get("p", 1)
38 urls.extend(site().get_urls(page=page))
40 urls.extend(site.get_urls(page=page))
42 raise Http404("Page %s empty" % page)
43 except PageNotAnInteger:
44 raise Http404("No page '%s'" % page)
45 xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls}))
46 return HttpResponse(xml, mimetype='application/xml')
48 class OsqaSitemap(Sitemap):
52 return Question.objects.filter_state(deleted=False)
54 def lastmod(self, obj):
55 return obj.last_activity_at
57 def location(self, obj):
58 return obj.get_absolute_url()
60 def __get(self, name, obj, default=None):
62 attr = getattr(self, name)
63 except AttributeError:
69 def get_urls(self, page=1):
71 for item in self.paginator.page(page).object_list:
72 loc = "%s%s" % (settings.APP_URL, self.__get('location', item))
75 'lastmod': self.__get('lastmod', item, None),
76 'changefreq': self.__get('changefreq', item, None),
77 'priority': self.__get('priority', item, None)