]> git.openstreetmap.org Git - osqa.git/blob - forum/sitemap.py
We created a sitemap index and a separate view forum.sitemap.index, not using the...
[osqa.git] / forum / sitemap.py
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
5 from django.template import loader
6 from django.core import urlresolvers
7
8 def index(request, sitemaps):
9     sites = []
10     for section, site in sitemaps.items():
11         if callable(site):
12             pages = site().paginator.num_pages
13         else:
14             pages = site.paginator.num_pages
15         sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': section})
16         sites.append('%s%s' % (settings.APP_URL, sitemap_url))
17         if pages > 1:
18             for page in range(2, pages+1):
19                 sites.append('%s%s?p=%s' % (settings.APP_URL, sitemap_url, page))
20     xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
21     return HttpResponse(xml, mimetype='application/xml')
22
23 class OsqaSitemap(Sitemap):
24     changefreq = 'daily'
25     priority = 0.5
26     def items(self):
27         return Question.objects.filter_state(deleted=False)
28
29     def lastmod(self, obj):
30         return obj.last_activity_at
31
32     def location(self, obj):
33         return obj.get_absolute_url()
34
35     def __get(self, name, obj, default=None):
36         try:
37             attr = getattr(self, name)
38         except AttributeError:
39             return default
40         if callable(attr):
41             return attr(obj)
42         return attr
43
44     def get_urls(self, page=1):
45         urls = []
46         for item in self.paginator.page(page).object_list:
47             loc = "%s%s" % (settings.APP_URL, self.__get('location', item))
48             url_info = {
49                 'location':   loc,
50                 'lastmod':    self.__get('lastmod', item, None),
51                 'changefreq': self.__get('changefreq', item, None),
52                 'priority':   self.__get('priority', item, None)
53             }
54             urls.append(url_info)
55         return urls