]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/wradmin/controllers/rodelbahn.py
4a5ffb1d951d45f0d62020335bf6c561999d0fb9
[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 wikipage_to_wrsleddingcache, 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
24     def list(self):
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')
30     
31     
32     def view(self, id):
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')
41     
42     
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
46     #     
47     #     
48     #     sl = model.WrSleddingCache()
49     #     sl.page_id = sledding_wiki.page_id
50     #     sl.page_title = to_title(sledding_wiki.page_title)
51     #     
52     #     # Match Rodelbahnbox
53     #     wikitext = sledding_wiki.old_text
54     #     regexp = re.compile(u"\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL)
55     #     match = regexp.search(wikitext)
56     #     if not match:
57     #         raise Exception(u"No 'Rodelbahnbox' found")
58     #     box = match.group(1)
59     #     
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
88     #     
89     #     # Match Forumlink (e.g. {{Forumlink|68}})
90     #     match = re.search(u"\{\{Forumlink\|(\d+)\}\}", wikitext)
91     #     if match: sl.forum_id = match.group(1)
92     #     
93     #     return sl
94     
95     
96     def update(self):
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()
101         
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)
105         # Original SQL:
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"
107         
108         # Delete all existing entries in wrsleddingcache
109         c.execute(wrsleddingcache.delete())
110         
111         # Refill wrsleddingcache table
112         error_msg = ''
113         for sl in sledding_pages:
114             try: 
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()
120         
121         # Redirect to result page
122         if error_msg == '': session['flash'] = u'Die Rodelbahnliste wurde erfolgreich aktualisiert.'
123         else: session['flash'] = error_msg
124         session.save()
125         return redirect_to(controller='rodelbahn', action='list')