3 from django.contrib.sitemaps import Sitemap
4 from forum.models import Question
5 from forum.settings import QUESTIONS_SITEMAP_LIMIT, QUESTIONS_SITEMAP_CHANGEFREQ
6 from django.conf import settings
7 from django.http import HttpResponse, Http404
8 from django.template import loader
9 from django.core import urlresolvers
10 from django.utils.encoding import smart_str
11 from django.core.paginator import EmptyPage, PageNotAnInteger
13 def index(request, sitemaps):
15 for section, site in sitemaps.items():
17 pages = site().paginator.num_pages
19 pages = site.paginator.num_pages
20 sitemap_url = urlresolvers.reverse('sitemap_section_index', kwargs={'section': section})
22 # Replace double forward slashes with single ones
23 final_url = '%s%s' % (settings.APP_URL, sitemap_url)
24 final_url = re.sub("/+", "/", final_url)
25 final_url = final_url.replace('http:/', 'http://')
26 final_url = final_url.replace('https:/', 'https://')
28 sites.append(final_url)
30 xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
31 return HttpResponse(xml, mimetype='application/xml')
33 def sitemap_section_index(request, section, sitemaps):
35 sitemap = sitemaps[section]()
37 raise Http404("Sitemap doesn't exist")
39 paginator = sitemap.paginator
43 for page in paginator.page_range:
44 location = urlresolvers.reverse('sitemap_section_page', kwargs={ 'page' : page, 'section' : section })
45 location = '%s%s' % (settings.APP_URL, location)
46 location = re.sub("/+", "/", location)
47 location = location.replace('http:/', 'http://')
48 location = location.replace('https:/', 'https://')
49 locations.append(location)
51 xml = loader.render_to_string('sitemap_section_index.xml', { 'locations' : locations, })
52 return HttpResponse(xml, mimetype='application/xml')
54 def sitemap(request, sitemaps, section=None, page=1):
56 if section is not None:
57 if section not in sitemaps:
58 raise Http404("No sitemap available for section: %r" % section)
59 maps.append(sitemaps[section])
61 maps = sitemaps.values()
66 urls.extend(site().get_urls(page=page))
68 urls.extend(site.get_urls(page=page))
70 raise Http404("Page %s empty" % page)
71 except PageNotAnInteger:
72 raise Http404("No page '%s'" % page)
73 xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls}))
74 return HttpResponse(xml, mimetype='application/xml')
76 class OsqaSitemap(Sitemap):
77 limit = QUESTIONS_SITEMAP_LIMIT
78 changefreq = QUESTIONS_SITEMAP_CHANGEFREQ
81 return Question.objects.filter_state(deleted=False).order_by('id')
83 def lastmod(self, obj):
84 return obj.last_activity_at
86 def location(self, obj):
87 return obj.get_absolute_url()
89 def __get(self, name, obj, default=None):
91 attr = getattr(self, name)
92 except AttributeError:
98 def get_urls(self, page=1):
100 for item in self.paginator.page(page).object_list:
101 loc = "%s%s" % (settings.APP_URL, self.__get('location', item))
104 'lastmod': self.__get('lastmod', item, None),
105 'changefreq': self.__get('changefreq', item, None),
106 'priority': self.__get('priority', item, None)
108 urls.append(url_info)