Remove directory controllers.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Sun, 6 Aug 2017 18:56:04 +0000 (18:56 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Sun, 6 Aug 2017 18:56:04 +0000 (18:56 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wrfeed/trunk@2700 7aebc617-e5e2-0310-91dc-80fb5f6d2477

wrfeed/berichte.py [new file with mode: 0644]
wrfeed/controllers/__init__.py [deleted file]
wrfeed/controllers/berichte.py [deleted file]
wrfeed/wrfeed.py

diff --git a/wrfeed/berichte.py b/wrfeed/berichte.py
new file mode 100644 (file)
index 0000000..10e83af
--- /dev/null
@@ -0,0 +1,146 @@
+"""Creates an Atom-Feed for single or multiple winterrodeln sled reports.
+
+Format:
+    http://www.winterrodeln.org/feed/berichte/alle
+    http://www.winterrodeln.org/feed/berichte/bahn/kemater_alm
+    http://www.winterrodeln.org/feed/berichte/bahnen/5+280+251
+    http://www.winterrodeln.org/feed/berichte/region/osttirol
+See:
+    http://www.atompub.org/
+    http://effbot.org/zone/element.htm
+    http://www.winterrodeln.org/trac/wiki/UrlSchema
+"""
+import datetime
+from xml.etree.ElementTree import Element, SubElement, tostring
+from osgeo import ogr
+from flask import url_for
+
+
+class CreateFeedError(RuntimeError):
+    pass
+
+
+class RegionNotFoundError(CreateFeedError):
+    pass
+
+
+def create_feed(conn, limit, page_title=None, page_ids=None, region_name=None):
+    """If a page_title is given, only the reports for the given sledrun are shown.
+    If a list of page_ids is given, only the reports for the selected pages are shown.
+    If a region name (lower case) is given, the reports just for this region are shown.
+    Otherwise, all reports are shown."""
+    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'
+    where = 'where date_invalid > now() and delete_date is null'
+    order = 'order by id desc'
+    limit = 'limit {0}'.format(limit)
+    params = []
+
+    if not page_title is None:
+        # page_title is given
+        page_title = page_title.replace('_', ' ')
+        where += ' and lcase(wrreport.page_title) = lcase(%s)'
+        params += [page_title]
+    elif not page_ids is None:
+        # a list of page_ids is given
+        assert len(page_ids) > 0
+        where += ' and ('
+        where += " or ".join(['wrreport.page_id=%s' for page_id in page_ids])
+        where += ')'
+        params += list(map(str, page_ids))
+    elif not region_name is None:
+        # a name of a region is given
+        # (1) find out whether the region exists
+        subselect = 'select aswkb(border) as border_wkb from wrregion where name=lcase(%s)'
+        subresult = conn.execute(subselect, region_name)
+        if subresult.rowcount == 0:
+            # no region with such a name
+            raise RegionNotFoundError(region_name)
+        assert subresult.rowcount == 1
+        row = subresult.fetchone()
+        # (2) now we have the border
+        border_wkb = row['border_wkb'] # border as WKB
+        where += ' and contains(geomfromwkb(%s), point(position_longitude, position_latitude))'
+        params += [border_wkb]
+        # the following variables are needed for the additional filtering below
+        border = ogr.CreateGeometryFromWkb(border_wkb)
+        point = ogr.Geometry(ogr.wkbPoint)
+    else:
+        # user wants to have all reports
+        pass
+    sql = ' '.join([select, where, order, limit])
+    result = conn.execute(sql, *params)
+
+    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'})
+    feed_title = SubElement(feed, "title")
+    feed_title.text = "Winterrodeln Rodelbahnberichte"
+    feed_id = SubElement(feed, "id")
+    if not page_title is None:
+        feed_id.text = url_for('bahn', bahn=page_title, _external=True)
+    elif not page_ids is None:
+        feed_id.text = url_for('bahnen', bahnen="+".join(map(str, page_ids)), _external=True)
+    elif not region_name is None:
+        feed_id.text = url_for('region', region=region_name, _external=True)
+    else:
+        feed_id.text = url_for('alle', _external=True)
+    feed_updated = SubElement(feed, "updated")
+    feed.append(Element("link", rel="self", href=feed_id.text))
+
+    last_updated = None
+    for row in result:
+        page_id, page_title, report_id, date_report, date_entry, condition, description, author_name, author_userid, author_username, lon, lat = row
+
+        if not region_name is None:
+            # mysql 5.5 doesn't have specification conform geospacial functions.
+            # http://dev.mysql.com/doc/refman/5.5/en/functions-for-testing-spatial-relations-between-geometric-objects.html
+            # therefore we have to do further filtering here.
+            point.SetPoint(0, lon, lat)
+            if not point.Within(border):
+                continue
+
+        page_title_url = page_title.replace(' ', '_')
+        entry = SubElement(feed, "entry")
+        entry_title = SubElement(entry, "title")
+        entry_title.text = page_title
+        entry.append(Element("link", rel="alternate", href="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/wiki/{0}#{1}".format(page_title_url, report_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 = "Bericht für " + date_report.isoformat()
+        if not condition is None:
+            entry_content_condition = SubElement(entry_content_ul, "li")
+            entry_content_condition.text = "Schneelage: " + {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelmäßig', 4: 'Schlecht', 5: '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 or len(author_name.strip()) == 0: entry_author_name.text = "Anonymous user"
+        else: entry_author_name.text = author_name
+        if not lon is None and not lat is None:
+            entry_geo = SubElement(entry, "georss:point")
+            entry_geo.text = "{lat} {lon}".format(lat=lat, lon=lon)
+        entry_wrreport = SubElement(entry, "wr:report")
+        entry_wrreport.attrib['report_id'] = str(report_id)
+        entry_wrreport.attrib['page_id'] = str(page_id)
+        entry_wrreport.attrib['page_title'] = page_title
+        entry_wrreport.attrib['date_report'] = str(date_report)
+        entry_wrreport.attrib['date_entry'] = str(date_entry)
+        entry_wrreport.attrib['condition'] = "0" if condition is None else str(condition)
+        entry_wrreport.attrib['author_name'] = author_name
+        entry_wrreport.attrib['author_username'] = "" if author_userid is None else author_username
+        entry_wrreport.text = description
+
+    if last_updated is None: last_updated = datetime.datetime.now()
+    feed_updated.text = last_updated.isoformat() + "+01:00"
+
+    return b'<?xml version="1.0" encoding="utf-8"?>\n' + tostring(feed)
diff --git a/wrfeed/controllers/__init__.py b/wrfeed/controllers/__init__.py
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/wrfeed/controllers/berichte.py b/wrfeed/controllers/berichte.py
deleted file mode 100644 (file)
index 10e83af..0000000
+++ /dev/null
@@ -1,146 +0,0 @@
-"""Creates an Atom-Feed for single or multiple winterrodeln sled reports.
-
-Format:
-    http://www.winterrodeln.org/feed/berichte/alle
-    http://www.winterrodeln.org/feed/berichte/bahn/kemater_alm
-    http://www.winterrodeln.org/feed/berichte/bahnen/5+280+251
-    http://www.winterrodeln.org/feed/berichte/region/osttirol
-See:
-    http://www.atompub.org/
-    http://effbot.org/zone/element.htm
-    http://www.winterrodeln.org/trac/wiki/UrlSchema
-"""
-import datetime
-from xml.etree.ElementTree import Element, SubElement, tostring
-from osgeo import ogr
-from flask import url_for
-
-
-class CreateFeedError(RuntimeError):
-    pass
-
-
-class RegionNotFoundError(CreateFeedError):
-    pass
-
-
-def create_feed(conn, limit, page_title=None, page_ids=None, region_name=None):
-    """If a page_title is given, only the reports for the given sledrun are shown.
-    If a list of page_ids is given, only the reports for the selected pages are shown.
-    If a region name (lower case) is given, the reports just for this region are shown.
-    Otherwise, all reports are shown."""
-    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'
-    where = 'where date_invalid > now() and delete_date is null'
-    order = 'order by id desc'
-    limit = 'limit {0}'.format(limit)
-    params = []
-
-    if not page_title is None:
-        # page_title is given
-        page_title = page_title.replace('_', ' ')
-        where += ' and lcase(wrreport.page_title) = lcase(%s)'
-        params += [page_title]
-    elif not page_ids is None:
-        # a list of page_ids is given
-        assert len(page_ids) > 0
-        where += ' and ('
-        where += " or ".join(['wrreport.page_id=%s' for page_id in page_ids])
-        where += ')'
-        params += list(map(str, page_ids))
-    elif not region_name is None:
-        # a name of a region is given
-        # (1) find out whether the region exists
-        subselect = 'select aswkb(border) as border_wkb from wrregion where name=lcase(%s)'
-        subresult = conn.execute(subselect, region_name)
-        if subresult.rowcount == 0:
-            # no region with such a name
-            raise RegionNotFoundError(region_name)
-        assert subresult.rowcount == 1
-        row = subresult.fetchone()
-        # (2) now we have the border
-        border_wkb = row['border_wkb'] # border as WKB
-        where += ' and contains(geomfromwkb(%s), point(position_longitude, position_latitude))'
-        params += [border_wkb]
-        # the following variables are needed for the additional filtering below
-        border = ogr.CreateGeometryFromWkb(border_wkb)
-        point = ogr.Geometry(ogr.wkbPoint)
-    else:
-        # user wants to have all reports
-        pass
-    sql = ' '.join([select, where, order, limit])
-    result = conn.execute(sql, *params)
-
-    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'})
-    feed_title = SubElement(feed, "title")
-    feed_title.text = "Winterrodeln Rodelbahnberichte"
-    feed_id = SubElement(feed, "id")
-    if not page_title is None:
-        feed_id.text = url_for('bahn', bahn=page_title, _external=True)
-    elif not page_ids is None:
-        feed_id.text = url_for('bahnen', bahnen="+".join(map(str, page_ids)), _external=True)
-    elif not region_name is None:
-        feed_id.text = url_for('region', region=region_name, _external=True)
-    else:
-        feed_id.text = url_for('alle', _external=True)
-    feed_updated = SubElement(feed, "updated")
-    feed.append(Element("link", rel="self", href=feed_id.text))
-
-    last_updated = None
-    for row in result:
-        page_id, page_title, report_id, date_report, date_entry, condition, description, author_name, author_userid, author_username, lon, lat = row
-
-        if not region_name is None:
-            # mysql 5.5 doesn't have specification conform geospacial functions.
-            # http://dev.mysql.com/doc/refman/5.5/en/functions-for-testing-spatial-relations-between-geometric-objects.html
-            # therefore we have to do further filtering here.
-            point.SetPoint(0, lon, lat)
-            if not point.Within(border):
-                continue
-
-        page_title_url = page_title.replace(' ', '_')
-        entry = SubElement(feed, "entry")
-        entry_title = SubElement(entry, "title")
-        entry_title.text = page_title
-        entry.append(Element("link", rel="alternate", href="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/wiki/{0}#{1}".format(page_title_url, report_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 = "Bericht für " + date_report.isoformat()
-        if not condition is None:
-            entry_content_condition = SubElement(entry_content_ul, "li")
-            entry_content_condition.text = "Schneelage: " + {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelmäßig', 4: 'Schlecht', 5: '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 or len(author_name.strip()) == 0: entry_author_name.text = "Anonymous user"
-        else: entry_author_name.text = author_name
-        if not lon is None and not lat is None:
-            entry_geo = SubElement(entry, "georss:point")
-            entry_geo.text = "{lat} {lon}".format(lat=lat, lon=lon)
-        entry_wrreport = SubElement(entry, "wr:report")
-        entry_wrreport.attrib['report_id'] = str(report_id)
-        entry_wrreport.attrib['page_id'] = str(page_id)
-        entry_wrreport.attrib['page_title'] = page_title
-        entry_wrreport.attrib['date_report'] = str(date_report)
-        entry_wrreport.attrib['date_entry'] = str(date_entry)
-        entry_wrreport.attrib['condition'] = "0" if condition is None else str(condition)
-        entry_wrreport.attrib['author_name'] = author_name
-        entry_wrreport.attrib['author_username'] = "" if author_userid is None else author_username
-        entry_wrreport.text = description
-
-    if last_updated is None: last_updated = datetime.datetime.now()
-    feed_updated.text = last_updated.isoformat() + "+01:00"
-
-    return b'<?xml version="1.0" encoding="utf-8"?>\n' + tostring(feed)
index 2bf02c92fc05a6d8f7510abcb62a203cfb4ff952..2d707d090225ef9c1a6addea533108a847832934 100644 (file)
@@ -3,8 +3,7 @@
 # FLASK_APP=wrfeed WRFEED_SETTINGS=production.cfg python3 -m flask run
 from sqlalchemy.engine import create_engine
 from flask import Flask, send_from_directory, abort, g, jsonify
-from wrfeed.controllers.berichte import create_feed
-
+from wrfeed.berichte import create_feed
 
 app = Flask(__name__)
 app.config.from_envvar('WRFEED_SETTINGS')