news_feed.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright 2013 The Distro Tracker Developers
  2. # See the COPYRIGHT file at the top-level directory of this distribution and
  3. # at http://deb.li/DTAuthors
  4. #
  5. # This file is part of Distro Tracker. It is subject to the license terms
  6. # in the LICENSE file found in the top-level directory of this
  7. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  8. # including this file, may be copied, modified, propagated, or distributed
  9. # except according to the terms contained in the LICENSE file.
  10. """Implements the RSS news feed."""
  11. from __future__ import unicode_literals
  12. from django.conf import settings
  13. from django.http import Http404
  14. from django.contrib.syndication.views import Feed
  15. from distro_tracker.core.models import get_web_package
  16. from distro_tracker.core.models import News
  17. from distro_tracker.core.models import NewsRenderer
  18. from distro_tracker.core.models import ActionItem
  19. from itertools import chain
  20. class PackageNewsFeed(Feed):
  21. _DEFAULT_LIMIT = 30
  22. def get_object(self, request, package_name):
  23. package = get_web_package(package_name)
  24. if package is None:
  25. raise Http404
  26. return package
  27. def title(self, obj):
  28. return "{vendor} package news for {pkg}".format(
  29. vendor=settings.DISTRO_TRACKER_VENDOR_NAME,
  30. pkg=obj.name)
  31. def link(self, obj):
  32. return obj.get_absolute_url()
  33. def description(self, obj):
  34. return (
  35. "Latest developer's news for {vendor} source package {pkg}"
  36. .format(vendor=settings.DISTRO_TRACKER_VENDOR_NAME, pkg=obj.name)
  37. )
  38. def items(self, obj):
  39. item_limit = getattr(settings, 'DISTRO_TRACKER_RSS_ITEM_LIMIT',
  40. self._DEFAULT_LIMIT)
  41. news = obj.news_set.all()
  42. action_items = obj.action_items.all()
  43. def item_key(item):
  44. if isinstance(item, ActionItem):
  45. return item.last_updated_timestamp
  46. elif isinstance(item, News):
  47. return item.datetime_created
  48. all_items = chain(news, action_items)
  49. return sorted(all_items, key=item_key, reverse=True)[:item_limit]
  50. def item_title(self, item):
  51. if isinstance(item, News):
  52. return item.title
  53. elif isinstance(item, ActionItem):
  54. return item.short_description
  55. def item_description(self, item):
  56. if isinstance(item, News):
  57. renderer_class = NewsRenderer.get_renderer_for_content_type(
  58. item.content_type)
  59. if renderer_class is None:
  60. renderer_class = NewsRenderer.get_renderer_for_content_type(
  61. 'text/plain')
  62. renderer = renderer_class(item)
  63. return renderer.render_to_string()
  64. elif isinstance(item, ActionItem):
  65. return item.full_description
  66. def item_pubdate(self, item):
  67. if isinstance(item, ActionItem):
  68. return item.last_updated_timestamp
  69. elif isinstance(item, News):
  70. return item.datetime_created