Skip to content

Commit 0a88ed6

Browse files
committed
atom feed
1 parent 2c8e624 commit 0a88ed6

File tree

6 files changed

+61
-10
lines changed

6 files changed

+61
-10
lines changed

atom.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from tipfy import RequestHandler, request, Response, url_for
2+
from tipfy.ext.jinja2 import render_template
3+
from google.appengine.ext import db
4+
import models
5+
from filters import FilterCollection, filters
6+
7+
class AtomViewHandler(RequestHandler):
8+
def get(self, **kwargs):
9+
response = Response(mimetype = 'application/atom+xml')
10+
11+
collection = FilterCollection(filters, request, response, cookies=False)
12+
13+
q = models.accepted_quotes()
14+
collection.add_to_query(q)
15+
q.order('-creation_date')
16+
17+
out = render_template(
18+
'cppbash/atom.xml',
19+
title = "C++ Bash",
20+
link = url_for('home', full=True),
21+
quotes = q)
22+
23+
response.response = [out]
24+
25+
return response

filters.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ def __init__(self, name, alternatives, default = '', add_empty = True):
88
self._alternatives.append('')
99
self._default = default
1010

11-
def filter(self, request, response):
11+
def filter(self, request, response, cookies=True):
1212
data = request.args.get(self.name, None)
1313
if data != None:
14-
response.set_cookie(self.name, data)
14+
if cookies:
15+
response.set_cookie(self.name, data)
1516
else:
16-
data = request.cookies.get(self.name, None)
17+
if cookies:
18+
data = request.cookies.get(self.name, None)
1719
if data == None:
1820
data = self._default
19-
response.set_cookie(self.name, data)
21+
if cookies:
22+
response.set_cookie(self.name, data)
2023
return data
2124

2225
def alternatives(self, removed):
@@ -25,13 +28,13 @@ def alternatives(self, removed):
2528
result.remove(removed)
2629
return result
2730

28-
def compute(self, request, response):
29-
current = self.filter(request, response)
31+
def compute(self, request, response, cookies=True):
32+
current = self.filter(request, response, cookies)
3033
other = self.alternatives(current)
3134
return (current, other)
3235

33-
def make_instance(self, request, response):
34-
(current, other) = self.compute(request, response)
36+
def make_instance(self, request, response, cookies=True):
37+
(current, other) = self.compute(request, response, cookies)
3538
return FilterInstance(self.name, current, other, self._alternatives)
3639

3740
class FilterInstance(object):
@@ -46,10 +49,10 @@ def add_to_query(self, query):
4649
query.filter(self.name + ' =', self.current)
4750

4851
class FilterCollection(object):
49-
def __init__(self, filters, request, response):
52+
def __init__(self, filters, request, response, cookies=True):
5053
self.instances = dict()
5154
for filt in filters:
52-
self.instances[filt.name] = filt.make_instance(request, response)
55+
self.instances[filt.name] = filt.make_instance(request, response, cookies)
5356

5457
def __getitem__(self, key):
5558
return self.instances[key]

middleware/jinja.py

+4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ def linebreaks(eval_ctx, value):
1818
def urlparameter(value):
1919
return urllib.quote_plus(value)
2020

21+
def atomdatetime(value):
22+
return value.replace(microsecond=0, tzinfo=None).isoformat() + 'Z'
23+
2124
class JinjaMiddleware(object):
2225
def pre_dispatch_handler(self):
2326
env = get_env()
2427
env.autoescape = True
2528
env.filters['linebreaks'] = linebreaks
2629
env.filters['urlparameter'] = urlparameter
30+
env.filters['atomdatetime'] = atomdatetime
2731

2832

templates/cppbash/atom.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<feed xmlns="http://www.w3.org/2005/Atom">
3+
4+
<title>{{ title }}</title>
5+
<link href="{{ link }}"/>
6+
7+
{% for quote in quotes -%}
8+
<entry>
9+
<title>Quote from {{ quote.creation_date }} ({{ quote.language }} {{ quote.programming_language }})</title>
10+
<link href="{{ url_for('quote-view', id = quote.key().id(), full=True) }}"/>
11+
<id>{{ url_for('quote-view', id = quote.key().id(), full=True) }}</id>
12+
<updated>{{ quote.creation_date|atomdatetime }}</updated>
13+
<content type="text">{{ quote.quote }}</content>
14+
</entry>
15+
{% endfor %}
16+
</feed>

templates/cppbash/base.html

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<meta charset="utf-8"> <!-- for validator freaks -->
55
<title>{{ head_title }} C++ Bash - Quotes from the C++ world </title>
66
<link rel="stylesheet" type="text/css" href="/static/main.css"/>
7+
<link href="{{ url_for('atom-view') }}" type="application/atom+xml" rel="alternate" title="Sitewide ATOM Feed" />
8+
79
<script type="text/javascript" src="/static/jquery-min.js"></script>
810
<script type="text/javascript">
911
var _gaq = _gaq || [];

urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def get_rules():
2222
tipfy.Rule('/review/<int:id>', endpoint='review-quote', handler='review.ReviewQuoteHandler'),
2323
tipfy.Rule('/quote/<int:id>', endpoint='quote-view', handler='quote.QuoteViewHandler'),
2424
tipfy.Rule('/random', endpoint='random-quote', handler='random_quote.RandomQuoteHandler'),
25+
tipfy.Rule('/atom', endpoint='atom-view', handler='atom.AtomViewHandler')
2526
]
2627

2728
for app_module in tipfy.get_config('tipfy', 'apps_installed'):

0 commit comments

Comments
 (0)