|
0
|
1 |
from django.core import urlresolvers |
|
|
2 |
from django.contrib.sitemaps import Sitemap |
|
|
3 |
|
|
|
4 |
class 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): |
|
|
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) |
|
|
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 |
|