2 # -*- coding: UTF-8 -*-
3 """Creates an Atom-Feed for single or multiple winterrodeln sled reports.
6 http://www.winterrodeln.org/feeds/berichte/alle
7 http://www.winterrodeln.org/feeds/berichte/bahn/kemater_alm
8 http://www.winterrodeln.org/feeds/berichte/bahnen/22+42+132
10 http://www.atompub.org/
11 http://effbot.org/zone/element.htm
12 http://www.winterrodeln.org/trac/wiki/UrlSchema
16 from xml.etree.ElementTree import Element, SubElement, tostring
18 from sqlalchemy.engine import create_engine
22 from pylons import request, response, session, config, tmpl_context as c, url
23 from pylons.controllers.util import abort, redirect
25 from wrfeed.lib.base import BaseController, render
27 log = logging.getLogger(__name__)
30 def create_feed(page_title=None, page_ids=None):
31 """If a page_title is given, only the reports for the given sled are shown.
32 If a list of page_ids is given, only the reports for the selected pages are shown.
33 Otherwise, all reports are shown."""
35 engine = create_engine(config['sqlalchemy.url'])
36 conn = engine.connect()
38 select = "select id, page_title, date_report, date_entry, `condition`, description, author_name, author_username from wrreport "
39 if not page_title is None:
41 page_title = page_title.replace('_', ' ')
42 sql = select + "where lcase(page_title) = lcase(%s) and date_invalid > now() and delete_date is null order by id desc limit 50"
43 result = conn.execute(sql, page_title)
44 elif not page_ids is None:
45 # a list of page_ids is given
46 sql = [select + "where "]
49 sql += " or ".join(['page_id=%s' for page_id in page_ids])
51 sql += 'and date_invalid > now() and delete_date is null order by id desc limit 50'
52 page_ids_str = [str(page_id) for page_id in page_ids]
53 result = conn.execute("".join(sql), *page_ids_str)
55 # user wants to have all reports
56 sql = select + "where date_invalid > now() and delete_date is null order by id desc limit 50"
57 result = conn.execute(sql)
60 feed = Element("feed", xmlns="http://www.w3.org/2005/Atom")
61 feed_title = SubElement(feed, "title")
62 feed_title.text = "Winterrodeln Rodelbahnberichte"
63 feed_id = SubElement(feed, "id")
64 if not page_title is None:
65 feed_id.text = url(controller='berichte', action='bahn', id=page_title)
66 elif not page_ids is None:
67 feed_id.text = url(controller='berichte', action='bahnen', id="+".join(page_ids_str))
69 feed_id.text = url(controller='berichte', action='alle')
70 feed_updated = SubElement(feed, "updated")
71 feed.append(Element("link", rel="self", href=feed_id.text))
75 id, page_title, date_report, date_entry, condition, description, author_name, author_username = row
76 page_title_url = page_title.replace(u' ', u'_')
77 entry = SubElement(feed, "entry")
78 entry_title = SubElement(entry, "title")
79 entry_title.text = page_title
80 entry.append(Element("link", rel="alternate", href=u"http://www.winterrodeln.org/wiki/{0}".format(page_title_url), type="text/html", hreflang="de"))
81 entry_id = SubElement(entry, "id")
82 entry_id.text = u"http://www.winterrodeln.org/feeds/schneelage/{0}/{1}".format(page_title_url, id)
83 entry_updated = SubElement(entry, "updated")
84 entry_updated.text = date_entry.isoformat() + "+01:00"
85 if last_updated is None: last_updated = date_entry
86 # entry_summary = SubElement(entry, "summary")
87 # entry_summary.text = str(condition)
88 entry_content = SubElement(entry, "content")
89 entry_content.attrib["type"] = "xhtml"
90 entry_content_div = SubElement(entry_content, "div")
91 entry_content_div.attrib["xmlns"] = "http://www.w3.org/1999/xhtml"
92 entry_content_ul = SubElement(entry_content_div, "ul")
93 if not date_report is None:
94 entry_content_date = SubElement(entry_content_ul, "li")
95 entry_content_date.text = u"Bericht für " + date_report.isoformat()
96 if not condition is None:
97 entry_content_condition = SubElement(entry_content_ul, "li")
98 entry_content_condition.text = u"Schneelage: " + {1: u'Sehr gut', 2: u'Gut', 3: u'Mittelmäßig', 4: u'Schlecht', 5: u'Geht nicht'}[condition]
99 entry_content_description = SubElement(entry_content_ul, "li")
100 entry_content_description.text = description
101 entry_author = SubElement(entry, "author")
102 entry_author_name = SubElement(entry_author, "name")
103 if author_name is None: entry_author_name.text = "Anonymous user"
104 else: entry_author_name.text = author_name
106 if last_updated is None: last_updated = datetime.datetime.now()
107 feed_updated.text = last_updated.isoformat() + "+01:00"
109 feed_xml = '<?xml version="1.0" encoding="utf-8"?>\n' + tostring(feed)
115 class BerichteController(BaseController):
118 """http://www.winterrodeln.org/feeds/berichte/alle"""
119 response.content_type = 'application/atom+xml'
124 """http://www.winterrodeln.org/feeds/berichte/bahn/kemater_alm"""
125 response.content_type = 'application/atom+xml'
126 return create_feed(page_title=id)
129 def bahnen(self, id):
130 """http://www.winterrodeln.org/feeds/berichte/bahnen/22+42+132"""
131 page_ids = id.split('+')
132 page_ids = [int(page_id) for page_id in page_ids]
133 response.content_type = 'application/atom+xml'
134 return create_feed(page_ids=page_ids)