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/view_wikitext/<int:id>")
42 def rodelbahn_view_wikitext(id):
43 return RodelbahnController().view_wikitext(id)
46 @app.route("/rodelbahn/json/edit/<int:id>")
48 @admin_permission.require(403)
49 def rodelbahn_json_edit(id):
50 return RodelbahnController().json_edit(id)
53 @app.route("/rodelbahn/update")
55 def rodelbahn_update():
56 return RodelbahnController().update()
59 @app.route("/rodelbahn/update_regioncache")
61 def rodelbahn_update_regioncache():
62 return RodelbahnController().update_regioncache()
65 @app.route("/rodelbahn/update_mapcache")
67 def rodelbahn_update_mapcache():
68 return RodelbahnController().update_mapcache()
71 @app.route("/bericht/list")
73 @admin_permission.require(403)
75 return BerichtController().list()
78 @app.route("/bericht/view/<int:id>")
80 @admin_permission.require(403)
82 return BerichtController().view(id)
85 @app.route("/bericht/change_date_invalid/<int:id>", methods=['POST'])
87 @admin_permission.require(403)
88 def bericht_change_date_invalid(id):
89 return BerichtController().change_date_invalid(id)
92 @app.route("/bericht/update_reportcache")
94 def bericht_update_reportcache():
95 return BerichtController().update_reportcache()
98 @app.route("/gasthaus/list")
101 return GasthausController().list()
104 @app.route("/gasthaus/view/<int:id>")
106 def gasthaus_view(id):
107 return GasthausController().view(id)
110 @app.route("/gasthaus/update")
112 def gasthaus_update():
113 return GasthausController().update()
116 @app.route("/coordtool/index")
118 def coordtool_index():
119 return CoordtoolController().index()
122 @app.route("/coordtool/convert", methods=['POST'])
124 def coordtool_convert():
125 return CoordtoolController().convert()
128 @app.route("/login", methods=['GET', 'POST'])
131 if form.validate_on_submit():
132 user = db.session.query(wradmin.model.MwUser).filter_by(user_name=form.user_name.data).first()
133 if user is not None and password_is_correct(form.password.data, user.user_password.decode()):
134 login_user(user, form.remember_me.data)
135 identity_changed.send(current_app._get_current_object(), identity=Identity(user.get_id()))
136 next = request.args.get('next')
137 if next is None or not next.startswith('/'):
138 next = url_for('index')
139 flash('Sie sind nun angemeldet.')
140 return redirect(next)
141 flash('Ungülter Benutzername oder ungültiges Passwort.')
142 return render_template('auth/login.html', form=form)
145 @app.route("/logout")
148 for key in ('identity.name', 'identity.auth_type'):
149 session.pop(key, None)
150 identity_changed.send(current_app._get_current_object(), identity=AnonymousIdentity())
151 flash('Sie wurden ausgeloggt.')
152 return redirect(url_for('index'))
155 @login_manager.user_loader
156 def user_loader(user_id):
157 return wradmin.db.session.query(wradmin.model.MwUser).get(user_id)
160 @identity_loaded.connect_via(app)
161 def on_identity_loaded(sender, identity):
162 identity.user = current_user
163 user_id = current_user.get_id()
164 if user_id is not None:
165 identity.provides.add(UserNeed(user_id))
166 if wradmin.db.session.query(wradmin.model.MwUserGroups).get((user_id, 'sysop')) is not None:
167 identity.provides.add(RoleNeed('admin'))