Code

Ticket #3624: feeds.2.diff

File feeds.2.diff, 4.7 KB (added by Rob Hudson <treborhudson@…>, 7 years ago)

Cleaned up, tested override and default, and added docs.

Line 
1Index: django/contrib/syndication/feeds.py
2===================================================================
3--- django/contrib/syndication/feeds.py (revision 5427)
4+++ django/contrib/syndication/feeds.py (working copy)
5@@ -85,11 +85,11 @@
6         try:
7             title_tmp = loader.get_template(self.title_template_name)
8         except TemplateDoesNotExist:
9-            title_tmp = Template('{{ obj }}')
10+            title_tmp = self.__get_dynamic_attr('item_title', obj, Template('{{ obj }}'))
11         try:
12             description_tmp = loader.get_template(self.description_template_name)
13         except TemplateDoesNotExist:
14-            description_tmp = Template('{{ obj }}')
15+            description_tmp = self.__get_dynamic_attr('item_description', obj, Template('{{ obj }}')))
16 
17         for item in self.__get_dynamic_attr('items', obj):
18             link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item))
19Index: docs/syndication_feeds.txt
20===================================================================
21--- docs/syndication_feeds.txt  (revision 5427)
22+++ docs/syndication_feeds.txt  (working copy)
23@@ -93,6 +93,7 @@
24 latest five news items::
25 
26     from django.contrib.syndication.feeds import Feed
27+    from django.template import Template
28     from chicagocrime.models import NewsItem
29 
30     class LatestEntries(Feed):
31@@ -103,6 +104,9 @@
32         def items(self):
33             return NewsItem.objects.order_by('-pub_date')[:5]
34 
35+        def item_description(self):
36+            return Template('{{ obj.body }}')
37+
38 Note:
39 
40     * The class subclasses ``django.contrib.syndication.feeds.Feed``.
41@@ -114,6 +118,9 @@
42       `object-relational mapper`_, ``items()`` doesn't have to return model
43       instances. Although you get a few bits of functionality "for free" by
44       using Django models, ``items()`` can return any type of object you want.
45+    * ``item_description(self)`` is method that returns a ``Template`` object.
46+      This method of defining a ``Template`` can be used if you prefer this to
47+      creating template files located on the file system.
48     * If you're creating an Atom feed, rather than an RSS feed, set the
49       ``subtitle`` attribute instead of the ``description`` attribute. See
50       `Publishing Atom and RSS feeds in tandem`_, later, for an example.
51@@ -123,11 +130,13 @@
52 put into those elements.
53 
54     * To specify the contents of ``<title>`` and ``<description>``, create
55-      `Django templates`_ called ``feeds/latest_title.html`` and
56-      ``feeds/latest_description.html``, where ``latest`` is the ``slug``
57-      specified in the URLconf for the given feed. Note the ``.html`` extension
58-      is required. The RSS system renders that template for each item, passing
59-      it two template context variables:
60+      `Django templates`_.  This can be done by either creating template files
61+      called ``feeds/latest_title.html`` and ``feeds/latest_description.html``,
62+      where ``latest`` is the ``slug`` specified in the URLconf for the given
63+      feed, or by defining the properties ``item_title`` and ``item_description``
64+      in your subclass.  Note the ``.html`` extension is required when using
65+      template files. The RSS system renders that template for each item,
66+      passing it two template context variables:
67 
68           * ``{{ obj }}`` -- The current object (one of whichever objects you
69             returned in ``items()``).
70@@ -540,6 +549,16 @@
71             django.core.exceptions.ObjectDoesNotExist on error.
72             """
73 
74+        # ITEM TITLE -- If the template defined in title_template or the default
75+        # template file 'feeds/SLUG_title.html' does not exist, the framework
76+        # looks for this method.  If this method is not defined,
77+        # Template('{{ obj }}') is used by default.
78+
79+        def item_title(self):
80+            """
81+            Returns a Template object defining how to render the title.
82+            """
83+
84         # ITEM LINK -- One of these three is required. The framework looks for
85         # them in this order.
86 
87@@ -556,6 +575,16 @@
88             Returns the URL for every item in the feed.
89             """
90 
91+        # ITEM DESCRIPTION -- If the template defined in description_template
92+        # or the default template file 'feeds/SLUG_description.html' does not
93+        # exist, the framework looks for this method.  If this method is not
94+        # defined, Template('{{ obj }}') is used by default.
95+
96+        def item_description(self):
97+            """
98+            Returns a Template object defining how to render the description.
99+            """
100+
101         # ITEM AUTHOR NAME --One of the following three is optional. The
102         # framework looks for them in this order.
103