app/django/contrib/sitemaps/__init__.py
changeset 54 03e267d67478
child 323 ff1a9aa48cfd
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 from django.core import urlresolvers
       
     2 import urllib
       
     3 
       
     4 PING_URL = "http://www.google.com/webmasters/tools/ping"
       
     5 
       
     6 class SitemapNotFound(Exception):
       
     7     pass
       
     8 
       
     9 def ping_google(sitemap_url=None, ping_url=PING_URL):
       
    10     """
       
    11     Alerts Google that the sitemap for the current site has been updated.
       
    12     If sitemap_url is provided, it should be an absolute path to the sitemap
       
    13     for this site -- e.g., '/sitemap.xml'. If sitemap_url is not provided, this
       
    14     function will attempt to deduce it by using urlresolvers.reverse().
       
    15     """
       
    16     if sitemap_url is None:
       
    17         try:
       
    18             # First, try to get the "index" sitemap URL.
       
    19             sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.index')
       
    20         except urlresolvers.NoReverseMatch:
       
    21             try:
       
    22                 # Next, try for the "global" sitemap URL.
       
    23                 sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap')
       
    24             except urlresolvers.NoReverseMatch:
       
    25                 pass
       
    26 
       
    27     if sitemap_url is None:
       
    28         raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.")
       
    29 
       
    30     from django.contrib.sites.models import Site
       
    31     current_site = Site.objects.get_current()
       
    32     url = "http://%s%s" % (current_site.domain, sitemap_url)
       
    33     params = urllib.urlencode({'sitemap':url})
       
    34     urllib.urlopen("%s?%s" % (ping_url, params))
       
    35 
       
    36 class Sitemap:
       
    37     def __get(self, name, obj, default=None):
       
    38         try:
       
    39             attr = getattr(self, name)
       
    40         except AttributeError:
       
    41             return default
       
    42         if callable(attr):
       
    43             return attr(obj)
       
    44         return attr
       
    45 
       
    46     def items(self):
       
    47         return []
       
    48 
       
    49     def location(self, obj):
       
    50         return obj.get_absolute_url()
       
    51 
       
    52     def get_urls(self):
       
    53         from django.contrib.sites.models import Site
       
    54         current_site = Site.objects.get_current()
       
    55         urls = []
       
    56         for item in self.items():
       
    57             loc = "http://%s%s" % (current_site.domain, self.__get('location', item))
       
    58             url_info = {
       
    59                 'location':   loc,
       
    60                 'lastmod':    self.__get('lastmod', item, None),
       
    61                 'changefreq': self.__get('changefreq', item, None),
       
    62                 'priority':   self.__get('priority', item, None)
       
    63             }
       
    64             urls.append(url_info)
       
    65         return urls
       
    66 
       
    67 class FlatPageSitemap(Sitemap):
       
    68     def items(self):
       
    69         from django.contrib.sites.models import Site
       
    70         current_site = Site.objects.get_current()
       
    71         return current_site.flatpage_set.all()
       
    72 
       
    73 class GenericSitemap(Sitemap):
       
    74     priority = None
       
    75     changefreq = None
       
    76 
       
    77     def __init__(self, info_dict, priority=None, changefreq=None):
       
    78         self.queryset = info_dict['queryset']
       
    79         self.date_field = info_dict.get('date_field', None)
       
    80         self.priority = priority
       
    81         self.changefreq = changefreq
       
    82 
       
    83     def items(self):
       
    84         # Make sure to return a clone; we don't want premature evaluation.
       
    85         return self.queryset.filter()
       
    86 
       
    87     def lastmod(self, item):
       
    88         if self.date_field is not None:
       
    89             return getattr(item, self.date_field)
       
    90         return None