Added controller to serve atom feeds - but not finished yet.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Fri, 8 Oct 2010 17:53:24 +0000 (17:53 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Fri, 8 Oct 2010 17:53:24 +0000 (17:53 +0000)
git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/feed@584 7aebc617-e5e2-0310-91dc-80fb5f6d2477

feed/controllers/berichte.py [new file with mode: 0644]
feed/tests/functional/test_berichte.py [new file with mode: 0644]

diff --git a/feed/controllers/berichte.py b/feed/controllers/berichte.py
new file mode 100644 (file)
index 0000000..4f97f3e
--- /dev/null
@@ -0,0 +1,105 @@
+#!/usr/bin/python2.6
+# -*- coding: UTF-8 -*-
+"""Creates an Atom-Feed for single or multiple winterrodeln sled reports.
+
+Format:
+    http://www.winterrodeln.org/feeds/berichte/alle
+    http://www.winterrodeln.org/feeds/berichte/bahn/kemater_alm
+    http://www.winterrodeln.org/feeds/berichte/bahnen/22+42+132 
+See:
+    http://www.atompub.org/
+    http://effbot.org/zone/element.htm
+    http://www.winterrodeln.org/trac/wiki/UrlSchema
+"""
+import sys
+import datetime
+# from elementtree.ElementTree import Element, SubElement, tostring
+from xml.etree.ElementTree import Element, SubElement, tostring
+
+from sqlalchemy.engine import create_engine
+
+import logging
+
+from pylons import request, response, session, config, tmpl_context as c, url
+from pylons.controllers.util import abort, redirect
+
+from feed.lib.base import BaseController, render
+
+log = logging.getLogger(__name__)
+
+
+def create_feed(engine):
+    param_page_title = None
+
+    conn = engine.connect()
+    if param_page_title is None:
+        sql = "select id, page_title, date_report, date_entry, `condition`, description, author_name, author_username from wrreport where date_invalid > now() and delete_date is null order by id desc limit 50"
+        # Debug:
+        sql = "select id, page_title, date_report, date_entry, `condition`, description, author_name, author_username from wrreport where delete_date is null order by id limit 5"
+        result = conn.execute(sql)
+    else:
+        sql = "select id, page_title, date_report, date_entry, `condition`, description, author_name, author_username from wrreport where page_title ilike ? and date_invalid > now() and delete_date is null order by id desc limit 50"
+        result = conn.execute(sql, param_page_title)
+
+
+    feed = Element("feed", xmlns="http://www.w3.org/2005/Atom")
+    feed_title = SubElement(feed, "title")
+    feed_title.text = "Winterrodeln Rodelbahnberichte"
+    feed_id = SubElement(feed, "id")
+    if param_page_title is None:
+        feed_id.text = "http://www.winterrodeln.org/feeds/schneelage"
+    else:
+        feed_id.text = "http://www.winterrodeln.org/feeds/schneelage/{0}".format(param_page_title.replace(u' ', u'_'))
+    feed_updated = SubElement(feed, "updated")
+    feed.append(Element("link", rel="self", href=feed_id.text))
+
+    last_updated = None
+    for row in result:
+        id, page_title, date_report, date_entry, condition, description, author_name, author_username = row
+        page_title_url = page_title.replace(u' ', u'_')
+        entry = SubElement(feed, "entry")
+        entry_title = SubElement(entry, "title")
+        entry_title.text = page_title
+        entry.append(Element("link", rel="alternate", href=u"http://www.winterrodeln.org/wiki/{0}".format(page_title_url), type="text/html", hreflang="de"))
+        entry_id = SubElement(entry, "id")
+        entry_id.text = "http://www.winterrodeln.org/feeds/schneelage/{0}/{1}".format(page_title_url, id)
+        entry_updated = SubElement(entry, "updated")
+        entry_updated.text = date_entry.isoformat() + "+01:00"
+        if last_updated is None: last_updated = date_entry
+        # entry_summary = SubElement(entry, "summary")
+        # entry_summary.text = str(condition)
+        entry_content = SubElement(entry, "content")
+        entry_content.attrib["type"] = "xhtml"
+        entry_content_div = SubElement(entry_content, "div")
+        entry_content_div.attrib["xmlns"] = "http://www.w3.org/1999/xhtml"
+        entry_content_ul = SubElement(entry_content_div, "ul")
+        if not date_report is None:
+            entry_content_date = SubElement(entry_content_ul, "li")
+            entry_content_date.text = u"Bericht für " + date_report.isoformat()
+        if not condition is None:
+            entry_content_condition = SubElement(entry_content_ul, "li")
+            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]
+        entry_content_description = SubElement(entry_content_ul, "li")
+        entry_content_description.text = description
+        entry_author = SubElement(entry, "author")
+        entry_author_name = SubElement(entry_author, "name")
+        if author_name is None: entry_author_name.text = "Anonymous user"
+        else: entry_author_name.text = author_name
+
+    if last_updated is None: last_updated = datetime.datetime.now()
+    feed_updated.text = last_updated.isoformat() + "+01:00"
+
+    feed_xml =  '<?xml version="1.0" encoding="utf-8"?>\n' + tostring(feed)
+    conn.close()
+    return feed_xml
+
+
+
+class BerichteController(BaseController):
+
+    def index(self):
+        # Return a rendered template
+        #return render('/berichte.mako')
+        # or, return a response
+        engine = create_engine(config['sqlalchemy.url'])
+        return create_feed(engine)
diff --git a/feed/tests/functional/test_berichte.py b/feed/tests/functional/test_berichte.py
new file mode 100644 (file)
index 0000000..693a9f2
--- /dev/null
@@ -0,0 +1,7 @@
+from feed.tests import *
+
+class TestBerichteController(TestController):
+
+    def test_index(self):
+        response = self.app.get(url(controller='berichte', action='index'))
+        # Test response...