django.contrib.sitemaps: 80 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/django/contrib/sitemaps/__init__.py

Stats: 0 executed, 75 missed, 5 excluded, 40 ignored

  1. from django.contrib.sites.models import Site
  2. from django.core import urlresolvers, paginator
  3. from django.core.exceptions import ImproperlyConfigured
  4. import urllib
  5. PING_URL = "http://www.google.com/webmasters/tools/ping"
  6. class SitemapNotFound(Exception):
  7. pass
  8. def ping_google(sitemap_url=None, ping_url=PING_URL):
  9. """
  10. Alerts Google that the sitemap for the current site has been updated.
  11. If sitemap_url is provided, it should be an absolute path to the sitemap
  12. for this site -- e.g., '/sitemap.xml'. If sitemap_url is not provided, this
  13. function will attempt to deduce it by using urlresolvers.reverse().
  14. """
  15. if sitemap_url is None:
  16. try:
  17. # First, try to get the "index" sitemap URL.
  18. sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.index')
  19. except urlresolvers.NoReverseMatch:
  20. try:
  21. # Next, try for the "global" sitemap URL.
  22. sitemap_url = urlresolvers.reverse('django.contrib.sitemaps.views.sitemap')
  23. except urlresolvers.NoReverseMatch:
  24. pass
  25. if sitemap_url is None:
  26. raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.")
  27. from django.contrib.sites.models import Site
  28. current_site = Site.objects.get_current()
  29. url = "http://%s%s" % (current_site.domain, sitemap_url)
  30. params = urllib.urlencode({'sitemap':url})
  31. urllib.urlopen("%s?%s" % (ping_url, params))
  32. class Sitemap(object):
  33. # This limit is defined by Google. See the index documentation at
  34. # http://sitemaps.org/protocol.php#index.
  35. limit = 50000
  36. # If protocol is None, the URLs in the sitemap will use the protocol
  37. # with which the sitemap was requested.
  38. protocol = None
  39. def __get(self, name, obj, default=None):
  40. try:
  41. attr = getattr(self, name)
  42. except AttributeError:
  43. return default
  44. if callable(attr):
  45. return attr(obj)
  46. return attr
  47. def items(self):
  48. return []
  49. def location(self, obj):
  50. return obj.get_absolute_url()
  51. def _get_paginator(self):
  52. return paginator.Paginator(self.items(), self.limit)
  53. paginator = property(_get_paginator)
  54. def get_urls(self, page=1, site=None, protocol=None):
  55. # Determine protocol
  56. if self.protocol is not None:
  57. protocol = self.protocol
  58. if protocol is None:
  59. protocol = 'http'
  60. # Determine domain
  61. if site is None:
  62. if Site._meta.installed:
  63. try:
  64. site = Site.objects.get_current()
  65. except Site.DoesNotExist:
  66. pass
  67. if site is None:
  68. raise ImproperlyConfigured("To use sitemaps, either enable the sites framework or pass a Site/RequestSite object in your view.")
  69. domain = site.domain
  70. urls = []
  71. for item in self.paginator.page(page).object_list:
  72. loc = "%s://%s%s" % (protocol, domain, self.__get('location', item))
  73. priority = self.__get('priority', item, None)
  74. url_info = {
  75. 'item': item,
  76. 'location': loc,
  77. 'lastmod': self.__get('lastmod', item, None),
  78. 'changefreq': self.__get('changefreq', item, None),
  79. 'priority': str(priority is not None and priority or ''),
  80. }
  81. urls.append(url_info)
  82. return urls
  83. class FlatPageSitemap(Sitemap):
  84. def items(self):
  85. current_site = Site.objects.get_current()
  86. return current_site.flatpage_set.filter(registration_required=False)
  87. class GenericSitemap(Sitemap):
  88. priority = None
  89. changefreq = None
  90. def __init__(self, info_dict, priority=None, changefreq=None):
  91. self.queryset = info_dict['queryset']
  92. self.date_field = info_dict.get('date_field', None)
  93. self.priority = priority
  94. self.changefreq = changefreq
  95. def items(self):
  96. # Make sure to return a clone; we don't want premature evaluation.
  97. return self.queryset.filter()
  98. def lastmod(self, item):
  99. if self.date_field is not None:
  100. return getattr(item, self.date_field)
  101. return None