Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from urllib.parse import urlencode
  2. from urllib.request import urlopen
  3. from django.apps import apps as django_apps
  4. from django.conf import settings
  5. from django.core import paginator
  6. from django.core.exceptions import ImproperlyConfigured
  7. from django.urls import NoReverseMatch, reverse
  8. from django.utils import translation
  9. PING_URL = "https://www.google.com/webmasters/tools/ping"
  10. class SitemapNotFound(Exception):
  11. pass
  12. def ping_google(sitemap_url=None, ping_url=PING_URL):
  13. """
  14. Alert Google that the sitemap for the current site has been updated.
  15. If sitemap_url is provided, it should be an absolute path to the sitemap
  16. for this site -- e.g., '/sitemap.xml'. If sitemap_url is not provided, this
  17. function will attempt to deduce it by using urls.reverse().
  18. """
  19. sitemap_full_url = _get_sitemap_full_url(sitemap_url)
  20. params = urlencode({'sitemap': sitemap_full_url})
  21. urlopen('%s?%s' % (ping_url, params))
  22. def _get_sitemap_full_url(sitemap_url):
  23. if not django_apps.is_installed('django.contrib.sites'):
  24. raise ImproperlyConfigured("ping_google requires django.contrib.sites, which isn't installed.")
  25. if sitemap_url is None:
  26. try:
  27. # First, try to get the "index" sitemap URL.
  28. sitemap_url = reverse('django.contrib.sitemaps.views.index')
  29. except NoReverseMatch:
  30. try:
  31. # Next, try for the "global" sitemap URL.
  32. sitemap_url = reverse('django.contrib.sitemaps.views.sitemap')
  33. except NoReverseMatch:
  34. pass
  35. if sitemap_url is None:
  36. raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.")
  37. Site = django_apps.get_model('sites.Site')
  38. current_site = Site.objects.get_current()
  39. return 'http://%s%s' % (current_site.domain, sitemap_url)
  40. class Sitemap:
  41. # This limit is defined by Google. See the index documentation at
  42. # http://www.sitemaps.org/protocol.html#index.
  43. limit = 50000
  44. # If protocol is None, the URLs in the sitemap will use the protocol
  45. # with which the sitemap was requested.
  46. protocol = None
  47. def __get(self, name, obj, default=None):
  48. try:
  49. attr = getattr(self, name)
  50. except AttributeError:
  51. return default
  52. if callable(attr):
  53. return attr(obj)
  54. return attr
  55. def items(self):
  56. return []
  57. def location(self, obj):
  58. return obj.get_absolute_url()
  59. @property
  60. def paginator(self):
  61. return paginator.Paginator(self.items(), self.limit)
  62. def get_urls(self, page=1, site=None, protocol=None):
  63. # Determine protocol
  64. if self.protocol is not None:
  65. protocol = self.protocol
  66. if protocol is None:
  67. protocol = 'http'
  68. # Determine domain
  69. if site is None:
  70. if django_apps.is_installed('django.contrib.sites'):
  71. Site = django_apps.get_model('sites.Site')
  72. try:
  73. site = Site.objects.get_current()
  74. except Site.DoesNotExist:
  75. pass
  76. if site is None:
  77. raise ImproperlyConfigured(
  78. "To use sitemaps, either enable the sites framework or pass "
  79. "a Site/RequestSite object in your view."
  80. )
  81. domain = site.domain
  82. if getattr(self, 'i18n', False):
  83. urls = []
  84. current_lang_code = translation.get_language()
  85. for lang_code, lang_name in settings.LANGUAGES:
  86. translation.activate(lang_code)
  87. urls += self._urls(page, protocol, domain)
  88. translation.activate(current_lang_code)
  89. else:
  90. urls = self._urls(page, protocol, domain)
  91. return urls
  92. def _urls(self, page, protocol, domain):
  93. urls = []
  94. latest_lastmod = None
  95. all_items_lastmod = True # track if all items have a lastmod
  96. for item in self.paginator.page(page).object_list:
  97. loc = "%s://%s%s" % (protocol, domain, self.__get('location', item))
  98. priority = self.__get('priority', item)
  99. lastmod = self.__get('lastmod', item)
  100. if all_items_lastmod:
  101. all_items_lastmod = lastmod is not None
  102. if (all_items_lastmod and
  103. (latest_lastmod is None or lastmod > latest_lastmod)):
  104. latest_lastmod = lastmod
  105. url_info = {
  106. 'item': item,
  107. 'location': loc,
  108. 'lastmod': lastmod,
  109. 'changefreq': self.__get('changefreq', item),
  110. 'priority': str(priority if priority is not None else ''),
  111. }
  112. urls.append(url_info)
  113. if all_items_lastmod and latest_lastmod:
  114. self.latest_lastmod = latest_lastmod
  115. return urls
  116. class GenericSitemap(Sitemap):
  117. priority = None
  118. changefreq = None
  119. def __init__(self, info_dict, priority=None, changefreq=None, protocol=None):
  120. self.queryset = info_dict['queryset']
  121. self.date_field = info_dict.get('date_field')
  122. self.priority = priority
  123. self.changefreq = changefreq
  124. self.protocol = protocol
  125. def items(self):
  126. # Make sure to return a clone; we don't want premature evaluation.
  127. return self.queryset.filter()
  128. def lastmod(self, item):
  129. if self.date_field is not None:
  130. return getattr(item, self.date_field)
  131. return None
  132. default_app_config = 'django.contrib.sitemaps.apps.SiteMapsConfig'