seafile_ynh/sources/seafile-server-3.1.1/seahub/thirdpart/Djblets-0.6.14.dev-py2.6.egg/djblets/feedview/views.py
Elie 0f063400be Add sources
Former-commit-id: 2e47976ec5
2014-08-06 22:42:37 -04:00

47 lines
1.4 KiB
Python

import httplib
import urllib2
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.template.loader import render_to_string
from djblets.util.misc import cache_memoize
DEFAULT_EXPIRATION = 2 * 24 * 60 * 60 # 2 days
def view_feed(request, url, template_name="feedview/feed-page.html",
cache_expiration=DEFAULT_EXPIRATION, extra_context={}):
"""
Renders an RSS or Atom feed using the given template. This will use
a cached copy if available in order to reduce hits to the server.
"""
def fetch_feed():
from djblets.feedview import feedparser
data = urllib2.urlopen(url).read()
parser = feedparser.parse(data)
context = {
'parser': parser,
}
context.update(extra_context)
return render_to_string(template_name,
RequestContext(request, context))
try:
return HttpResponse(cache_memoize("feed-%s" % url, fetch_feed,
cache_expiration,
force_overwrite=request.GET.has_key("reload")))
except (urllib2.URLError, httplib.HTTPException), e:
context = {
'error': e,
}
context.update(extra_context)
return render_to_response(template_name,
RequestContext(request, context))