1 from django.utils.feedgenerator import Atom1Feed, Rss201rev2Feed
2 from django.conf.urls.defaults import url, patterns
3 from django.contrib.sites.models import Site
4 from django.core.urlresolvers import reverse
5 from django.http import HttpResponse
6 from philo.utils import paginate
9 class FeedMultiViewMixin(object):
11 This mixin provides common methods for adding feeds to multiviews. In order to use this mixin,
12 the multiview must define feed_title (probably as properties that return values
13 on related objects.) feed_description may also be defined; it defaults to an empty string.
18 rss_feed = Rss201rev2Feed
20 feed_description = None
23 def page_view(self, func, page):
25 Wraps an object-fetching function and renders the results as a page.
27 def inner(request, extra_context=None, **kwargs):
28 objects, extra_context = func(request=request, extra_context=extra_context, **kwargs)
30 context = self.get_context()
31 context.update(extra_context or {})
33 if 'page' in kwargs or 'page' in request.GET or (hasattr(self, 'per_page') and self.per_page):
34 page_num = kwargs.get('page', request.GET.get('page', 1))
35 paginator, paginated_page, objects = paginate(objects, self.per_page, page_num)
36 context.update({'paginator': paginator, 'paginated_page': paginated_page, self.list_var: objects})
38 context.update({self.list_var: objects})
40 return page.render_to_response(request, extra_context=context)
44 def feed_view(self, func, reverse_name):
46 Wraps an object-fetching function and renders the results as a rss or atom feed.
48 def inner(request, extra_context=None, **kwargs):
49 objects, extra_context = func(request=request, extra_context=extra_context, **kwargs)
51 if 'HTTP_ACCEPT' in request.META and 'rss' in request.META['HTTP_ACCEPT'] and 'atom' not in request.META['HTTP_ACCEPT']:
56 current_site = Site.objects.get_current()
57 #Could this be done with request.path instead somehow?
59 'link': 'http://%s/%s/%s/' % (current_site.domain, request.node.get_absolute_url().strip('/'), reverse(reverse_name, urlconf=self, kwargs=kwargs).strip('/'))
61 feed = self.get_feed(feed_type, extra_context, feed_kwargs)
65 'link': 'http://%s/%s/%s/' % (current_site.domain, request.node.get_absolute_url().strip('/'), self.get_subpath(obj).strip('/'))
67 self.add_item(feed, obj, kwargs=kwargs)
69 response = HttpResponse(mimetype=feed.mime_type)
70 feed.write(response, 'utf-8')
75 def get_feed(self, feed_type, extra_context, kwargs=None):
79 defaults.update(kwargs or {})
81 if feed_type == 'rss':
82 return self.rss_feed(**defaults)
84 if 'description' in defaults and defaults['description'] and 'subtitle' not in defaults:
85 defaults['subtitle'] = defaults['description']
87 return self.atom_feed(**defaults)
89 def feed_patterns(self, object_fetcher, page, base_name):
90 urlpatterns = patterns('',
91 url(r'^$', self.page_view(object_fetcher, page), name=base_name)
93 if self.feeds_enabled:
94 feed_name = '%s_feed' % base_name
95 urlpatterns = patterns('',
96 url(r'^%s/$' % self.feed_suffix, self.feed_view(object_fetcher, feed_name), name=feed_name),
100 def add_item(self, feed, obj, kwargs=None):
101 defaults = kwargs or {}
102 feed.add_item(**defaults)