Registered Tag as a value model.
[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.contrib.sites.models import Site
4 from django.core.urlresolvers import reverse
5 from django.http import HttpResponse
6 from philo.utils import paginate
7
8
9 class FeedMultiViewMixin(object):
10         """
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.
14         """
15         feed_suffix = 'feed'
16         feeds_enabled = True
17         atom_feed = Atom1Feed
18         rss_feed = Rss201rev2Feed
19         feed_title = None
20         feed_description = None
21         list_var = 'objects'
22         
23         def page_view(self, func, page):
24                 """
25                 Wraps an object-fetching function and renders the results as a page.
26                 """
27                 def inner(request, extra_context=None, **kwargs):
28                         objects, extra_context = func(request=request, extra_context=extra_context, **kwargs)
29
30                         context = self.get_context()
31                         context.update(extra_context or {})
32
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})
37                         else:
38                                 context.update({self.list_var: objects})
39
40                         return page.render_to_response(request, extra_context=context)
41
42                 return inner
43         
44         def feed_view(self, func, reverse_name):
45                 """
46                 Wraps an object-fetching function and renders the results as a rss or atom feed.
47                 """
48                 def inner(request, extra_context=None, **kwargs):
49                         objects, extra_context = func(request=request, extra_context=extra_context, **kwargs)
50         
51                         if 'HTTP_ACCEPT' in request.META and 'rss' in request.META['HTTP_ACCEPT'] and 'atom' not in request.META['HTTP_ACCEPT']:
52                                 feed_type = 'rss'
53                         else:
54                                 feed_type = 'atom'
55                         
56                         current_site = Site.objects.get_current()
57                         #Could this be done with request.path instead somehow?
58                         feed_kwargs = {
59                                 'link': 'http://%s/%s/%s/' % (current_site.domain, request.node.get_absolute_url().strip('/'), reverse(reverse_name, urlconf=self, kwargs=kwargs).strip('/'))
60                         }
61                         feed = self.get_feed(feed_type, extra_context, feed_kwargs)
62                         
63                         for obj in objects:
64                                 kwargs = {
65                                         'link': 'http://%s/%s/%s/' % (current_site.domain, request.node.get_absolute_url().strip('/'), self.get_subpath(obj).strip('/'))
66                                 }
67                                 self.add_item(feed, obj, kwargs=kwargs)
68         
69                         response = HttpResponse(mimetype=feed.mime_type)
70                         feed.write(response, 'utf-8')
71                         return response
72
73                 return inner
74         
75         def get_feed(self, feed_type, extra_context, kwargs=None):
76                 defaults = {
77                         'description': ''
78                 }
79                 defaults.update(kwargs or {})
80                 
81                 if feed_type == 'rss':
82                         return self.rss_feed(**defaults)
83                 
84                 if 'description' in defaults and defaults['description'] and 'subtitle' not in defaults:
85                         defaults['subtitle'] = defaults['description']
86                 
87                 return self.atom_feed(**defaults)
88         
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)
92                 )
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),
97                         ) + urlpatterns
98                 return urlpatterns
99         
100         def add_item(self, feed, obj, kwargs=None):
101                 defaults = kwargs or {}
102                 feed.add_item(**defaults)