-
-ATOM = feedgenerator.Atom1Feed.mime_type
-RSS = feedgenerator.Rss201rev2Feed.mime_type
-FEEDS = SortedDict([
- (ATOM, feedgenerator.Atom1Feed),
- (RSS, feedgenerator.Rss201rev2Feed),
-])
-FEED_CHOICES = (
- (ATOM, "Atom"),
- (RSS, "RSS"),
-)
-
-
-class FeedView(MultiView):
- """
- The FeedView expects to handle a number of different feeds for the
- same object - i.e. patterns for a blog to handle all entries or
- just entries for a certain year/month/day.
-
- This class would subclass django.contrib.syndication.views.Feed, but
- that would make it callable, which causes problems.
- """
- feed_type = models.CharField(max_length=50, choices=FEED_CHOICES, default=ATOM)
- feed_suffix = models.CharField(max_length=255, blank=False, default="feed")
- feeds_enabled = models.BooleanField(default=True)
- feed_length = models.PositiveIntegerField(blank=True, null=True, default=15, help_text="The maximum number of items to return for this feed. All items will be returned if this field is blank.")
-
- item_title_template = models.ForeignKey(Template, blank=True, null=True, related_name="%(app_label)s_%(class)s_title_related")
- item_description_template = models.ForeignKey(Template, blank=True, null=True, related_name="%(app_label)s_%(class)s_description_related")
-
- item_context_var = 'items'
- object_attr = 'object'
-
- description = ""
-
- def feed_patterns(self, base, get_items_attr, page_attr, reverse_name):
- """
- Given the name to be used to reverse this view and the names of
- the attributes for the function that fetches the objects, returns
- patterns suitable for inclusion in urlpatterns.
- """
- urlpatterns = patterns('')
- if self.feeds_enabled:
- feed_reverse_name = "%s_feed" % reverse_name
- feed_view = http_not_acceptable(self.feed_view(get_items_attr, feed_reverse_name))
- feed_pattern = r'%s%s%s$' % (base, (base and base[-1] != "^") and "/" or "", self.feed_suffix)
- urlpatterns += patterns('',
- url(feed_pattern, feed_view, name=feed_reverse_name),
- )
- urlpatterns += patterns('',
- url(r"%s$" % base, self.page_view(get_items_attr, page_attr), name=reverse_name)
- )
- return urlpatterns
-
- def get_object(self, request, **kwargs):
- return getattr(self, self.object_attr)
-
- def feed_view(self, get_items_attr, reverse_name):
- """
- Returns a view function that renders a list of items as a feed.
- """
- get_items = callable(get_items_attr) and get_items_attr or getattr(self, get_items_attr)
-
- def inner(request, extra_context=None, *args, **kwargs):
- obj = self.get_object(request, *args, **kwargs)
- feed = self.get_feed(obj, request, reverse_name)
- items, xxx = get_items(request, extra_context=extra_context, *args, **kwargs)
- self.populate_feed(feed, items, request)
-
- response = HttpResponse(mimetype=feed.mime_type)
- feed.write(response, 'utf-8')
- return response
-
- return inner
-
- def page_view(self, get_items_attr, page_attr):
- """
- Returns a view function that renders a list of items as a page.
- """
- get_items = callable(get_items_attr) and get_items_attr or getattr(self, get_items_attr)
- page = isinstance(page_attr, Page) and page_attr or getattr(self, page_attr)
-
- def inner(request, extra_context=None, *args, **kwargs):
- items, extra_context = get_items(request, extra_context=extra_context, *args, **kwargs)
- items, item_context = self.process_page_items(request, items)
-
- context = self.get_context()
- context.update(extra_context or {})
- context.update(item_context or {})
-
- return page.render_to_response(request, extra_context=context)
- return inner
-
- def process_page_items(self, request, items):
- """
- Hook for handling any extra processing of items based on a
- request, such as pagination or searching. This method is
- expected to return a list of items and a dictionary to be
- added to the page context.
- """
- item_context = {
- self.item_context_var: items
- }
- return items, item_context