-#!/usr/bin/python2.7
+#!/usr/bin/python3.4
# -*- coding: iso-8859-15 -*-
# $Id$
# $HeadURL$
from sqlalchemy import schema
from sqlalchemy.sql import select
from sqlalchemy.sql.expression import func as sqlfunc
-import geoalchemy.functions as geofunc
from osgeo import ogr
import formencode
-from wrpylib import mwdb, wrmwdb, mwmarkup, wrmwmarkup
+from wrpylib import mwdb, wrmwdb, mwmarkup, wrmwmarkup, wrvalidators
class UpdateCacheError(RuntimeError):
transaction = connection.begin()
# Query all sled runs
- q = select([page, categorylinks, revision, text], (page.c.page_latest==revision.c.rev_id) & (text.c.old_id==revision.c.rev_text_id) & (categorylinks.c.cl_from==page.c.page_id) & (categorylinks.c.cl_to==u'Rodelbahn'))
+ q = select([page, categorylinks, revision, text], (page.c.page_latest==revision.c.rev_id) & (text.c.old_id==revision.c.rev_text_id) & (categorylinks.c.cl_from==page.c.page_id) & (categorylinks.c.cl_to=='Rodelbahn'))
sledrun_pages = connection.execute(q)
# Original SQL:
# sql = u"select page_id, rev_id, old_id, page_title, old_text, 'In_Arbeit' in (select cl_to from categorylinks where cl_from=page_id) as under_construction from page, revision, text, categorylinks where page_latest=rev_id and old_id=rev_text_id and cl_from=page_id and cl_to='Rodelbahn' order by page_title"
start, end, sledrun = wrmwmarkup.rodelbahnbox_to_sledrun(sledrun_page.old_text)
sledrun.page_id = sledrun_page.page_id
sledrun.page_title = sledrun_page.page_title
- sledrun.under_construction = connection.execute(select([categorylinks], (categorylinks.c.cl_from==sledrun_page.page_id) & (categorylinks.c.cl_to == u'In_Arbeit')).alias('x').count()).fetchone()[0] > 0 # It would be better to do this in the query above
+ sledrun.name_url = wrvalidators.sledrun_page_title_to_pretty_url(sledrun_page.page_title)
+ sledrun.under_construction = connection.execute(select([categorylinks], (categorylinks.c.cl_from==sledrun_page.page_id) & (categorylinks.c.cl_to == 'In_Arbeit')).alias('x').count()).fetchone()[0] > 0 # It would be better to do this in the query above
connection.execute(wrsledruncache.insert(sledrun.__dict__))
except (RuntimeError, formencode.Invalid) as e:
transaction.rollback()
- error_msg = u"Error at sled run '{0}': {1}".format(sledrun_page.page_title, unicode(e))
+ error_msg = "Error at sled run '{0}': {1}".format(sledrun_page.page_title, str(e))
raise UpdateCacheError(error_msg, sledrun_page.page_title, e)
transaction.commit()
transaction = connection.begin()
# Query all inns
- q = select([page, categorylinks, revision, text], (page.c.page_latest==revision.c.rev_id) & (text.c.old_id==revision.c.rev_text_id) & (categorylinks.c.cl_from==page.c.page_id) & (categorylinks.c.cl_to==u'Gasthaus'))
+ q = select([page, categorylinks, revision, text], (page.c.page_latest==revision.c.rev_id) & (text.c.old_id==revision.c.rev_text_id) & (categorylinks.c.cl_from==page.c.page_id) & (categorylinks.c.cl_to=='Gasthaus'))
inn_pages = connection.execute(q)
# Delete all existing entries in wrinncache
start, end, inn = wrmwmarkup.gasthausbox_to_inn(inn_page.old_text)
inn.page_id = inn_page.page_id
inn.page_title = inn_page.page_title
- inn.under_construction = connection.execute(select([categorylinks], (categorylinks.c.cl_from==inn_page.page_id) & (categorylinks.c.cl_to == u'In_Arbeit')).alias('x').count()).fetchone()[0] > 0 # It would be better to do this in the query above
+ inn.under_construction = connection.execute(select([categorylinks], (categorylinks.c.cl_from==inn_page.page_id) & (categorylinks.c.cl_to == 'In_Arbeit')).alias('x').count()).fetchone()[0] > 0 # It would be better to do this in the query above
connection.execute(wrinncache.insert(inn.__dict__))
except (RuntimeError, formencode.Invalid) as e:
transaction.rollback()
- error_msg = u"Error as inn '{0}': {1}".format(inn_page.page_title, unicode(e))
+ error_msg = "Error as inn '{0}': {1}".format(inn_page.page_title, str(e))
raise UpdateCacheError(error_msg, inn_page.page_title, e)
transaction.commit()
transaction = connection.begin()
# Query all sledruns
- q = select([page, categorylinks, revision, text], (page.c.page_latest==revision.c.rev_id) & (text.c.old_id==revision.c.rev_text_id) & (categorylinks.c.cl_from==page.c.page_id) & (categorylinks.c.cl_to==u'Rodelbahn'))
+ q = select([page, categorylinks, revision, text], (page.c.page_latest==revision.c.rev_id) & (text.c.old_id==revision.c.rev_text_id) & (categorylinks.c.cl_from==page.c.page_id) & (categorylinks.c.cl_to=='Rodelbahn'))
sledrun_pages = connection.execute(q)
# Original SQL:
# sql = u"select page_id, rev_id, old_id, page_title, old_text, 'In_Arbeit' in (select cl_to from categorylinks where cl_from=page_id) as under_construction from page, revision, text, categorylinks where page_latest=rev_id and old_id=rev_text_id and cl_from=page_id and cl_to='Rodelbahn' order by page_title"
if properties['type'] in wrmwmarkup.WRMAP_POINT_TYPES:
lon, lat = coordinates
label = properties.get('name')
- point_types = {u'gasthaus': u'hut', u'haltestelle': u'busstop', u'parkplatz': u'carpark', u'achtung': u'warning', u'foto': u'photo', u'verleih': u'rental', u'punkt': u'point'}
+ point_types = {'gasthaus': 'hut', 'haltestelle': 'busstop', 'parkplatz': 'carpark', 'achtung': 'warning', 'foto': 'photo', 'verleih': 'rental', 'punkt': 'point'}
point_type = point_types[properties['type']]
- sql = u'insert into wrmappointcache (page_id, type, point, label) values (%s, %s, POINT(%s, %s), %s)'
+ sql = 'insert into wrmappointcache (page_id, type, point, label) values (%s, %s, POINT(%s, %s), %s)'
connection.execute(sql, (sledrun_page.page_id, point_type, lon, lat, label))
# Paths
elif properties['type'] in wrmwmarkup.WRMAP_LINE_TYPES:
- path_types = {u'rodelbahn': u'sledrun', u'gehweg': u'walkup', u'alternative': u'alternative', u'lift': u'lift', u'anfahrt': u'recommendedcarroute', u'linie': u'line'}
+ path_types = {'rodelbahn': 'sledrun', 'gehweg': 'walkup', 'alternative': 'alternative', 'lift': 'lift', 'anfahrt': 'recommendedcarroute', 'linie': 'line'}
path_type = path_types[properties['type']]
- path = u", ".join(["{0} {1}".format(lon, lat) for lon, lat in coordinates])
- path = u'LineString({0})'.format(path)
- if path_type == u'recommendedcarroute': continue
- sql = u'insert into wrmappathcache (path, page_id, type) values (GeomFromText(%s), %s, %s)'
+ path = ", ".join(["{0} {1}".format(lon, lat) for lon, lat in coordinates])
+ path = 'LineString({0})'.format(path)
+ if path_type == 'recommendedcarroute': continue
+ sql = 'insert into wrmappathcache (path, page_id, type) values (GeomFromText(%s), %s, %s)'
connection.execute(sql, (path, sledrun_page.page_id, path_type))
else:
- raise RuntimeError(u'Unknown feature type {0}'.format(properties['type']))
+ raise RuntimeError('Unknown feature type {0}'.format(properties['type']))
except RuntimeError as e:
- error_msg = u"Error at sledrun '{0}': {1}".format(sledrun_page.page_title, unicode(e))
+ error_msg = "Error at sledrun '{0}': {1}".format(sledrun_page.page_title, str(e))
transaction.rollback()
raise UpdateCacheError(error_msg, sledrun_page.page_title, e)
transaction.commit()
connection.execute(wrregioncache.delete())
# Query all combinations of sledruns and regions
- sel = select([wrregion.c.id.label('region_id'), wrregion.c.border.label('border'), wrsledruncache.c.page_id, wrsledruncache.c.position_longitude, wrsledruncache.c.position_latitude], geofunc.gcontains(wrregion.c.border, sqlfunc.point(wrsledruncache.c.position_longitude, wrsledruncache.c.position_latitude)))
+ sel = select([wrregion.c.id.label('region_id'), sqlfunc.AsWKB(wrregion.c.border).label('border'), wrsledruncache.c.page_id, wrsledruncache.c.position_longitude, wrsledruncache.c.position_latitude], sqlfunc.contains(wrregion.c.border, sqlfunc.point(wrsledruncache.c.position_longitude, wrsledruncache.c.position_latitude)))
ins = wrregioncache.insert()
# Refill wrregioncache
result = connection.execute(sel)
for row in result:
point.SetPoint(0, row.position_longitude, row.position_latitude)
- if point.Within(ogr.CreateGeometryFromWkb(row.border.geom_wkb)):
+ if point.Within(ogr.CreateGeometryFromWkb(row.border)):
connection.execute(ins.values(region_id=row.region_id, page_id=row.page_id))
# commit