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 to_unsigned, to_date, to_geo, to_title, to_tristate, to_email, to_url, to_phone, conv, unicode_e
16 log = logging.getLogger(__name__)
18 class GasthausController(BaseController):
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')
30 q = model.meta.Session.query(model.WrInnCache)
32 if c.inn is None: abort(404)
33 return render('gasthaus_view.html')
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)
43 wikitext = inn_wiki.old_text
44 regexp = re.compile(u"\{\{(Gasthausbox[^\}]*)\}\}", re.DOTALL)
45 match = regexp.search(wikitext)
47 raise Exception(u"No 'Gasthausbox' found")
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
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()
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)
84 # Delete all existing entries in wrinncache
85 c.execute(wrinncache.delete())
87 # Refill wrinncache table
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()
97 # Redirect to result page
98 if error_msg == '': session['flash'] = u'Die Gasthausliste wurde erfolgreich aktualisiert.'
99 else: session['flash'] = error_msg
101 return redirect_to(controller='gasthaus', action='list')