- Author:
- philgyford
- Posted:
- September 21, 2010
- Language:
- Python
- Version:
- 1.2
- Score:
- 2 (after 2 ratings)
This creates an RSS feed that has "content:encoded" elements for each item in the feed.
The "description" is best used for a brief summary of the entry, while the extra "content:encoded" element is designed for the entire contents of something.
This is the code I'm using for a weblog app. The main features you'd need to copy to add "content:encoded" elements to your own feed are:
- ExtendedRSSFeed() -- this is used to create a new kind of feed generator class that will know about these extra elements.
- feed_type = ExtendedRSSFeed -- we tell the feed class which feed generator class to use.
- item_extra_kwargs() -- we add the "content:encoded" element to each item. This populates the element by calling...
- item_content_encoded() -- this prepares the actual content. The name doesn't have to be in this format, but it seemed sensible to follow convention. The body of my weblog Entries are split into two parts and here it makes sure we add both parts, both of which contain HTML (which the syndication classes will encode appropriately.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Rss201rev2Feed
from django.shortcuts import get_object_or_404
from weblog.models import Blog,Entry
class ExtendedRSSFeed(Rss201rev2Feed):
"""
Create a type of RSS feed that has content:encoded elements.
"""
def root_attributes(self):
attrs = super(ExtendedRSSFeed, self).root_attributes()
attrs['xmlns:content'] = 'http://purl.org/rss/1.0/modules/content/'
return attrs
def add_item_elements(self, handler, item):
super(ExtendedRSSFeed, self).add_item_elements(handler, item)
handler.addQuickElement(u'content:encoded', item['content_encoded'])
class LatestEntriesFeed(Feed):
feed_type = ExtendedRSSFeed
def get_object(self, request, blog_slug):
return get_object_or_404(Blog, slug=blog_slug)
# Elements for the top-level, channel.
def title(self, obj):
return obj.name
def link(self, obj):
return obj.get_absolute_url()
def description(self, obj):
return obj.description
def items(self):
return Entry.live.all()[:5]
def item_extra_kwargs(self, item):
return {'content_encoded': self.item_content_encoded(item)}
# Elements for each item.
def item_title(self, item):
return item.title
def item_description(self, item):
return item.excerpt
def item_author_name(self, item):
if (item.author.get_full_name()):
return item.author.get_full_name()
else:
return item.author
def item_pubdate(self, item):
return item.published_date
def item_content_encoded(self, item):
content = item.body_html
if (item.body_more_html):
content += item.body_more_html
return content
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.