Winer

Winer provides the same API as django’s syndication Feed class, adapted to a Philo-style MultiView for easy database management. Apps that need syndication can simply subclass FeedView, override a few methods, and start serving RSS and Atom feeds. See BlogView for a concrete implementation example.

class philo.contrib.winer.models.FeedView(*args, **kwargs)

FeedView is an abstract model which handles a number of pages and related feeds for a single object such as a blog or newsletter. In addition to all other methods and attributes, FeedView supports the same generic API as django.contrib.syndication.views.Feed.

feed_type

The type of feed which should be served by the FeedView.

feed_suffix

The suffix which will be appended to a page URL for a feed_type feed of its items. Default: “feed”. Note that RSS and Atom feeds will always be available at <page_url>/rss and <page_url>/atom regardless of the value of this setting.

feeds_enabled

A BooleanField - whether or not feeds are enabled.

feed_length

A PositiveIntegerField - the maximum number of items to return for this feed. All items will be returned if this field is blank. Default: 15.

item_title_template

A ForeignKey to a Template which will be used to render the title of each item in the feed if provided.

item_description_template

A ForeignKey to a Template which will be used to render the description of each item in the feed if provided.

item_context_var

An attribute holding the name of the context variable to be populated with the items managed by the FeedView. Default: “items”

object_attr

An attribute holding the name of the attribute on a subclass of FeedView which will contain the main object of a feed (such as a Blog.) Default: “object”

Example:

class BlogView(FeedView):
    blog = models.ForeignKey(Blog)

    object_attr = 'blog'
    item_context_var = 'entries'
description

An attribute holding a description of the feeds served by the FeedView. This is a required part of the django.contrib.syndication.view.Feed API.

feed_patterns(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. In addition to base (which will serve the page at page_attr) and base + feed_suffix (which will serve a feed_type feed), patterns will be provided for each registered feed type as base + slug.

Parameters:
  • base – The base of the returned patterns - that is, the subpath pattern which will reference the page for the items. The feed_suffix will be appended to this subpath.
  • get_items_attr – A callable or the name of a callable on the FeedView which will return an (items, extra_context) tuple. This will be passed directly to feed_view() and page_view().
  • page_attr – A Page instance or the name of an attribute on the FeedView which contains a Page instance. This will be passed directly to page_view() and will be rendered with the items from get_items_attr.
  • reverse_name – The string which is considered the “name” of the view function returned by page_view() for the given parameters.
Returns:

Patterns suitable for use in urlpatterns.

Example:

class BlogView(FeedView):
    blog = models.ForeignKey(Blog)
    entry_archive_page = models.ForeignKey(Page)
    
    @property
    def urlpatterns(self):
        urlpatterns = self.feed_patterns(r'^', 'get_all_entries', 'index_page', 'index')
        urlpatterns += self.feed_patterns(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})', 'get_entries_by_ymd', 'entry_archive_page', 'entries_by_day')
        return urlpatterns
    
    def get_entries_by_ymd(request, year, month, day, extra_context=None):
        entries = Blog.entries.all()
        # filter entries based on the year, month, and day.
        return entries, extra_context

See also

get_feed_type()

get_object(request, **kwargs)

By default, returns the object stored in the attribute named by object_attr. This can be overridden for subclasses that publish different data for different URL parameters. It is part of the django.contrib.syndication.views.Feed API.

feed_view(get_items_attr, reverse_name, feed_type=None)

Returns a view function that renders a list of items as a feed.

Parameters:
  • get_items_attr – A callable or the name of a callable on the FeedView that will return a (items, extra_context) tuple when called with the object for the feed and view arguments.
  • reverse_name – The name which can be used reverse the page for this feed using the FeedView as the urlconf.
  • feed_type – The slug used to render the feed class which will be used by the returned view function.
Returns:

A view function that renders a list of items as a feed.

page_view(get_items_attr, page_attr)
Parameters:
  • get_items_attr – A callable or the name of a callable on the FeedView that will return a (items, extra_context) tuple when called with view arguments.
  • page_attr – A Page instance or the name of an attribute on the FeedView which contains a Page instance. This will be rendered with the items from get_items_attr.
Returns:

A view function that renders a list of items as an HttpResponse.

process_page_items(request, items)

Hook for handling any extra processing of items based on an HttpRequest, 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.

get_feed_type(request, feed_type=None)

If feed_type is not None, returns the corresponding class from the registry or raises HttpNotAcceptable.

Otherwise, intelligently chooses a feed type for a given request. Tries to return feed_type, but if the Accept header does not include that mimetype, tries to return the best match from the feed types that are offered by the FeedView. If none of the offered feed types are accepted by the HttpRequest, raises HttpNotAcceptable.

If mimeparse is installed, it will be used to select the best matching accepted format; otherwise, the first available format that is accepted will be selected.

get_feed(obj, request, reverse_name, feed_type=None, *args, **kwargs)

Returns an unpopulated django.utils.feedgenerator.DefaultFeed object for this object.

Parameters:
  • obj – The object for which the feed should be generated.
  • request – The current request.
  • reverse_name – The name which can be used to reverse the URL of the page corresponding to this feed.
  • feed_type – The slug used to register the feed class that will be instantiated and returned.
Returns:

An instance of the feed class registered as feed_type, falling back to feed_type if feed_type is None.

populate_feed(feed, items, request)

Populates a django.utils.feedgenerator.DefaultFeed instance as is returned by get_feed() with the passed-in items.

feed_extra_kwargs(obj)

Returns an extra keyword arguments dictionary that is used when initializing the feed generator.

item_extra_kwargs(item)

Returns an extra keyword arguments dictionary that is used with the add_item call of the feed generator.

exception philo.contrib.winer.exceptions.HttpNotAcceptable

This will be raised in FeedView.get_feed_type() if an Http-Accept header will not accept any of the feed content types that are available.

class philo.contrib.winer.middleware.HttpNotAcceptableMiddleware

Middleware to catch HttpNotAcceptable and return an HttpResponse with a 406 response code. See RFC 2616.

Project Versions

Previous topic

Waldo

Next topic

Contributing to Philo

This Page