Merge branch 'gilbert' of git://github.com/lapilofu/philo into gilbert
[philo.git] / contrib / penfield / utils.py
1 from django.utils.feedgenerator import Atom1Feed, Rss201rev2Feed
2 from django.conf.urls.defaults import url, patterns
3 from django.core.urlresolvers import reverse
4 from django.http import HttpResponse
5
6
7 class FeedMultiViewMixin(object):
8         """
9         This mixin provides common methods for adding feeds to multiviews. In order to use this mixin,
10         the multiview must define feed_title (probably as properties that return values
11         on related objects.) feed_description may also be defined; it defaults to an empty string.
12         """
13         feed_suffix = 'feed'
14         feeds_enabled = True
15         atom_feed = Atom1Feed
16         rss_feed = Rss201rev2Feed
17         feed_description = ''
18         
19         def page_view(self, func, page, list_var='entries'):
20                 """
21                 Wraps an object-fetching function and renders the results as a page.
22                 """
23                 def inner(request, node=None, extra_context=None, **kwargs):
24                         objects, extra_context = func(request=request, node=node, extra_context=extra_context, **kwargs)
25
26                         context = self.get_context()
27                         context.update(extra_context or {})
28
29                         if 'page' in kwargs or 'page' in request.GET:
30                                 page_num = kwargs.get('page', request.GET.get('page', 1))
31                                 paginator, paginated_page, objects = paginate(objects, self.per_page, page_num)
32                                 context.update({'paginator': paginator, 'paginated_page': paginated_page, list_var: objects})
33                         else:
34                                 context.update({list_var: objects})
35
36                         return page.render_to_response(node, request, extra_context=context)
37
38                 return inner
39         
40         def feed_view(self, func, reverse_name):
41                 """
42                 Wraps an object-fetching function and renders the results as a rss or atom feed.
43                 """
44                 def inner(request, node=None, extra_context=None, **kwargs):
45                         objects, extra_context = func(request=request, node=node, extra_context=extra_context, **kwargs)
46         
47                         if 'HTTP_ACCEPT' in request.META and 'rss' in request.META['HTTP_ACCEPT'] and 'atom' not in request.META['HTTP_ACCEPT']:
48                                 feed_type = 'rss'
49                         else:
50                                 feed_type = 'atom'
51                         
52                         feed = self.get_feed(feed_type, request, node, kwargs, reverse_name)
53                         
54                         for obj in objects:
55                                 feed.add_item(obj.title, '/%s/%s/' % (node.get_absolute_url().strip('/'), self.get_subpath(obj).strip('/')), description=self.get_obj_description(obj))
56         
57                         response = HttpResponse(mimetype=feed.mime_type)
58                         feed.write(response, 'utf-8')
59                         return response
60
61                 return inner
62         
63         def get_feed(self, feed_type, request, node, kwargs, reverse_name):
64                 title = self.feed_title
65                 link = '/%s/%s/' % (node.get_absolute_url().strip('/'), reverse(reverse_name, urlconf=self, kwargs=kwargs).strip('/'))
66                 description = self.feed_description
67                 if feed_type == 'rss':
68                         return self.rss_feed(title, link, description)
69                 
70                 return self.atom_feed(title, link, description, subtitle=description)
71         
72         def feed_patterns(self, object_fetcher, page, base_name):
73                 feed_name = '%s_feed' % base_name
74                 urlpatterns = patterns('',
75                         url(r'^%s/$' % self.feed_suffix, self.feed_view(object_fetcher, feed_name), name=feed_name),
76                         url(r'^$', self.page_view(object_fetcher, page), name=base_name)
77                 )
78                 return urlpatterns
79         
80         def get_obj_description(self, obj):
81                 raise NotImplementedError