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