Mango / feeds.py

from django.core.cache import cache
from django.http import HttpResponse, HttpResponseServerError
from django.utils import feedgenerator
from mango import utils
from mango.settings import *

def atom(request):
    content_type = 'application/atom+xml; charset=utf-8'
    cache_key = feed_url = BASE_URL + request.META['PATH_INFO']
    xml = cache.get(cache_key)
    if xml:
        return HttpResponse(xml, content_type=content_type)

    def required_kwargs(required, post=None):
        try:
            return dict([key, function()] for key, function in required.items())
        except KeyError:
            return False

    def all_kwargs(required, optional, post=None):
        kwargs = required_kwargs(required, post)
        if not kwargs:
            return False
        for key, function in optional.items():
            try:
                value = function()
                if value: # don't include pair if its value is empty
                    kwargs[key] = value
            except KeyError:
                pass
        return kwargs

    required = {
        'title':        lambda: unicode(SITE_TITLE, 'utf-8'),
        'link':         lambda: unicode(BASE_URL, 'utf-8'),
        'description':  lambda: u'', # required by constructor, but does not affect output
    }
    optional = {
        'author_name':  lambda: unicode(PRIMARY_AUTHOR_NAME, 'utf-8'),
        'author_email': lambda: unicode(PRIMARY_AUTHOR_EMAIL, 'utf-8'),
        'author_link':  lambda: unicode(PRIMARY_AUTHOR_URL, 'utf-8'),
        'feed_url':     lambda: feed_url,
        'feed_guid':    lambda: unicode(BASE_URL, 'utf-8'),
    }

    kwargs = all_kwargs(required, optional)

    if not kwargs:
        return HttpResponseServerError

    feed = feedgenerator.Atom1Feed(**kwargs)
    todo = FEED_MAX_POSTS
    all = todo == 0

    for post in utils.posts():
        if todo or all:
            required = {
                'title':       lambda: post['meta']['title'],
                'link':        lambda: post['canon_urls']['abs'],
                'description': lambda: post['html'],
            }
            optional = {
                'author_name': lambda: post['meta']['author'],
                'pubdate':     lambda: post['meta']['datetime'],
                'unique_id':   lambda: post['canon_urls']['abs'],
            }
            kwargs = all_kwargs(required, optional, post)
            if kwargs:
                feed.add_item(**kwargs)
                todo -= 1

    xml = feed.writeString('utf-8')
    cache.set(cache_key, xml, INDEX_CACHE_SECONDS)
    return HttpResponse(xml, content_type=content_type)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.