fromdjangoimporthttpfromdjango.core.cacheimportcachefromdjango.utilsimportfeedgeneratorimportmango.settingsfrommango.decoratorsimportbaseurlfrommango.mainimportIndex@baseurldefatom(request,tag=None):content_type='application/atom+xml; charset=utf-8'feed_url=request.build_absolute_uri()cache_key='mango:%s'%feed_urlxml=cache.get(cache_key)ifxml:returnhttp.HttpResponse(xml,content_type=content_type)defrequired_kwargs(required,post=None):try:returndict([key,function()]forkey,functioninrequired.items())exceptKeyError:returnFalsedefall_kwargs(required,optional,post=None):kwargs=required_kwargs(required,post)ifnotkwargs:returnFalseforkey,functioninoptional.items():try:value=function()ifvalue:# don't include pair if its value is emptykwargs[key]=valueexceptKeyError:passreturnkwargsrequired={'title':lambda:mango.settings.SITE_TITLE,'link':lambda:baseurl(),'description':lambda:u'',# required by constructor, but does not affect output}optional={'author_name':lambda:mango.settings.PRIMARY_AUTHOR_NAME,'author_email':lambda:mango.settings.PRIMARY_AUTHOR_EMAIL,'author_link':lambda:mango.settings.PRIMARY_AUTHOR_URL,'feed_url':lambda:feed_url,'feed_guid':lambda:baseurl(),}kwargs=all_kwargs(required,optional)ifnotkwargs:returnhttp.HttpResponseServerErrorfeed=feedgenerator.Atom1Feed(**kwargs)todo=mango.settings.FEED_MAX_POSTSall=todo==0forpostinIndex.get().descendants():if(todoorall)and(tagisNoneorpost.has_tag(tag)):required={'title':lambda:post.title_text,'link':lambda:post.permalink(),'description':lambda:post.html,}optional={'author_name':lambda:post.meta['author'],'pubdate':lambda:post.datetime,'unique_id':lambda:post.permalink(),}kwargs=all_kwargs(required,optional,post)ifkwargs:feed.add_item(**kwargs)todo-=1xml=feed.writeString('utf-8')cache.set(cache_key,xml,mango.settings.INDEX_CACHE_SECONDS)returnhttp.HttpResponse(xml,content_type=content_type)