Mango /

from django import http
from django.core.cache import cache
from django.utils import feedgenerator

from mango.main import Category
from mango.settings import *

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

    def required_kwargs(required, post=None):
            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():
                value = function()
                if value: # don't include pair if its value is empty
                    kwargs[key] = value
            except KeyError:
        return kwargs

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

    kwargs = all_kwargs(required, optional)

    if not kwargs:
        return http.HttpResponseServerError

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

    for post in Category.toplevel().descendants(pages=False):
        if (todo or all) and (tag is None or post.has_tag(tag)):
            required = {
                'title':       lambda: post.title,
                'link':        lambda: post.urls['canon']['abs'],
                'description': lambda: post.html,
            optional = {
                'author_name': lambda: post.meta['author'],
                'pubdate':     lambda: post.datetime,
                'unique_id':   lambda: post.urls['canon']['abs'],
            kwargs = all_kwargs(required, optional, post)
            if kwargs:
                todo -= 1

    xml = feed.writeString('utf-8')
    cache.set(cache_key, xml, INDEX_CACHE_SECONDS)
    return http.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
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.