X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/wradmin.git/blobdiff_plain/74b9844d14da11dfd0bdedfe08d3693d88d05fd1..d126c9a8fd9fac41d26dc98df5ed4883d8ee2ffb:/wradmin/__init__.py diff --git a/wradmin/__init__.py b/wradmin/__init__.py index f61da7b..c80fc17 100644 --- a/wradmin/__init__.py +++ b/wradmin/__init__.py @@ -1,52 +1,126 @@ -# http://flask.pocoo.org/ -# FLASK_APP=wradmin FLASK_DEBUG=1 WRADMIN_SETTINGS=development.cfg python3 -m flask run -# FLASK_APP=wradmin WRADMIN_SETTINGS=production.cfg python3 -m flask run -from flask import Flask, send_from_directory, abort, g +from flask import Flask, send_from_directory, abort, g, render_template, request, redirect, url_for, flash from sqlalchemy.engine import create_engine -from wradmin.genshi import render_genshi_template +import wradmin.model +import wradmin.template_helper +from wradmin.controllers.rodelbahn import RodelbahnController +from wradmin.controllers.gasthaus import GasthausController +from wradmin.controllers.bericht import BerichtController +from wradmin.controllers.coordtool import CoordtoolController +from wradmin.auth import password_is_correct +from wradmin.auth.forms import LoginForm +from flask_login import LoginManager, current_user, login_required, login_user, logout_user app = Flask(__name__) app.config.from_envvar('WRADMIN_SETTINGS') +wradmin.model.init_model(create_engine(app.config['DATABASE_URI'])) +app.jinja_env.globals.update(h=wradmin.template_helper.PylonsHelper()) +login_manager = LoginManager(app) +login_manager.login_view = "login" -def get_db(): - """Opens a new database connection if there is none yet for the - current application context. - """ - if not hasattr(g, 'db'): - g.db_engine = create_engine(app.config['DATABASE_URI']) - g.db = g.db_engine.connect() - return g.db +@app.before_request +def _before_request(): + g.user = current_user @app.teardown_appcontext -def close_db(error): - """Closes the database again at the end of the request.""" - if hasattr(g, 'db'): - g.db.close() +def remove_db_session(error): + """Removes the database session at the end of the request.""" + wradmin.model.meta.Session.remove() @app.route("/") def index(): - return render_genshi_template('index.html') + return render_template('index.html') @app.route("/rodelbahn/list") def rodelbahn_list(): - pass + return RodelbahnController().list() +@app.route("/rodelbahn/view/") +def rodelbahn_view(id): + return RodelbahnController().view(id) + + +@app.route("/rodelbahn/update") +def rodelbahn_update(): + return RodelbahnController().update() + + +@app.route("/rodelbahn/update_regioncache") +def rodelbahn_update_regioncache(): + return RodelbahnController().update_regioncache() + +@app.route("/rodelbahn/update_mapcache") +def rodelbahn_update_mapcache(): + return RodelbahnController().update_mapcache() + @app.route("/bericht/list") def bericht_list(): - pass + return BerichtController().list() + + +@app.route("/bericht/view/") +def bericht_view(id): + return BerichtController().view(id) + + +@app.route("/bericht/change_date_invalid/", methods=['POST']) +def bericht_change_date_invalid(id): + return BerichtController().change_date_invalid(id) @app.route("/gasthaus/list") def gasthaus_list(): - pass + return GasthausController().list() + + +@app.route("/gasthaus/view/") +def gasthaus_view(id): + return GasthausController().view(id) + + +@app.route("/gasthaus/update") +def gasthaus_update(): + return GasthausController().update() @app.route("/coordtool/index") def coordtool_index(): - pass + return CoordtoolController().index() + + +@app.route("/coordtool/convert", methods=['POST']) +def coordtool_convert(): + return CoordtoolController().convert() + + +@app.route("/login", methods=['GET', 'POST']) +def login(): + form = LoginForm() + if form.validate_on_submit(): + user = wradmin.model.meta.Session.query(wradmin.model.MwUser).filter_by(user_name=form.user_name.data).first() + if user is not None and password_is_correct(form.password.data, user.user_password.decode()): + login_user(user, form.remember_me.data) + next = request.args.get('next') + if next is None or not next.startswith('/'): + next = url_for('index') + flash('Sie sind nun angemeldet.') + return redirect(next) + flash('Ungülter Benutzername oder ungültiges Passwort.') + return render_template('auth/login.html', form=form) + + +@app.route("/logout") +def logout(): + logout_user() + flash('Sie wurden ausgeloggt.') + return redirect(url_for('index')) + + +@login_manager.user_loader +def user_loader(user_id): + return wradmin.model.meta.Session.query(wradmin.model.MwUser).get(user_id)