georss.py 2.1 KB

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