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 from django.contrib.sites.models import get_current_site
8 from django.utils.encoding import smart_str
9 from django.core.paginator import EmptyPage, PageNotAnInteger
11 def index(request, sitemaps):
13 for section, site in sitemaps.items():
15 pages = site().paginator.num_pages
17 pages = site.paginator.num_pages
18 sitemap_url = urlresolvers.reverse('forum.sitemap.sitemap', kwargs={'section': section})
19 sites.append('%s%s' % (settings.APP_URL, sitemap_url))
21 for page in range(2, pages+1):
22 sites.append('%s%s?p=%s' % (settings.APP_URL, sitemap_url, page))
23 xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
24 return HttpResponse(xml, mimetype='application/xml')
26 def sitemap(request, sitemaps, section=None):
28 if section is not None:
29 if section not in sitemaps:
30 raise Http404("No sitemap available for section: %r" % section)
31 maps.append(sitemaps[section])
33 maps = sitemaps.values()
34 page = request.GET.get("p", 1)
39 urls.extend(site().get_urls(page=page))
41 urls.extend(site.get_urls(page=page))
43 raise Http404("Page %s empty" % page)
44 except PageNotAnInteger:
45 raise Http404("No page '%s'" % page)
46 xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls}))
47 return HttpResponse(xml, mimetype='application/xml')
49 class OsqaSitemap(Sitemap):
53 return Question.objects.filter_state(deleted=False)
55 def lastmod(self, obj):
56 return obj.last_activity_at
58 def location(self, obj):
59 return obj.get_absolute_url()
61 def __get(self, name, obj, default=None):
63 attr = getattr(self, name)
64 except AttributeError:
70 def get_urls(self, page=1):
72 for item in self.paginator.page(page).object_list:
73 loc = "%s%s" % (settings.APP_URL, self.__get('location', item))
76 'lastmod': self.__get('lastmod', item, None),
77 'changefreq': self.__get('changefreq', item, None),
78 'priority': self.__get('priority', item, None)