Ticket #7793: sitemaps-pagination.2.diff

File sitemaps-pagination.2.diff, 3.9 KB (added by Julian Bez, 16 years ago)

Updated: Limit is an attribute now

  • django/contrib/sitemaps/__init__.py

     
    1 from django.core import urlresolvers
     1from django.core import urlresolvers, paginator
    22import urllib
    33
    44PING_URL = "http://www.google.com/webmasters/tools/ping"
     
    3434    urllib.urlopen("%s?%s" % (ping_url, params))
    3535
    3636class Sitemap:
     37    limit = 50000
     38   
    3739    def __get(self, name, obj, default=None):
    3840        try:
    3941            attr = getattr(self, name)
     
    4850
    4951    def location(self, obj):
    5052        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):
    5361        from django.contrib.sites.models import Site
    5462        current_site = Site.objects.get_current()
    5563        urls = []
    56         for item in self.items():
     64        for item in self.paginator.page(page).object_list:
    5765            loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
    5866            url_info = {
    5967                'location':   loc,
  • django/contrib/sitemaps/views.py

     
    33from django.contrib.sites.models import Site
    44from django.core import urlresolvers
    55from django.utils.encoding import smart_str
     6from django.core.paginator import EmptyPage, PageNotAnInteger
    67
    78def index(request, sitemaps):
    89    current_site = Site.objects.get_current()
    910    sites = []
    1011    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
    1217        sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': section})
    1318        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))
    1422    xml = loader.render_to_string('sitemap_index.xml', {'sitemaps': sites})
    1523    return HttpResponse(xml, mimetype='application/xml')
    1624
     
    2230        maps.append(sitemaps[section])
    2331    else:
    2432        maps = sitemaps.values()
     33    page = request.GET.get("p", 1)
    2534    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)
    3044    xml = smart_str(loader.render_to_string('sitemap.xml', {'urlset': urls}))
    3145    return HttpResponse(xml, mimetype='application/xml')
  • docs/sitemaps.txt

     
    282282both ``sitemap-flatpages.xml`` and ``sitemap-blog.xml``. The ``Sitemap``
    283283classes and the ``sitemaps`` dict don't change at all.
    284284
     285If one of your sitemaps is going to have more than 50,000 URLs you should
     286create an index file. Your sitemap will get paginated and the index will
     287reflect that.
     288
    285289Pinging Google
    286290==============
    287291
Back to Top