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.core.urlresolvers import get_script_prefix
11 from django.utils.encoding import smart_str
12 from django.core.paginator import EmptyPage, PageNotAnInteger
14 def index(request, sitemaps):
16 for section in sitemaps.keys():
17 sitemap_url = urlresolvers.reverse('sitemap_section_index', prefix='/', kwargs={'section': section})
19 # Replace double forward slashes with single ones
20 final_url = '%s%s' % (settings.APP_URL, sitemap_url)
21 final_url = re.sub("/+", "/", final_url)
22 final_url = final_url.replace('http:/', 'http://')
23 final_url = final_url.replace('https:/', 'https://')
25 sites.append(final_url)
27 xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
28 return HttpResponse(xml, content_type='application/xml')
30 def sitemap_section_index(request, section, sitemaps):
32 sitemap = sitemaps[section]()
34 raise Http404("Sitemap doesn't exist")
36 paginator = sitemap.paginator
40 for page in paginator.page_range:
41 location = urlresolvers.reverse('sitemap_section_page', prefix='/', kwargs={ 'page' : page, 'section' : section })
42 location = '%s%s' % (settings.APP_URL, location)
43 location = re.sub("/+", "/", location)
44 location = location.replace('http:/', 'http://')
45 location = location.replace('https:/', 'https://')
46 locations.append(location)
48 xml = loader.render_to_string('sitemap_section_index.xml', { 'locations' : locations, })
49 return HttpResponse(xml, content_type='application/xml')
51 def sitemap(request, sitemaps, section=None, page=1):
53 if section is not None:
54 if section not in sitemaps:
55 raise Http404("No sitemap available for section: %r" % section)
56 maps.append(sitemaps[section])
58 maps = sitemaps.values()
63 urls.extend(site().get_urls(page=page))
65 urls.extend(site.get_urls(page=page))
67 raise Http404("Page %s empty" % page)
68 except PageNotAnInteger:
69 raise Http404("No page '%s'" % page)
70 xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls}))
71 return HttpResponse(xml, content_type='application/xml')
73 class OsqaSitemap(Sitemap):
74 limit = QUESTIONS_SITEMAP_LIMIT
75 changefreq = QUESTIONS_SITEMAP_CHANGEFREQ
78 return Question.objects.filter_state(deleted=False).order_by('id')
80 def lastmod(self, obj):
81 return obj.last_activity_at
83 def location(self, obj):
84 return obj.get_absolute_url()
86 def __get(self, name, obj, default=None):
88 attr = getattr(self, name)
89 except AttributeError:
95 def get_urls(self, page=1):
97 for item in self.paginator.page(page).object_list:
98 root_relative_url = self.__get('location', item)
99 relative_url = root_relative_url[len(get_script_prefix()):]
100 loc = "%s/%s" % (settings.APP_URL, relative_url)
103 'lastmod': self.__get('lastmod', item, None),
104 'changefreq': self.__get('changefreq', item, None),
105 'priority': self.__get('priority', item, None)
107 urls.append(url_info)