Generating the feed for a region is basically implemented.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Sun, 27 Oct 2013 20:09:38 +0000 (20:09 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Sun, 27 Oct 2013 20:09:38 +0000 (20:09 +0000)
MySQL 5.5 doesn't implement spatial "contains" correctly. That is not
corrected yet by the program.

git-svn-id: http://www.winterrodeln.org/svn/wrfeed/trunk@1759 7aebc617-e5e2-0310-91dc-80fb5f6d2477

wrfeed/controllers/berichte.py

index 7018a9dfd9bc37b837e11d9924b21ef5d19f8198..74b7d2315f85ca52ed446ccc2d6cd5c8ce331023 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python2.6
+#!/usr/bin/python2.7
 # -*- coding: UTF-8 -*-
 """Creates an Atom-Feed for single or multiple winterrodeln sled reports.
 
@@ -6,6 +6,7 @@ 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
@@ -28,9 +29,10 @@ from wrfeed.lib.base import BaseController, render
 log = logging.getLogger(__name__)
 
 
-def create_feed(page_title=None, page_ids=None):
+def create_feed(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."""
 
     engine = create_engine(config['sqlalchemy.url'])
@@ -56,6 +58,19 @@ def create_feed(page_title=None, page_ids=None):
         where += " or ".join(['wrreport.page_id=%s' for page_id in page_ids])
         where += ')'
         params += 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 webob.exc.HTTPNotFound()
+        assert subresult.rowcount == 1
+        row = subresult.fetchone()
+        border_wkb = row['border_wkb'] # border as WKB
+        where += ' and contains(geomfromwkb(%s), point(position_longitude, position_latitude))'
+        params += [border_wkb]
     else:
         # user wants to have all reports
         pass
@@ -70,6 +85,8 @@ def create_feed(page_title=None, page_ids=None):
         feed_id.text = url(qualified=True, controller='berichte', action='bahn', id=page_title)
     elif not page_ids is None:
         feed_id.text = url(qualified=True, controller='berichte', action='bahnen', id="+".join(map(str, page_ids)))
+    elif not region_name is None:
+        feed_id.text = url(qualified=True, controller='berichte', action='region', id=region_name)
     else:
         feed_id.text = url(qualified=True, controller='berichte', action='alle')
     feed_updated = SubElement(feed, "updated")
@@ -162,3 +179,11 @@ class BerichteController(BaseController):
             abort(400) # bad request
         response.content_type = 'application/atom+xml'
         return create_feed(page_ids=page_ids)
+
+    def region(self, id):
+        """Handles URLs like
+        http://www.winterrodeln.org/feed/berichte/region/osttirol
+        """
+        response.content_type = 'application/atom+xml'
+        return create_feed(region_name=id)
+