2 # -*- coding: UTF-8 -*-
3 """Creates an Atom-Feed for single or multiple winterrodeln sled reports.
6 http://www.winterrodeln.org/feed/berichte/alle
7 http://www.winterrodeln.org/feed/berichte/bahn/kemater_alm
8 http://www.winterrodeln.org/feed/berichte/bahnen/5+280+251
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
23 from pylons import request, response, session, config, tmpl_context as c, url
24 from pylons.controllers.util import abort, redirect
26 from wrfeed.lib.base import BaseController, render
28 log = logging.getLogger(__name__)
31 def create_feed(page_title=None, page_ids=None):
32 """If a page_title is given, only the reports for the given sledrun are shown.
33 If a list of page_ids is given, only the reports for the selected pages are shown.
34 Otherwise, all reports are shown."""
36 engine = create_engine(config['sqlalchemy.url'])
37 limit = int(config['feedentrylimit'])
38 conn = engine.connect()
40 select = 'select wrreport.page_id, wrreport.page_title, wrreport.id, date_report, date_entry, `condition`, description, author_name, author_userid, author_username, position_longitude, position_latitude from wrreport left outer join wrsledruncache on wrreport.page_id=wrsledruncache.page_id'
41 where = 'where date_invalid > now() and delete_date is null'
42 order = 'order by id desc'
43 limit = 'limit {0}'.format(limit)
46 if not page_title is None:
48 page_title = page_title.replace('_', ' ')
49 where += ' and lcase(wrreport.page_title) = lcase(%s)'
50 params += [page_title]
51 elif not page_ids is None:
52 # a list of page_ids is given
53 if len(page_ids) == 0:
54 raise webob.exc.HTTPBadRequest()
56 where += " or ".join(['wrreport.page_id=%s' for page_id in page_ids])
58 params += map(str, page_ids)
60 # user wants to have all reports
62 sql = ' '.join([select, where, order, limit])
63 result = conn.execute(sql, *params)
65 feed = Element("feed", xmlns="http://www.w3.org/2005/Atom", attrib={'xmlns:georss': 'http://www.georss.org/georss', 'xmlns:wr': 'http://www.winterrodeln.org/schema/wrreport'})
66 feed_title = SubElement(feed, "title")
67 feed_title.text = "Winterrodeln Rodelbahnberichte"
68 feed_id = SubElement(feed, "id")
69 if not page_title is None:
70 feed_id.text = url(qualified=True, controller='berichte', action='bahn', id=page_title)
71 elif not page_ids is None:
72 feed_id.text = url(qualified=True, controller='berichte', action='bahnen', id="+".join(map(str, page_ids)))
74 feed_id.text = url(qualified=True, controller='berichte', action='alle')
75 feed_updated = SubElement(feed, "updated")
76 feed.append(Element("link", rel="self", href=feed_id.text))
80 page_id, page_title, report_id, date_report, date_entry, condition, description, author_name, author_userid, author_username, lon, lat = row
81 page_title_url = page_title.replace(u' ', u'_')
82 entry = SubElement(feed, "entry")
83 entry_title = SubElement(entry, "title")
84 entry_title.text = page_title
85 entry.append(Element("link", rel="alternate", href=u"http://www.winterrodeln.org/wiki/{0}".format(page_title_url), type="text/html", hreflang="de"))
86 entry_id = SubElement(entry, "id")
87 entry_id.text = u"http://www.winterrodeln.org/wiki/{0}#{1}".format(page_title_url, report_id)
88 entry_updated = SubElement(entry, "updated")
89 entry_updated.text = date_entry.isoformat() + "+01:00"
90 if last_updated is None: last_updated = date_entry
91 # entry_summary = SubElement(entry, "summary")
92 # entry_summary.text = str(condition)
93 entry_content = SubElement(entry, "content")
94 entry_content.attrib["type"] = "xhtml"
95 entry_content_div = SubElement(entry_content, "div")
96 entry_content_div.attrib["xmlns"] = "http://www.w3.org/1999/xhtml"
97 entry_content_ul = SubElement(entry_content_div, "ul")
98 if not date_report is None:
99 entry_content_date = SubElement(entry_content_ul, "li")
100 entry_content_date.text = u"Bericht für " + date_report.isoformat()
101 if not condition is None:
102 entry_content_condition = SubElement(entry_content_ul, "li")
103 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]
104 entry_content_description = SubElement(entry_content_ul, "li")
105 entry_content_description.text = description
106 entry_author = SubElement(entry, "author")
107 entry_author_name = SubElement(entry_author, "name")
108 if author_name is None or len(author_name.strip()) == 0: entry_author_name.text = "Anonymous user"
109 else: entry_author_name.text = author_name
110 if not lon is None and not lat is None:
111 entry_geo = SubElement(entry, "georss:point")
112 entry_geo.text = "{lat} {lon}".format(lat=lat, lon=lon)
113 entry_wrreport = SubElement(entry, "wr:report")
114 entry_wrreport.attrib['report_id'] = str(report_id)
115 entry_wrreport.attrib['page_id'] = str(page_id)
116 entry_wrreport.attrib['page_title'] = page_title
117 entry_wrreport.attrib['date_report'] = str(date_report)
118 entry_wrreport.attrib['date_entry'] = str(date_entry)
119 entry_wrreport.attrib['condition'] = "0" if condition is None else str(condition)
120 entry_wrreport.attrib['author_name'] = author_name
121 entry_wrreport.attrib['author_username'] = "" if author_userid is None else author_username
122 entry_wrreport.text = description
124 if last_updated is None: last_updated = datetime.datetime.now()
125 feed_updated.text = last_updated.isoformat() + "+01:00"
127 feed_xml = '<?xml version="1.0" encoding="utf-8"?>\n' + tostring(feed)
133 class BerichteController(BaseController):
137 http://127.0.0.1:5000/berichte/alle
138 http://www.winterrodeln.org/feed/berichte/alle
140 response.content_type = 'application/atom+xml'
146 http://127.0.0.1:5000/berichte/bahn/kemater_alm
147 http://www.winterrodeln.org/feed/berichte/bahn/kemater_alm
149 response.content_type = 'application/atom+xml'
150 return create_feed(page_title=id)
153 def bahnen(self, id):
155 http://127.0.0.1:5000/berichte/bahnen/5+280+251
156 http://www.winterrodeln.org/feed/berichte/bahnen/5+280+251
158 page_ids = id.split('+')
160 page_ids = [int(page_id) for page_id in page_ids]
162 abort(400) # bad request
163 response.content_type = 'application/atom+xml'
164 return create_feed(page_ids=page_ids)