Fixed #2158 -- Added title_template and description_template hooks to Feed class in syndication framework.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3214 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-06-26 23:19:03 +00:00
parent 6dc719312d
commit e923ed2a83
2 changed files with 23 additions and 7 deletions

View File

@ -16,10 +16,14 @@ class Feed(object):
item_pubdate = None item_pubdate = None
item_enclosure_url = None item_enclosure_url = None
feed_type = feedgenerator.DefaultFeed feed_type = feedgenerator.DefaultFeed
title_template = None
description_template = None
def __init__(self, slug, feed_url): def __init__(self, slug, feed_url):
self.slug = slug self.slug = slug
self.feed_url = feed_url self.feed_url = feed_url
self.title_template_name = self.title_template or ('feeds/%s_title.html' % slug)
self.description_template_name = self.description_template or ('feeds/%s_description.html' % slug)
def item_link(self, item): def item_link(self, item):
try: try:
@ -77,13 +81,13 @@ class Feed(object):
) )
try: try:
title_template = loader.get_template('feeds/%s_title.html' % self.slug) title_tmp = loader.get_template(self.title_template_name)
except TemplateDoesNotExist: except TemplateDoesNotExist:
title_template = Template('{{ obj }}') title_tmp = Template('{{ obj }}')
try: try:
description_template = loader.get_template('feeds/%s_description.html' % self.slug) description_tmp = loader.get_template(self.description_template_name)
except TemplateDoesNotExist: except TemplateDoesNotExist:
description_template = Template('{{ obj }}') description_tmp = Template('{{ obj }}')
for item in self.__get_dynamic_attr('items', obj): for item in self.__get_dynamic_attr('items', obj):
link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item)) link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item))
@ -102,9 +106,9 @@ class Feed(object):
else: else:
author_email = author_link = None author_email = author_link = None
feed.add_item( feed.add_item(
title = title_template.render(Context({'obj': item, 'site': current_site})).decode('utf-8'), title = title_tmp.render(Context({'obj': item, 'site': current_site})).decode('utf-8'),
link = link, link = link,
description = description_template.render(Context({'obj': item, 'site': current_site})).decode('utf-8'), description = description_tmp.render(Context({'obj': item, 'site': current_site})).decode('utf-8'),
unique_id = link, unique_id = link,
enclosure = enc, enclosure = enc,
pubdate = self.__get_dynamic_attr('item_pubdate', item), pubdate = self.__get_dynamic_attr('item_pubdate', item),

View File

@ -134,7 +134,9 @@ put into those elements.
If you don't create a template for either the title or description, the If you don't create a template for either the title or description, the
framework will use the template ``"{{ obj }}"`` by default -- that is, framework will use the template ``"{{ obj }}"`` by default -- that is,
the normal string representation of the object. the normal string representation of the object. You can also change the
names of these two templates by specifying ``title_template`` and
``description_template`` as attributes of your ``Feed`` class.
* To specify the contents of ``<link>``, you have two options. For each * To specify the contents of ``<link>``, you have two options. For each
item in ``items()``, Django first tries executing a item in ``items()``, Django first tries executing a
``get_absolute_url()`` method on that object. If that method doesn't ``get_absolute_url()`` method on that object. If that method doesn't
@ -342,6 +344,16 @@ This example illustrates all possible attributes and methods for a ``Feed`` clas
feed_type = feedgenerator.Rss201rev2Feed feed_type = feedgenerator.Rss201rev2Feed
# TEMPLATE NAMES -- Optional. These should be strings representing
# names of Django templates that the system should use in rendering the
# title and description of your feed items. Both are optional.
# If you don't specify one, or either, Django will use the template
# 'feeds/SLUG_title.html' and 'feeds/SLUG_description.html', where SLUG
# is the slug you specify in the URL.
title_template = None
description_template = None
# TITLE -- One of the following three is required. The framework looks # TITLE -- One of the following three is required. The framework looks
# for them in this order. # for them in this order.