1 from flask import g, render_template, request, redirect, url_for, flash, \
3 from flask_login import current_user, login_required, login_user, logout_user
4 from flask_principal import RoleNeed, identity_changed, identity_loaded, Identity, \
5 AnonymousIdentity, UserNeed
8 import wradmin.template_helper
9 from wradmin.app import app, db, admin_permission, login_manager
10 from wradmin.auth import password_is_correct
11 from wradmin.auth.forms import LoginForm
12 from wradmin.controllers.bericht import BerichtController
13 from wradmin.controllers.coordtool import CoordtoolController
14 from wradmin.controllers.gasthaus import GasthausController
15 from wradmin.controllers.rodelbahn import RodelbahnController
19 def _before_request():
25 return render_template('index.html')
28 @app.route("/rodelbahn/list")
31 return RodelbahnController().list()
34 @app.route("/rodelbahn/view/<int:id>")
36 @admin_permission.require(403)
37 def rodelbahn_view(id):
38 return RodelbahnController().view(id)
41 @app.route("/rodelbahn/json/edit/<int:id>")
43 @admin_permission.require(403)
44 def rodelbahn_json_edit(id):
45 return RodelbahnController().json_edit(id)
48 @app.route("/rodelbahn/update")
50 def rodelbahn_update():
51 return RodelbahnController().update()
54 @app.route("/rodelbahn/update_regioncache")
56 def rodelbahn_update_regioncache():
57 return RodelbahnController().update_regioncache()
60 @app.route("/rodelbahn/update_mapcache")
62 def rodelbahn_update_mapcache():
63 return RodelbahnController().update_mapcache()
66 @app.route("/bericht/list")
68 @admin_permission.require(403)
70 return BerichtController().list()
73 @app.route("/bericht/view/<int:id>")
75 @admin_permission.require(403)
77 return BerichtController().view(id)
80 @app.route("/bericht/change_date_invalid/<int:id>", methods=['POST'])
82 @admin_permission.require(403)
83 def bericht_change_date_invalid(id):
84 return BerichtController().change_date_invalid(id)
87 @app.route("/bericht/update_reportcache")
89 def bericht_update_reportcache():
90 return BerichtController().update_reportcache()
93 @app.route("/gasthaus/list")
96 return GasthausController().list()
99 @app.route("/gasthaus/view/<int:id>")
101 def gasthaus_view(id):
102 return GasthausController().view(id)
105 @app.route("/gasthaus/update")
107 def gasthaus_update():
108 return GasthausController().update()
111 @app.route("/coordtool/index")
113 def coordtool_index():
114 return CoordtoolController().index()
117 @app.route("/coordtool/convert", methods=['POST'])
119 def coordtool_convert():
120 return CoordtoolController().convert()
123 @app.route("/login", methods=['GET', 'POST'])
126 if form.validate_on_submit():
127 user = db.session.query(wradmin.model.MwUser).filter_by(user_name=form.user_name.data).first()
128 if user is not None and password_is_correct(form.password.data, user.user_password.decode()):
129 login_user(user, form.remember_me.data)
130 identity_changed.send(current_app._get_current_object(), identity=Identity(user.get_id()))
131 next = request.args.get('next')
132 if next is None or not next.startswith('/'):
133 next = url_for('index')
134 flash('Sie sind nun angemeldet.')
135 return redirect(next)
136 flash('Ungülter Benutzername oder ungültiges Passwort.')
137 return render_template('auth/login.html', form=form)
140 @app.route("/logout")
143 for key in ('identity.name', 'identity.auth_type'):
144 session.pop(key, None)
145 identity_changed.send(current_app._get_current_object(), identity=AnonymousIdentity())
146 flash('Sie wurden ausgeloggt.')
147 return redirect(url_for('index'))
150 @login_manager.user_loader
151 def user_loader(user_id):
152 return wradmin.db.session.query(wradmin.model.MwUser).get(user_id)
155 @identity_loaded.connect_via(app)
156 def on_identity_loaded(sender, identity):
157 identity.user = current_user
158 user_id = current_user.get_id()
159 if user_id is not None:
160 identity.provides.add(UserNeed(user_id))
161 if wradmin.db.session.query(wradmin.model.MwUserGroups).get((user_id, 'sysop')) is not None:
162 identity.provides.add(RoleNeed('admin'))