2 # -*- coding: iso-8859-15 -*-
5 from pylons import request, response, session, url, tmpl_context as c
6 from pylons.controllers.util import abort, redirect
7 import webhelpers.paginate as paginate
9 from wradmin.lib.base import BaseController, render
10 import wradmin.model as model
11 import sqlalchemy as sa
14 from wradmin.lib.mediawiki import wikipage_to_wrsleddingcache, unicode_e
16 from weberror import collector # Prevent a bug of weberror # See https://bitbucket.org/bbangert/weberror/issue/3/nameerror-global-name-fallback_encoding-is-not
17 collector.FALLBACK_ENCODING = 'utf-8' # --"--
19 log = logging.getLogger(__name__)
21 class RodelbahnController(BaseController):
24 return render('index.html')
28 "Lists all sledding routes"
29 q = model.meta.Session.query(model.WrSleddingCache)
30 q = q.order_by(model.WrSleddingCache.page_title)
31 c.paginator = paginate.Page(q, page=int(request.params.get('page', 1)), items_per_page = 25)
32 return render('rodelbahn_list.html')
36 "Displays a sledding route"
37 q = model.meta.Session.query(model.WrSleddingCache)
38 c.sledding = q.get(id)
39 if c.sledding is None: abort(404)
40 q = model.meta.Session.query(model.WrReport)
41 q = q.filter_by(page_id=id).order_by(sa.sql.expression.desc(model.WrReport.id))
42 c.paginator = paginate.Page(q, page=int(request.params.get('page', 1)), items_per_page = 25)
43 return render('rodelbahn_view.html')
46 # def _wikipage_to_wrsleddingcache(self, sledding_wiki):
47 # "Converts a sledding route wiki page to a sledding route wrsleddingcache database record."
48 # # TODO: Use mediawiki.wikipage_to_wrsleddingcache
51 # sl = model.WrSleddingCache()
52 # sl.page_id = sledding_wiki.page_id
53 # sl.page_title = to_title(sledding_wiki.page_title)
55 # # Match Rodelbahnbox
56 # wikitext = sledding_wiki.old_text
57 # regexp = re.compile(u"\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL)
58 # match = regexp.search(wikitext)
60 # raise Exception(u"No 'Rodelbahnbox' found")
61 # box = match.group(1)
63 # # Process Rodelbahnbox
64 # for property in box.split('|'):
65 # property = property.strip()
66 # if property == u'Rodelbahnbox': continue
67 # key_value = property.split('=')
68 # if len(key_value) != 2:
69 # raise Exception(u"Property '%s' has unexpected format" % key_value)
70 # key = key_value[0].strip()
71 # value = key_value[1].strip()
72 # if key == u'Rodelbahnnummer': pass
73 # elif key == u'Länge': sl.length = conv(to_unsigned, value, u'Länge')
74 # elif key == u'Gehzeit': sl.walktime = conv(to_unsigned, value, u'Gehzeit')
75 # elif key == u'Höhe oben': sl.height_top = conv(to_unsigned, value, u'Höhe oben')
76 # elif key == u'Höhe unten': sl.height_bottom = conv(to_unsigned, value, u'Höhe unten')
77 # elif key == u'Aufstieg getrennt': sl.walkup_separate = conv(to_bool, value, u'Aufstieg getrennt')
78 # elif key == u'Lift': sl.lift = conv(to_bool, value, u'Lift')
79 # elif key == u'Beleuchtung': sl.night_light = conv(to_bool, value, u'Beleuchtung')
80 # elif key == u'Rodelverleih': sl.sledge_rental = conv(to_bool, value, u'Rodelverleih')
81 # elif key == u'Öffentliche Anreise': sl.public_transport = conv(to_bool, value, u'Öffentliche Anreise')
82 # elif key == u'Bild': sl.image = value
83 # elif key == u'Position': (sl.position_latitude, sl.position_longitude) = conv(to_geo, value, u'Position') # '47.583333 N 15.75 E'
84 # elif key == u'Auskunft': sl.information = conv(to_phone_info, value, u'Auskunft')
85 # elif key == u'In Übersichtskarte': sl.show_in_overview = conv(to_bool, value, u'In Übersichtskarte')
86 # elif key == u'Aufnahmedatum': sl.creation_date = conv(to_date, value, u'Aufnahmedatum') # '2006-03-15'
87 # elif key == u'Lawinengefahr':
88 # if not value in [u'kaum', u'selten', u'gelegentlich', u'häufig']: raise formencode.Invalid(u"No valid value for 'Lawinengefahr': '%s'" % value, value, None)
89 # else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Rodelbahnbox: '%s' (mit Wert '%s')" % (key, value), value, None)
90 # sl.under_construction = None
92 # # Match Forumlink (e.g. {{Forumlink|68}})
93 # match = re.search(u"\{\{Forumlink\|(\d+)\}\}", wikitext)
94 # if match: sl.forum_id = match.group(1)
100 "Updates the wrsleddingcache table from the wiki"
101 from wradmin.model import page_table as page, wrsleddingcache_table as wrsleddingcache, categorylinks_table as categorylinks, revision_table as revision, text_table as text
102 from sqlalchemy.sql import select
103 c = model.meta.Session.connection()
105 # As MySQL does not support transactions we make a dry run first and only continue if there is no fatal error
107 # Query all sledding routes
108 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'))
109 sledding_pages = c.execute(q)
111 # 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"
113 # Delete all existing entries in wrsleddingcache
114 if i == 1: c.execute(wrsleddingcache.delete())
116 # Refill wrsleddingcache table
118 for sl in sledding_pages:
120 sl = wikipage_to_wrsleddingcache(sl.page_id, sl.page_title, sl.old_text)
121 sl.under_construction = c.execute(select([categorylinks], (categorylinks.c.cl_from==sl.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
122 if i == 1: model.meta.Session.add(sl)
123 except (RuntimeError, formencode.Invalid) as e: error_msg = u"Fehler bei Rodelbahn '%s': " % sl.page_title + unicode_e(e)
124 if i == 1: model.meta.Session.commit()
126 # Redirect to result page
127 if error_msg == '': session['flash'] = u'Die Rodelbahnliste wurde erfolgreich aktualisiert.'
128 else: session['flash'] = error_msg
130 return redirect(url(controller='rodelbahn', action='list'))