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.

views.py 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. from calendar import timegm
  2. from django.conf import settings
  3. from django.contrib.sites.shortcuts import get_current_site
  4. from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
  5. from django.http import Http404, HttpResponse
  6. from django.template import TemplateDoesNotExist, loader
  7. from django.utils import feedgenerator
  8. from django.utils.encoding import iri_to_uri
  9. from django.utils.html import escape
  10. from django.utils.http import http_date
  11. from django.utils.timezone import get_default_timezone, is_naive, make_aware
  12. def add_domain(domain, url, secure=False):
  13. protocol = 'https' if secure else 'http'
  14. if url.startswith('//'):
  15. # Support network-path reference (see #16753) - RSS requires a protocol
  16. url = '%s:%s' % (protocol, url)
  17. elif not url.startswith(('http://', 'https://', 'mailto:')):
  18. url = iri_to_uri('%s://%s%s' % (protocol, domain, url))
  19. return url
  20. class FeedDoesNotExist(ObjectDoesNotExist):
  21. pass
  22. class Feed:
  23. feed_type = feedgenerator.DefaultFeed
  24. title_template = None
  25. description_template = None
  26. def __call__(self, request, *args, **kwargs):
  27. try:
  28. obj = self.get_object(request, *args, **kwargs)
  29. except ObjectDoesNotExist:
  30. raise Http404('Feed object does not exist.')
  31. feedgen = self.get_feed(obj, request)
  32. response = HttpResponse(content_type=feedgen.content_type)
  33. if hasattr(self, 'item_pubdate') or hasattr(self, 'item_updateddate'):
  34. # if item_pubdate or item_updateddate is defined for the feed, set
  35. # header so as ConditionalGetMiddleware is able to send 304 NOT MODIFIED
  36. response['Last-Modified'] = http_date(
  37. timegm(feedgen.latest_post_date().utctimetuple()))
  38. feedgen.write(response, 'utf-8')
  39. return response
  40. def item_title(self, item):
  41. # Titles should be double escaped by default (see #6533)
  42. return escape(str(item))
  43. def item_description(self, item):
  44. return str(item)
  45. def item_link(self, item):
  46. try:
  47. return item.get_absolute_url()
  48. except AttributeError:
  49. raise ImproperlyConfigured(
  50. 'Give your %s class a get_absolute_url() method, or define an '
  51. 'item_link() method in your Feed class.' % item.__class__.__name__
  52. )
  53. def item_enclosures(self, item):
  54. enc_url = self._get_dynamic_attr('item_enclosure_url', item)
  55. if enc_url:
  56. enc = feedgenerator.Enclosure(
  57. url=str(enc_url),
  58. length=str(self._get_dynamic_attr('item_enclosure_length', item)),
  59. mime_type=str(self._get_dynamic_attr('item_enclosure_mime_type', item)),
  60. )
  61. return [enc]
  62. return []
  63. def _get_dynamic_attr(self, attname, obj, default=None):
  64. try:
  65. attr = getattr(self, attname)
  66. except AttributeError:
  67. return default
  68. if callable(attr):
  69. # Check co_argcount rather than try/excepting the function and
  70. # catching the TypeError, because something inside the function
  71. # may raise the TypeError. This technique is more accurate.
  72. try:
  73. code = attr.__code__
  74. except AttributeError:
  75. code = attr.__call__.__code__
  76. if code.co_argcount == 2: # one argument is 'self'
  77. return attr(obj)
  78. else:
  79. return attr()
  80. return attr
  81. def feed_extra_kwargs(self, obj):
  82. """
  83. Return an extra keyword arguments dictionary that is used when
  84. initializing the feed generator.
  85. """
  86. return {}
  87. def item_extra_kwargs(self, item):
  88. """
  89. Return an extra keyword arguments dictionary that is used with
  90. the `add_item` call of the feed generator.
  91. """
  92. return {}
  93. def get_object(self, request, *args, **kwargs):
  94. return None
  95. def get_context_data(self, **kwargs):
  96. """
  97. Return a dictionary to use as extra context if either
  98. ``self.description_template`` or ``self.item_template`` are used.
  99. Default implementation preserves the old behavior
  100. of using {'obj': item, 'site': current_site} as the context.
  101. """
  102. return {'obj': kwargs.get('item'), 'site': kwargs.get('site')}
  103. def get_feed(self, obj, request):
  104. """
  105. Return a feedgenerator.DefaultFeed object, fully populated, for
  106. this feed. Raise FeedDoesNotExist for invalid parameters.
  107. """
  108. current_site = get_current_site(request)
  109. link = self._get_dynamic_attr('link', obj)
  110. link = add_domain(current_site.domain, link, request.is_secure())
  111. feed = self.feed_type(
  112. title=self._get_dynamic_attr('title', obj),
  113. subtitle=self._get_dynamic_attr('subtitle', obj),
  114. link=link,
  115. description=self._get_dynamic_attr('description', obj),
  116. language=settings.LANGUAGE_CODE,
  117. feed_url=add_domain(
  118. current_site.domain,
  119. self._get_dynamic_attr('feed_url', obj) or request.path,
  120. request.is_secure(),
  121. ),
  122. author_name=self._get_dynamic_attr('author_name', obj),
  123. author_link=self._get_dynamic_attr('author_link', obj),
  124. author_email=self._get_dynamic_attr('author_email', obj),
  125. categories=self._get_dynamic_attr('categories', obj),
  126. feed_copyright=self._get_dynamic_attr('feed_copyright', obj),
  127. feed_guid=self._get_dynamic_attr('feed_guid', obj),
  128. ttl=self._get_dynamic_attr('ttl', obj),
  129. **self.feed_extra_kwargs(obj)
  130. )
  131. title_tmp = None
  132. if self.title_template is not None:
  133. try:
  134. title_tmp = loader.get_template(self.title_template)
  135. except TemplateDoesNotExist:
  136. pass
  137. description_tmp = None
  138. if self.description_template is not None:
  139. try:
  140. description_tmp = loader.get_template(self.description_template)
  141. except TemplateDoesNotExist:
  142. pass
  143. for item in self._get_dynamic_attr('items', obj):
  144. context = self.get_context_data(item=item, site=current_site,
  145. obj=obj, request=request)
  146. if title_tmp is not None:
  147. title = title_tmp.render(context, request)
  148. else:
  149. title = self._get_dynamic_attr('item_title', item)
  150. if description_tmp is not None:
  151. description = description_tmp.render(context, request)
  152. else:
  153. description = self._get_dynamic_attr('item_description', item)
  154. link = add_domain(
  155. current_site.domain,
  156. self._get_dynamic_attr('item_link', item),
  157. request.is_secure(),
  158. )
  159. enclosures = self._get_dynamic_attr('item_enclosures', item)
  160. author_name = self._get_dynamic_attr('item_author_name', item)
  161. if author_name is not None:
  162. author_email = self._get_dynamic_attr('item_author_email', item)
  163. author_link = self._get_dynamic_attr('item_author_link', item)
  164. else:
  165. author_email = author_link = None
  166. tz = get_default_timezone()
  167. pubdate = self._get_dynamic_attr('item_pubdate', item)
  168. if pubdate and is_naive(pubdate):
  169. pubdate = make_aware(pubdate, tz)
  170. updateddate = self._get_dynamic_attr('item_updateddate', item)
  171. if updateddate and is_naive(updateddate):
  172. updateddate = make_aware(updateddate, tz)
  173. feed.add_item(
  174. title=title,
  175. link=link,
  176. description=description,
  177. unique_id=self._get_dynamic_attr('item_guid', item, link),
  178. unique_id_is_permalink=self._get_dynamic_attr(
  179. 'item_guid_is_permalink', item),
  180. enclosures=enclosures,
  181. pubdate=pubdate,
  182. updateddate=updateddate,
  183. author_name=author_name,
  184. author_email=author_email,
  185. author_link=author_link,
  186. categories=self._get_dynamic_attr('item_categories', item),
  187. item_copyright=self._get_dynamic_attr('item_copyright', item),
  188. **self.item_extra_kwargs(item)
  189. )
  190. return feed