PageRenderTime 19ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/sitemaps/georss.py

https://code.google.com/p/mango-py/
Python | 53 lines | 34 code | 4 blank | 15 comment | 4 complexity | f3314c6b9e852edb8faeb930e95e83c8 MD5 | raw file
 1from django.core import urlresolvers
 2from django.contrib.sitemaps import Sitemap
 3
 4class GeoRSSSitemap(Sitemap):
 5    """
 6    A minimal hook to produce sitemaps for GeoRSS feeds.
 7    """
 8    def __init__(self, feed_dict, slug_dict=None):
 9        """
10        This sitemap object initializes on a feed dictionary (as would be passed
11        to `django.contrib.syndication.views.feed`) and a slug dictionary.  
12        If the slug dictionary is not defined, then it's assumed the keys provide
13        the URL parameter to the feed.  However, if you have a complex feed (e.g.,
14        you override `get_object`, then you'll need to provide a slug dictionary.
15        The slug dictionary should have the same keys as the feed dictionary, but 
16        each value in the slug dictionary should be a sequence of slugs that may 
17        be used for valid feeds.  For example, let's say we have a feed that 
18        returns objects for a specific ZIP code in our feed dictionary:
19
20            feed_dict = {'zipcode' : ZipFeed}
21
22        Then we would use a slug dictionary with a list of the zip code slugs
23        corresponding to feeds you want listed in the sitemap:
24
25            slug_dict = {'zipcode' : ['77002', '77054']}
26        """
27        # Setting up.
28        self.feed_dict = feed_dict
29        self.locations = []
30        if slug_dict is None: slug_dict = {}
31        # Getting the feed locations.
32        for section in feed_dict.keys():
33            if slug_dict.get(section, False):
34                for slug in slug_dict[section]:
35                    self.locations.append('%s/%s' % (section, slug))
36            else:
37                self.locations.append(section)
38 
39    def get_urls(self, page=1, site=None):
40        """
41        This method is overrridden so the appropriate `geo_format` attribute
42        is placed on each URL element.
43        """
44        urls = Sitemap.get_urls(self, page=page, site=site)
45        for url in urls: url['geo_format'] = 'georss'
46        return urls
47
48    def items(self):
49        return self.locations
50
51    def location(self, obj):
52        return urlresolvers.reverse('django.contrib.syndication.views.feed', args=(obj,))
53