2 # -*- coding: iso-8859-15 -*-
5 from pylons import request, response, session, tmpl_context as c
6 from pylons.controllers.util import abort, redirect_to
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 log = logging.getLogger(__name__)
18 class RodelbahnController(BaseController):
21 return render('index.html')
25 "Lists all sledding routes"
26 q = model.meta.Session.query(model.WrSleddingCache)
27 q = q.order_by(model.WrSleddingCache.page_title)
28 c.paginator = paginate.Page(q, page=int(request.params.get('page', 1)), items_per_page = 25)
29 return render('rodelbahn_list.html')
33 "Displays a sledding route"
34 q = model.meta.Session.query(model.WrSleddingCache)
35 c.sledding = q.get(id)
36 if c.sledding is None: abort(404)
37 q = model.meta.Session.query(model.WrReport)
38 q = q.filter_by(page_id=id).order_by(sa.sql.expression.desc(model.WrReport.id))
39 c.paginator = paginate.Page(q, page=int(request.params.get('page', 1)), items_per_page = 25)
40 return render('rodelbahn_view.html')
43 # def _wikipage_to_wrsleddingcache(self, sledding_wiki):
44 # "Converts a sledding route wiki page to a sledding route wrsleddingcache database record."
45 # # TODO: Use mediawiki.wikipage_to_wrsleddingcache
48 # sl = model.WrSleddingCache()
49 # sl.page_id = sledding_wiki.page_id
50 # sl.page_title = to_title(sledding_wiki.page_title)
52 # # Match Rodelbahnbox
53 # wikitext = sledding_wiki.old_text
54 # regexp = re.compile(u"\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL)
55 # match = regexp.search(wikitext)
57 # raise Exception(u"No 'Rodelbahnbox' found")
58 # box = match.group(1)
60 # # Process Rodelbahnbox
61 # for property in box.split('|'):
62 # property = property.strip()
63 # if property == u'Rodelbahnbox': continue
64 # key_value = property.split('=')
65 # if len(key_value) != 2:
66 # raise Exception(u"Property '%s' has unexpected format" % key_value)
67 # key = key_value[0].strip()
68 # value = key_value[1].strip()
69 # if key == u'Rodelbahnnummer': pass
70 # elif key == u'Länge': sl.length = conv(to_unsigned, value, u'Länge')
71 # elif key == u'Gehzeit': sl.walktime = conv(to_unsigned, value, u'Gehzeit')
72 # elif key == u'Höhe oben': sl.height_top = conv(to_unsigned, value, u'Höhe oben')
73 # elif key == u'Höhe unten': sl.height_bottom = conv(to_unsigned, value, u'Höhe unten')
74 # elif key == u'Aufstieg getrennt': sl.walkup_separate = conv(to_bool, value, u'Aufstieg getrennt')
75 # elif key == u'Lift': sl.lift = conv(to_bool, value, u'Lift')
76 # elif key == u'Beleuchtung': sl.night_light = conv(to_bool, value, u'Beleuchtung')
77 # elif key == u'Rodelverleih': sl.sledge_rental = conv(to_bool, value, u'Rodelverleih')
78 # elif key == u'Öffentliche Anreise': sl.public_transport = conv(to_bool, value, u'Öffentliche Anreise')
79 # elif key == u'Bild': sl.image = value
80 # elif key == u'Position': (sl.position_latitude, sl.position_longitude) = conv(to_geo, value, u'Position') # '47.583333 N 15.75 E'
81 # elif key == u'Auskunft': sl.information = conv(to_phone_info, value, u'Auskunft')
82 # elif key == u'In Übersichtskarte': sl.show_in_overview = conv(to_bool, value, u'In Übersichtskarte')
83 # elif key == u'Aufnahmedatum': sl.creation_date = conv(to_date, value, u'Aufnahmedatum') # '2006-03-15'
84 # elif key == u'Lawinengefahr':
85 # 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)
86 # else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Rodelbahnbox: '%s' (mit Wert '%s')" % (key, value), value, None)
87 # sl.under_construction = None
89 # # Match Forumlink (e.g. {{Forumlink|68}})
90 # match = re.search(u"\{\{Forumlink\|(\d+)\}\}", wikitext)
91 # if match: sl.forum_id = match.group(1)
97 "Updates the wrsleddingcache table from the wiki"
98 from wradmin.model import page_table as page, wrsleddingcache_table as wrsleddingcache, categorylinks_table as categorylinks, revision_table as revision, text_table as text
99 from sqlalchemy.sql import select
100 c = model.meta.Session.connection()
102 # Query all sledding routes
103 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'))
104 sledding_pages = c.execute(q)
106 # 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"
108 # Delete all existing entries in wrsleddingcache
109 c.execute(wrsleddingcache.delete())
111 # Refill wrsleddingcache table
113 for sl in sledding_pages:
115 sl = wikipage_to_wrsleddingcache(sl.page_id, sl.page_title, sl.old_text)
116 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
117 model.meta.Session.add(sl)
118 except formencode.Invalid, e: error_msg = u"Fehler bei Rodelbahn '%s': " % sl.page_title + unicode_e(e)
119 model.meta.Session.commit()
121 # Redirect to result page
122 if error_msg == '': session['flash'] = u'Die Rodelbahnliste wurde erfolgreich aktualisiert.'
123 else: session['flash'] = error_msg
125 return redirect_to(controller='rodelbahn', action='list')