Skip to content

Commit

Permalink
Add Atom feeds.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjuxax committed Jan 16, 2012
1 parent 1233213 commit dedc2ac
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ sort.hot_recalc_threshold = seconds = 1
recent_comments.num = 10
recent_comments.enabled = true

atom.enabled = true

[pipeline:main]
pipeline =
egg:WebError#evalerror
Expand Down
2 changes: 2 additions & 0 deletions raggregate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def main(global_config, **settings):
config.add_route('user_info', '/user_info')
config.add_route('buttons', '/buttons')
config.add_route('favicon', '/favicon.ico')
config.add_route('atom_story', '/atom_story.xml')
config.add_route('atom_comment', '/atom_comment.xml')

config.add_subscriber(subscribers.ban, NewResponse)
config.add_subscriber(subscribers.user_session_handler, BeforeRender)
Expand Down
19 changes: 19 additions & 0 deletions raggregate/templates/base/atom_base.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
% if 'atom.enabled' not in request.registry.settings or request.registry.settings['atom.enabled'] != 'true':
<% return %>
% endif
<?xml version="1.0" encoding="utf-8"?>

<feed xmlns="http://www.w3.org/2005/Atom">

<%block name="feed_metadata">
<title>default</title>
<subtitle>default</subtitle>
<link href="${request.url}" rel="self" />
<id>default+default+${request.registry.settings['site.site_name']}</id>
</%block>
<link href="${request.route_url('home')}" />
<updated>${last_update}</updated>

${self.body()}

</feed>
29 changes: 29 additions & 0 deletions raggregate/templates/base/atom_comment.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%inherit file="atom_base.mak" />

<%block name="feed_metadata">
<%
site_name = request.registry.settings['site.site_name']
feed_title = "{0} comments".format(site_name)
feed_subtitle = "newest comments on {0}".format(site_name)
%>

<title>${feed_title}</title>
<subtitle>${feed_subtitle}</subtitle>
<link href="${request.route_url(route)}" rel="self" />
<id>${feed_title}+${feed_subtitle}+${site_name}</id>
</%block>

% for c in comments:
<% s = c.load_submission() %>
<entry>
<title>${c.submitter.display_name()} on ${s.title} (${fuzzify_date(c.added_on)})</title>
<author>
<name>${c.submitter.display_name()}</name>
</author>
<link href="${request.route_url('full', sub_id=template_filters.get_submission_identifier_for_url(c.submission_id), _query=[('comment_perma', c.id)])}" />
<id>urn:uuid:${c.id}</id>
<updated>${c.added_on.isoformat()}</updated>
<summary>${c.body}</summary>
</entry>
% endfor

28 changes: 28 additions & 0 deletions raggregate/templates/base/atom_story.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%inherit file="atom_base.mak" />

<%block name="feed_metadata">
<%
site_name = request.registry.settings['site.site_name']
feed_title = "{0} stories".format(site_name)
feed_subtitle = "newest stories on {0}".format(site_name)
%>

<title>${feed_title}</title>
<subtitle>${feed_subtitle}</subtitle>
<link href="${request.route_url(route)}" rel="self" />
<id>${feed_title}+${feed_subtitle}+${site_name}</id>
</%block>

% for s in stories:
<entry>
<title>${s.title}</title>
<author>
<name>${s.submitter.display_name()}</name>
</author>
<link href="${request.route_url('full', sub_id=template_filters.get_submission_identifier_for_url(s))}" />
<id>urn:uuid:${s.id}</id>
<updated>${s.added_on.isoformat()}</updated>
<summary>${s.description}</summary>
</entry>
% endfor

4 changes: 4 additions & 0 deletions raggregate/templates/base/base.mak
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<head>
<title><%block name="html_title">${site_name}</%block></title>
<script type="text/javascript" src="${static_base}jquery-1.7.min.js"></script>
% if 'atom.enabled' in request.registry.settings and request.registry.settings['atom.enabled'] == 'true':
<link href="${request.route_url('atom_story')}" type="application/atom+xml" rel="alternate" title="${site_name} stories" />
<link href="${request.route_url('atom_comment')}" type="application/atom+xml" rel="alternate" title="${site_name} comments" />
% endif
</head>
<style type="text/css">

Expand Down
28 changes: 28 additions & 0 deletions raggregate/views/feeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from raggregate import queries

from pyramid.view import view_config

from raggregate.models import DBSession


@view_config(renderer='atom_story.mak', route_name='atom_story')
def story(request):
s = request.session
r = request
dbsession = DBSession()

stories = queries.get_story_list(page_num = 1, per_page = 30, sort = 'new', request = r)
last_update = stories['stories'][0].added_on.isoformat()
request.response.content_type = "text/xml"
return {'stories': stories['stories'], 'route': 'atom_story', 'last_update': last_update}

@view_config(renderer='atom_comment.mak', route_name='atom_comment')
def comment(request):
s = request.session
r = request
dbsession = DBSession()

comments = queries.get_recent_comments(20)
last_update = comments[0].added_on.isoformat()
request.response.content_type = "text/xml"
return {'comments': comments, 'route': 'atom_comment', 'last_update': last_update}

0 comments on commit dedc2ac

Please sign in to comment.