]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/controllers/gasthaus.py
95faba71240d183e55bccbffd63633d3fd206961
[philipp/winterrodeln/wradmin.git] / wradmin / controllers / gasthaus.py
1 #!/usr/bin/python3.4
2 from flask import request, abort, redirect, url_for, flash, render_template
3 import paginate
4
5 import wrpylib.wrmwmarkup
6 import wrpylib.wrmwcache
7 import wrpylib.mwmarkup
8
9 import wradmin.model as model
10
11
12 class GasthausController:
13
14     def list(self):
15         """Lists all inns"""
16         q = model.meta.Session.query(model.WrInnCache)
17         q = q.order_by(model.WrInnCache.page_title)
18         paginator = paginate.Page(q.all(), page=int(request.args.get('page', 1)),
19                                   url_maker=lambda page: url_for('gasthaus_list', page=page),
20                                   items_per_page=25)
21         return render_template('gasthaus_list.html', paginator=paginator)
22
23     def view(self, id):
24         """Displays an inn"""
25         q = model.meta.Session.query(model.WrInnCache)
26         inn = q.get(id)
27         if inn is None:
28             abort(404)
29         return render_template('gasthaus_view.html', inn=inn)
30
31     def update(self):
32         """Updates the wrinncache table from the wiki"""
33         c = model.meta.Session.connection()
34         try:
35             wrpylib.wrmwcache.update_wrinncache(c)
36             model.meta.Session.commit()
37             flash('Die Gasthausliste wurde erfolgreich aktualisiert.', 'info')
38         except wrpylib.wrmwcache.UpdateCacheError as e:
39             title = str(e.args[1])
40             title = wrpylib.mwmarkup.dbkey_to_title(title)
41             msg = str(e.args[2])
42             msg = msg.replace('\n', '; ')
43             if len(e.args) == 3:
44                 flash("Fehler bei Gasthaus '{0}': {1}".format(title, msg), 'error')
45             else: flash(str(e), 'error')
46         # Redirect to result page
47         return redirect(url_for('gasthaus_list'))