Ticket #7793: sitemaps-pagination.2.diff
File sitemaps-pagination.2.diff, 3.9 KB (added by , 16 years ago) |
---|
-
django/contrib/sitemaps/__init__.py
1 from django.core import urlresolvers 1 from django.core import urlresolvers, paginator 2 2 import urllib 3 3 4 4 PING_URL = "http://www.google.com/webmasters/tools/ping" … … 34 34 urllib.urlopen("%s?%s" % (ping_url, params)) 35 35 36 36 class Sitemap: 37 limit = 50000 38 37 39 def __get(self, name, obj, default=None): 38 40 try: 39 41 attr = getattr(self, name) … … 48 50 49 51 def location(self, obj): 50 52 return obj.get_absolute_url() 51 52 def get_urls(self): 53 54 def _get_paginator(self): 55 if not hasattr(self, "paginator"): 56 self.paginator = paginator.Paginator(self.items(), self.limit) 57 return self.paginator 58 paginator = property(_get_paginator) 59 60 def get_urls(self, page=1): 53 61 from django.contrib.sites.models import Site 54 62 current_site = Site.objects.get_current() 55 63 urls = [] 56 for item in self. items():64 for item in self.paginator.page(page).object_list: 57 65 loc = "http://%s%s" % (current_site.domain, self.__get('location', item)) 58 66 url_info = { 59 67 'location': loc, -
django/contrib/sitemaps/views.py
3 3 from django.contrib.sites.models import Site 4 4 from django.core import urlresolvers 5 5 from django.utils.encoding import smart_str 6 from django.core.paginator import EmptyPage, PageNotAnInteger 6 7 7 8 def index(request, sitemaps): 8 9 current_site = Site.objects.get_current() 9 10 sites = [] 10 11 protocol = request.is_secure() and 'https' or 'http' 11 for section in sitemaps.keys(): 12 for section, site in sitemaps.items(): 13 if callable(site): 14 pages = site().paginator.num_pages 15 else: 16 pages = site.paginator.num_pages 12 17 sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': section}) 13 18 sites.append('%s://%s%s' % (protocol, current_site.domain, sitemap_url)) 19 if pages > 1: 20 for page in range(2, pages): 21 sites.append('%s://%s%s?p=%s' % (protocol, current_site.domain, sitemap_url, page)) 14 22 xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites}) 15 23 return HttpResponse(xml, mimetype='application/xml') 16 24 … … 22 30 maps.append(sitemaps[section]) 23 31 else: 24 32 maps = sitemaps.values() 33 page = request.GET.get("p", 1) 25 34 for site in maps: 26 if callable(site): 27 urls.extend(site().get_urls()) 28 else: 29 urls.extend(site.get_urls()) 35 try: 36 if callable(site): 37 urls.extend(site().get_urls(page)) 38 else: 39 urls.extend(site.get_urls(page)) 40 except EmptyPage: 41 raise Http404("Page %s empty" % page) 42 except PageNotAnInteger: 43 raise Http404("No page '%s'" % page) 30 44 xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls})) 31 45 return HttpResponse(xml, mimetype='application/xml') -
docs/sitemaps.txt
282 282 both ``sitemap-flatpages.xml`` and ``sitemap-blog.xml``. The ``Sitemap`` 283 283 classes and the ``sitemaps`` dict don't change at all. 284 284 285 If one of your sitemaps is going to have more than 50,000 URLs you should 286 create an index file. Your sitemap will get paginated and the index will 287 reflect that. 288 285 289 Pinging Google 286 290 ============== 287 291