Skip to content

Instantly share code, notes, and snippets.

@neara
Created July 28, 2013 08:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neara/6097894 to your computer and use it in GitHub Desktop.
Save neara/6097894 to your computer and use it in GitHub Desktop.
Django RSS feed example
from django.shortcuts import get_object_or_404
from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Rss201rev2Feed
from myproject.myapp.models import Model, Broadcast, \
BroadcastSocialAccountStatus
OFFICIAL_URL = "http://mainwebsite.com/"
OFFICIAL_LOGO_URL = 'https://s3.amazonaws.com/bucketname/logo.png'
class OfficialFeed(Rss201rev2Feed):
def add_root_elements(self, handler):
Rss201rev2Feed.add_root_elements(self, handler)
handler.startElement(u'image', {})
handler.addQuickElement(u"url", OFFICIAL_LOGO_URL)
handler.addQuickElement(u"title", u'MyProject')
handler.addQuickElement(u"link", OFFICIAL_URL)
handler.endElement(u"image")
class BSAFeed(Feed):
feed_type = OfficialFeed
def get_object(self, request, *args, **kwargs):
user_slug = request.META['PATH_INFO'].split('/')[2]
user = get_object_or_404(Model, slug=user_slug)
return user
def link(self, obj):
return '/user/{}/broadcasts/feed/'.format(obj.slug)
def title(self, obj):
return "%s Broadcasts Feed" % obj.name
def description(self, obj):
return "Updates on %s social media activity" % obj.name
def items(self, obj):
return BroadcastSocialAccount.objects.filter(broadcast__user=obj,
status=BroadcastSocialAccountStatus.SENT).order_by('-sent_at')
def item_title(self, item):
return "{} posted on {}".format(item.broadcast.user.name,
item.social_account.partner)
def item_description(self, item):
return item.broadcast.message
# item_link is only needed if NewsItem has no get_absolute_url method.
def item_link(self, item):
return item.get_broadcast_abs_url()
def item_pubdate(self, item):
return item.sent_at
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment