]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/__init__.py
ba056c1ec8f5d4f21d6b6a840e95af579469c270
[philipp/winterrodeln/wradmin.git] / wradmin / __init__.py
1 from flask import Flask, send_from_directory, abort, g, render_template, request, redirect, url_for, flash
2 from sqlalchemy.engine import create_engine
3 import wradmin.model
4 import wradmin.template_helper
5 from wradmin.controllers.rodelbahn import RodelbahnController
6 from wradmin.controllers.gasthaus import GasthausController
7 from wradmin.controllers.bericht import BerichtController
8 from wradmin.controllers.coordtool import CoordtoolController
9 from wradmin.auth import password_is_correct
10 from wradmin.auth.forms import LoginForm
11 from flask_login import LoginManager, current_user, login_required, login_user, logout_user
12
13
14 app = Flask(__name__)
15 app.config.from_envvar('WRADMIN_SETTINGS')
16 wradmin.model.init_model(create_engine(app.config['DATABASE_URI']))
17 app.jinja_env.globals.update(h=wradmin.template_helper.PylonsHelper())
18 login_manager = LoginManager(app)
19 login_manager.login_view = "login"
20
21
22 @app.before_request
23 def _before_request():
24     g.user = current_user
25
26
27 @app.teardown_appcontext
28 def remove_db_session(error):
29     """Removes the database session at the end of the request."""
30     wradmin.model.meta.Session.remove()
31
32
33 @app.route("/")
34 def index():
35     return render_template('index.html')
36
37
38 @app.route("/rodelbahn/list")
39 def rodelbahn_list():
40     return RodelbahnController().list()
41
42
43 @app.route("/rodelbahn/view/<int:id>")
44 def rodelbahn_view(id):
45     return RodelbahnController().view(id)
46
47
48 @app.route("/rodelbahn/update")
49 def rodelbahn_update():
50     return RodelbahnController().update()
51
52
53 @app.route("/rodelbahn/update_regioncache")
54 def rodelbahn_update_regioncache():
55     return RodelbahnController().update_regioncache()
56
57
58 @app.route("/bericht/list")
59 def bericht_list():
60     return BerichtController().list()
61
62
63 @app.route("/bericht/view/<int:id>")
64 def bericht_view(id):
65     return BerichtController().view(id)
66
67
68 @app.route("/bericht/change_date_invalid/<int:id>", methods=['POST'])
69 def bericht_change_date_invalid(id):
70     return BerichtController().change_date_invalid(id)
71
72
73 @app.route("/gasthaus/list")
74 def gasthaus_list():
75     return GasthausController().list()
76
77
78 @app.route("/gasthaus/view/<int:id>")
79 def gasthaus_view(id):
80     return GasthausController().view(id)
81
82
83 @app.route("/gasthaus/update")
84 def gasthaus_update():
85     return GasthausController().update()
86
87
88 @app.route("/coordtool/index")
89 def coordtool_index():
90     return CoordtoolController().index()
91
92
93 @app.route("/coordtool/convert", methods=['POST'])
94 def coordtool_convert():
95     return CoordtoolController().convert()
96
97
98 @app.route("/login", methods=['GET', 'POST'])
99 def login():
100     form = LoginForm()
101     if form.validate_on_submit():
102         user = wradmin.model.meta.Session.query(wradmin.model.MwUser).filter_by(user_name=form.user_name.data).first()
103         if user is not None and password_is_correct(form.password.data, user.user_password.decode()):
104             login_user(user, form.remember_me.data)
105             next = request.args.get('next')
106             if next is None or not next.startswith('/'):
107                 next = url_for('index')
108             flash('Sie sind nun angemeldet.')
109             return redirect(next)
110         flash('Ungülter Benutzername oder ungültiges Passwort.')
111     return render_template('auth/login.html', form=form)
112
113
114 @app.route("/logout")
115 def logout():
116     logout_user()
117     flash('Sie wurden ausgeloggt.')
118     return redirect(url_for('index'))
119
120
121 @login_manager.user_loader
122 def user_loader(user_id):
123     return wradmin.model.meta.Session.query(wradmin.model.MwUser).get(user_id)