Minor cleanup to penfield.utils after get_absolute_url refactor.
[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.http import HttpResponse
4 from philo.utils import paginate
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_title = None
18         feed_description = None
19         list_var = 'objects'
20         
21         def page_view(self, func, page):
22                 """
23                 Wraps an object-fetching function and renders the results as a page.
24                 """
25                 def inner(request, extra_context=None, **kwargs):
26                         objects, extra_context = func(request=request, extra_context=extra_context, **kwargs)
27
28                         context = self.get_context()
29                         context.update(extra_context or {})
30
31                         if 'page' in kwargs or 'page' in request.GET or (hasattr(self, 'per_page') and self.per_page):
32                                 page_num = kwargs.get('page', request.GET.get('page', 1))
33                                 paginator, paginated_page, objects = paginate(objects, self.per_page, page_num)
34                                 context.update({'paginator': paginator, 'paginated_page': paginated_page, self.list_var: objects})
35                         else:
36                                 context.update({self.list_var: objects})
37
38                         return page.render_to_response(request, extra_context=context)
39
40                 return inner
41         
42         def feed_view(self, func, reverse_name):
43                 """
44                 Wraps an object-fetching function and renders the results as a rss or atom feed.
45                 """
46                 def inner(request, extra_context=None, **kwargs):
47                         objects, extra_context = func(request=request, extra_context=extra_context, **kwargs)
48         
49                         if 'HTTP_ACCEPT' in request.META and 'rss' in request.META['HTTP_ACCEPT'] and 'atom' not in request.META['HTTP_ACCEPT']:
50                                 feed_type = 'rss'
51                         else:
52                                 feed_type = 'atom'
53                         
54                         feed_kwargs = {
55                                 'link': request.node.construct_url(subpath=self.reverse(reverse_name, kwargs=kwargs), request=request, with_domain=True)
56                         }
57                         feed = self.get_feed(feed_type, extra_context, feed_kwargs)
58                         
59                         for obj in objects:
60                                 kwargs = {
61                                         'link': request.node.construct_url(subpath=self.reverse(obj=obj), request=request, with_domain=True)
62                                 }
63                                 self.add_item(feed, obj, kwargs=kwargs)
64         
65                         response = HttpResponse(mimetype=feed.mime_type)
66                         feed.write(response, 'utf-8')
67                         return response
68
69                 return inner
70         
71         def get_feed(self, feed_type, extra_context, kwargs=None):
72                 defaults = {
73                         'description': ''
74                 }
75                 defaults.update(kwargs or {})
76                 
77                 if feed_type == 'rss':
78                         return self.rss_feed(**defaults)
79                 
80                 if 'description' in defaults and defaults['description'] and 'subtitle' not in defaults:
81                         defaults['subtitle'] = defaults['description']
82                 
83                 return self.atom_feed(**defaults)
84         
85         def feed_patterns(self, object_fetcher, page, base_name):
86                 urlpatterns = patterns('',
87                         url(r'^$', self.page_view(object_fetcher, page), name=base_name)
88                 )
89                 if self.feeds_enabled:
90                         feed_name = '%s_feed' % base_name
91                         urlpatterns = patterns('',
92                                 url(r'^%s$' % self.feed_suffix, self.feed_view(object_fetcher, feed_name), name=feed_name),
93                         ) + urlpatterns
94                 return urlpatterns
95         
96         def add_item(self, feed, obj, kwargs=None):
97                 defaults = kwargs or {}
98                 feed.add_item(**defaults)