]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/wradmin/controllers/rodelbahn.py
46dbba41d9cc795033950451b4d3e68aae7e5665
[philipp/winterrodeln/wradmin.git] / wradmin / wradmin / controllers / rodelbahn.py
1 #!/usr/bin/python2.6
2 # -*- coding: iso-8859-15 -*-
3 import logging
4
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
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 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' # --"--
18
19 log = logging.getLogger(__name__)
20
21 class RodelbahnController(BaseController):
22
23     def index(self):
24         return render('index.html')
25
26
27     def list(self):
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')
33     
34     
35     def view(self, id):
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')
44     
45     
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
49     #     
50     #     
51     #     sl = model.WrSleddingCache()
52     #     sl.page_id = sledding_wiki.page_id
53     #     sl.page_title = to_title(sledding_wiki.page_title)
54     #     
55     #     # Match Rodelbahnbox
56     #     wikitext = sledding_wiki.old_text
57     #     regexp = re.compile(u"\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL)
58     #     match = regexp.search(wikitext)
59     #     if not match:
60     #         raise Exception(u"No 'Rodelbahnbox' found")
61     #     box = match.group(1)
62     #     
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
91     #     
92     #     # Match Forumlink (e.g. {{Forumlink|68}})
93     #     match = re.search(u"\{\{Forumlink\|(\d+)\}\}", wikitext)
94     #     if match: sl.forum_id = match.group(1)
95     #     
96     #     return sl
97     
98     
99     def update(self):
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()
104         
105         # As MySQL does not support transactions we make a dry run first and only continue if there is no fatal error
106         for i in range(2):
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)
110             # Original SQL:
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"
112             
113             # Delete all existing entries in wrsleddingcache
114             if i == 1: c.execute(wrsleddingcache.delete())
115             
116             # Refill wrsleddingcache table
117             error_msg = u''
118             for sl in sledding_pages:
119                 try: 
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()
125         
126         # Redirect to result page
127         if error_msg == '': session['flash'] = u'Die Rodelbahnliste wurde erfolgreich aktualisiert.'
128         else: session['flash'] = error_msg
129         session.save()
130         return redirect(url(controller='rodelbahn', action='list'))