app/django/contrib/sitemaps/__init__.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
     1 from django.core import urlresolvers
     1 from django.core import urlresolvers, paginator
     2 import urllib
     2 import urllib
     3 
     3 
     4 PING_URL = "http://www.google.com/webmasters/tools/ping"
     4 PING_URL = "http://www.google.com/webmasters/tools/ping"
     5 
     5 
     6 class SitemapNotFound(Exception):
     6 class SitemapNotFound(Exception):
    31     current_site = Site.objects.get_current()
    31     current_site = Site.objects.get_current()
    32     url = "http://%s%s" % (current_site.domain, sitemap_url)
    32     url = "http://%s%s" % (current_site.domain, sitemap_url)
    33     params = urllib.urlencode({'sitemap':url})
    33     params = urllib.urlencode({'sitemap':url})
    34     urllib.urlopen("%s?%s" % (ping_url, params))
    34     urllib.urlopen("%s?%s" % (ping_url, params))
    35 
    35 
    36 class Sitemap:
    36 class Sitemap(object):
       
    37     # This limit is defined by Google. See the index documentation at
       
    38     # http://sitemaps.org/protocol.php#index.
       
    39     limit = 50000
       
    40 
    37     def __get(self, name, obj, default=None):
    41     def __get(self, name, obj, default=None):
    38         try:
    42         try:
    39             attr = getattr(self, name)
    43             attr = getattr(self, name)
    40         except AttributeError:
    44         except AttributeError:
    41             return default
    45             return default
    47         return []
    51         return []
    48 
    52 
    49     def location(self, obj):
    53     def location(self, obj):
    50         return obj.get_absolute_url()
    54         return obj.get_absolute_url()
    51 
    55 
    52     def get_urls(self):
    56     def _get_paginator(self):
       
    57         if not hasattr(self, "_paginator"):
       
    58             self._paginator = paginator.Paginator(self.items(), self.limit)
       
    59         return self._paginator
       
    60     paginator = property(_get_paginator)
       
    61 
       
    62     def get_urls(self, page=1):
    53         from django.contrib.sites.models import Site
    63         from django.contrib.sites.models import Site
    54         current_site = Site.objects.get_current()
    64         current_site = Site.objects.get_current()
    55         urls = []
    65         urls = []
    56         for item in self.items():
    66         for item in self.paginator.page(page).object_list:
    57             loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
    67             loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
    58             url_info = {
    68             url_info = {
    59                 'location':   loc,
    69                 'location':   loc,
    60                 'lastmod':    self.__get('lastmod', item, None),
    70                 'lastmod':    self.__get('lastmod', item, None),
    61                 'changefreq': self.__get('changefreq', item, None),
    71                 'changefreq': self.__get('changefreq', item, None),