Source

mediatr / feeds.py

from atompub.atom import Feed
from django.contrib.sites.models import Site


from models import Stream

def full_url(location):
    return 'http://%s%s' % (Site.objects.get_current().domain, location)

class StreamFeed(Feed):
    def get_object(self, params):
        if len(params) != 1:
            raise LookupError
        return Stream.objects.get(slug=params[0])

    def feed_id(self, obj):
        return obj.get_absolute_url()

    def feed_title(self, obj):
        return obj.name
    
    def feed_subtitle(self, obj):
        return obj.description
    
    def feed_links(self, obj):
        return [{"rel": "alternate", "href": full_url(obj.get_absolute_url()) }]
    
    def feed_icon(self, obj):
        return obj.icon.url

    def feed_authors(self, obj):
        return [{"name": obj.author.get_full_name()}]
    
    def feed_rights(self, obj):
        return obj.copyright
    
    def items(self, obj):
        return obj.items.order_by('-publish_date')
        
    def item_id(self, item):
        return item.get_absolute_url()
    
    def item_title(self, item):
        return item.name
    
    def item_content(self, item):
        if item.is_image():
            return {"type": "html"}, u"<img src='%s' alt=''/><br/>\n%s" % (item.file.url, item.text)
        else:
            return {"type": "html"}, item.text
    
    def item_updated(self, item):
        return item.publish_date
    
    def item_links(self, item):
        return [{'rel': 'enclosure', 'href': item.file.url, 'length': item.file.size, 'type': item.mime.name},
                {'rel': 'alternate', 'href': full_url(item.get_absolute_url())}]