Skip to content

Commit

Permalink
Deduplicate h/feeds/renderers.py
Browse files Browse the repository at this point in the history
  • Loading branch information
seanh committed Sep 7, 2015
1 parent b2467d6 commit 36a57eb
Showing 1 changed file with 31 additions and 109 deletions.
140 changes: 31 additions & 109 deletions h/feeds/renderers.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
# -*- coding: utf-8 -*-
"""Pyramid renderers for constructing RSS and Atom feeds from annotations."""

from pyramid import renderers

from h.feeds import atom
from h.feeds import rss


def render_annotations_to_atom(
request, annotations, atom_url, html_url=None, title=None,
subtitle=None):
"""Return an Atom feed of the given list of annotations.
:param annotations: A list of annotations from the API
:type annotations: list of dicts
:param atom_url: The URL where this Atom feed will be hosted
(the feed will contain a link to this URL)
:type atom_url: unicode
:param html_url: The URL to the HTML page that this Atom feed is a feed
for (the feed will contain a link to this URL).
:type html_url: unicode
:param title: The title of the feed
:type title: unicode
def _render(request, feed_from_annotations_func, annotations, html_url, title,
template, **kwargs):
"""Render the given arguments to an Atom or RSS unicode string."""

:param subtitle: The subtitle of the feed
:type subtitle: unicode
:returns: An Atom feed as an XML string
:rtype: unicode
"""
def annotation_url(annotation):
"""Return the HTML permalink URL for the given annotation."""
return request.resource_url(request.root, "a", annotation["id"])
Expand All @@ -40,83 +20,28 @@ def annotation_api_url(annotation):
return request.resource_url(request.root, "api", "annotations",
annotation["id"])

feed = atom.feed_from_annotations(
annotations=annotations, atom_url=atom_url,
annotation_url=annotation_url, annotation_api_url=annotation_api_url,
html_url=html_url, title=title, subtitle=subtitle)

return renderers.render(
'h:templates/atom.xml.jinja2', {"feed": feed}, request=request)


def render_annotations_to_rss(
request, annotations, title, html_url, description):
"""Return an RSS feed of the given list of annotations.
:param annotations: A list of annotation dicts from the Hypothesis API,
these annotations will become the <item>s in the RSS feed.
:type annotations: list of dicts
:param title: The name of the RSS channel, will go into the <title> element
in the RSS feed. This should be the same as the <title> of the HTML
page given in the html_url argument.
:type title: unicode
:param html_url: The URL to the HTML page that this RSS feed is a feed
for (the feed will contain a link to this URL).
:type html_url: unicode
:param description: A phrase of sentence describing the RSS channel.
:type description: unicode
:returns: An RSS feed as an XML string
:rtype: unicode
"""
def annotation_url(annotation):
"""Return the HTML permalink URL for the given annotation."""
return request.resource_url(request.root, "a", annotation["id"])

def annotation_api_url(annotation):
"""Return the JSON API URL for the given annotation."""
return request.resource_url(request.root, "api", "annotations",
annotation["id"])

feed = rss.feed_from_annotations(
feed = feed_from_annotations_func(
annotations=annotations, annotation_url=annotation_url,
annotation_api_url=annotation_api_url, html_url=html_url, title=title,
description=description)
**kwargs)

return renderers.render(
'h:templates/rss.xml.jinja2', {"feed": feed}, request=request)
return renderers.render(template, {"feed": feed}, request=request)


class AnnotationsAtomRendererFactory(object):

"""A Pyramid renderer that renders lists of annotations as Atom feeds.
"""A Pyramid renderer for constructing an Atom feed from annotations.
Usage:
@view_config(renderer='annotations_atom')
def my_view(request):
...
@view_config(..., renderer='annotations_atom')
def my_atom_view_callable(request):
return dict(
# A list of annotation dicts, these will be used as the Atom
# feed entries.
annotations=annotations,
# The URL where the Atom feed will be hosted.
atom_url="http://hypothes.is/stream.atom",
# The URL for the HTML page the feed is a feed of (optional).
html_url=html_url,
# The title of the feed (optional).
title=title,
# The subtitle for the feed (optional).
subtitle=subtitle)
annotations=list_of_annotation_dicts,
atom_url=url_where_this_atom_feed_is_served,
html_url=url_of_html_page_that_this_atom_feed_corresponds_to,
title=title_of_this_atom_feed,
subtitle=subtitle_of_this_atom_feed)
"""

Expand All @@ -125,31 +50,25 @@ def __init__(self, info):

def __call__(self, value, system):
system["request"].response.content_type = "application/atom+xml"
return render_annotations_to_atom(request=system["request"], **value)
return _render(request=system["request"],
feed_from_annotations_func=atom.feed_from_annotations,
template='h:templates/atom.xml.jinja2',
**value)


class AnnotationsRSSRendererFactory(object):

"""A Pyramid renderer that renders lists of annotations as RSS feeds.
"""A Pyramid renderer for constructing an RSS feed from annotations.
Usage:
@view_config(renderer='annotations_rss')
def my_view(request):
...
@view_config(..., renderer='annotations_rss')
def my_atom_view_callable(request):
return dict(
# A list of annotation dicts, these will be used as the RSS
# feed items.
annotations=annotations,
# The URL for the HTML page that the feed is a feed of.
html_url=html_url,
# The title of the feed.
title=title,
# A description of the feed.
description=description,
annotations=list_of_annotation_dicts,
html_url=url_of_html_page_that_this_rss_feed_corresponds_to,
title=title_of_this_rss_feed,
description=description_of_this_rss_feed)
"""

Expand All @@ -158,7 +77,10 @@ def __init__(self, info):

def __call__(self, value, system):
system["request"].response.content_type = "application/rss+xml"
return render_annotations_to_rss(request=system["request"], **value)
return _render(request=system["request"],
feed_from_annotations_func=rss.feed_from_annotations,
template='h:templates/rss.xml.jinja2',
**value)


def includeme(config):
Expand Down

0 comments on commit 36a57eb

Please sign in to comment.