]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/wradmin/controllers/gasthaus.py
7127ebdde33e1e8600ee1f2daeff6bdde808ed94
[philipp/winterrodeln/wradmin.git] / wradmin / wradmin / controllers / gasthaus.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_unsigned, to_date, to_geo, to_title, to_tristate, to_email, to_url, to_phone, conv, unicode_e
15
16 log = logging.getLogger(__name__)
17
18 class GasthausController(BaseController):
19
20     def list(self):
21         "Lists all inns"
22         q = model.meta.Session.query(model.WrInnCache)
23         q = q.order_by(model.WrInnCache.page_title)
24         c.paginator = paginate.Page(q, page=int(request.params.get('page', 1)), items_per_page = 25)
25         return render('gasthaus_list.html')
26     
27     
28     def view(self, id):
29         "Displays an inn"
30         q = model.meta.Session.query(model.WrInnCache)
31         c.inn =  q.get(id)
32         if c.inn is None: abort(404)
33         return render('gasthaus_view.html')
34     
35     
36     def _wikipage_to_wrinncache(self, inn_wiki):
37         "Converts an inn wiki page to an inn wrinncache database record."
38         inn = model.WrInnCache()
39         inn.page_id = inn_wiki.page_id
40         inn.page_title = to_title(inn_wiki.page_title)
41         
42         # Match Gasthausbox
43         wikitext = inn_wiki.old_text
44         regexp = re.compile(u"\{\{(Gasthausbox[^\}]*)\}\}", re.DOTALL)
45         match = regexp.search(wikitext)
46         if not match:
47             raise Exception(u"No 'Gasthausbox' found")
48         box = match.group(1)
49         
50         # Process Gashausbox
51         for property in box.split('|'):
52             property = property.strip()
53             if property == u'Gasthausbox': continue
54             key_value = property.split('=')
55             if len(key_value) != 2:
56                 raise Exception(u"Property '%s' has unexpected format" % key_value)
57             key = key_value[0].strip()
58             value = key_value[1].strip()
59             if key == u'Gasthausnummer': pass
60             elif key == u'E-Mail': inn.email = conv(to_email, value, u'E-Mail')
61             elif key == u'Homepage': inn.homepage = conv(to_url, value, u'Homepage')
62             elif key == u'Höhe': inn.height = conv(to_unsigned, value, u'Höhe')
63             elif key == u'Bild': inn.image = value
64             elif key == u'Position': (inn.position_latitude, inn.position_longitude) = conv(to_geo, value, u'Position') # '47.583333 N 15.75 E'
65             elif key == u'Telefon (Festnetz)': inn.phone = conv(to_phone, value, u'Telefon (Festnetz)')
66             elif key == u'Telefon (Mobil)': inn.mobile_phone = conv(to_phone, value, u'Telefon (Mobil)')
67             elif key == u'Rauchfrei': (inn.nonsmoker_area, inn.smoker_area) = conv(to_tristate, value, u'Rauchfrei')
68             elif key == u'Aufnahmedatum': inn.creation_date = conv(to_date, value, u'Aufnahmedatum') # '2006-03-15'
69             else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Gasthausbox: '%s' (mit Wert '%s')" % (key, value), value, None)
70         inn.under_construction = None
71         return inn
72     
73     
74     def update(self):
75         "Updates the wrinncache table from the wiki"
76         from wradmin.model import page_table as page, wrinncache_table as wrinncache, categorylinks_table as categorylinks, revision_table as revision, text_table as text
77         from sqlalchemy.sql import select
78         c = model.meta.Session.connection()
79         
80         # Query all inns
81         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'Gasthaus'))
82         inn_pages = c.execute(q)
83         
84         # Delete all existing entries in wrinncache
85         c.execute(wrinncache.delete())
86         
87         # Refill wrinncache table
88         error_msg = ''
89         for inn in inn_pages:
90             try: 
91                 inn = self._wikipage_to_wrinncache(inn)
92                 inn.under_construction = c.execute(select([categorylinks], (categorylinks.c.cl_from==inn.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
93                 model.meta.Session.add(inn)
94             except formencode.Invalid, e: error_msg = u"Fehler bei Gasthaus '%s': " % inn.page_title + unicode_e(e)
95         model.meta.Session.commit()
96         
97         # Redirect to result page
98         if error_msg == '': session['flash'] = u'Die Gasthausliste wurde erfolgreich aktualisiert.'
99         else: session['flash'] = error_msg
100         session.save()
101         return redirect_to(controller='gasthaus', action='list')