from flask import render_template, url_for, redirect, flash, jsonify, g, request from flask_login import login_user, current_user, logout_user, login_required from geruecht import app, db, bcrypt from geruecht.forms import RegistrationForm, FinanzerLogin from geruecht.model import User, History, Finanzer import flask_sijax import os, sys path = os.path.join('.', os.path.dirname(__file__), '../') sys.path.append(path) # The path where you want the extension to create the needed javascript files # DON'T put any of your files in this directory, because they'll be deleted! app.config["SIJAX_STATIC_PATH"] = os.path.join('.', os.path.dirname(__file__), 'static/js/sijax/') # You need to point Sijax to the json2.js library if you want to support # browsers that don't support JSON natively (like IE <= 7) app.config["SIJAX_JSON_URI"] = '/static/js/sijax/json2.js' flask_sijax.Sijax(app) history = [] @flask_sijax.route(app, "/", methods=['GET', 'POST']) @flask_sijax.route(app, "/home", methods=['GET', 'POST']) def home(): def add(obj_response, username, value): user_2 = User.query.filter_by(username=username).first() print(user_2) print(user_2.id) user_2.add(value) print(user_2) db.session.commit() print(obj_response, username, value) History(history=history, user=user_2, value=value) print(history) obj_response.html('div#flash', f'{user_2.username} wurde {"%.2f"%value} € hinzugefügt') obj_response.attr('div#flash', "class", "alert alert-success") obj_response.html(f'#{user_2.id}-sum', str("%.2f" % user_2.getsum() + " €")) def storner(obj_response): try: obj = history[len(history)-1] user_2 = User.query.filter_by(username=obj.user.username).first() print("{} {}".format(obj.user, obj.value)) obj.exec() print(history) print(user_2) db.session.commit() obj_response.html('div#flash', f'{"%.2f"%obj.value} wurden von {user_2.username} storniert') obj_response.attr('div#flash', "class", "alert alert-success") obj_response.html(f'#{user_2.id}-sum', str("%.2f" % user_2.getsum() + " €")) except IndexError: print("history: {} is empty".format(history)) obj_response.html('div#flash', "Der Timer ist abgelaufen!! Die Stornierliste ist leer!! Falls es was wichtiges ist, melde dich beim Finanzer oder Administrator!!") obj_response.attr('div#flash', "class", "alert alert-error") if g.sijax.is_sijax_request: g.sijax.register_callback('add', add) g.sijax.register_callback('storner', storner) return g.sijax.process_request() form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data) db.session.add(user) db.session.commit() flash(f'Person wurde angelegt: {form.username.data}', 'success') return redirect(url_for('home')) return render_template('home.html', users=User.query.all(), form=form) @flask_sijax.route(app, "/return") def to_home(): return home() @app.route("/login", methods=['GET', 'POST']) def login(): if current_user.is_authenticated: return redirect(url_for('home')) login_form = FinanzerLogin() if login_form.validate_on_submit(): user = Finanzer.query.filter_by(username=login_form.username.data).first() if user: if user and bcrypt.check_password_hash(user.password, login_form.password.data): login_user(user) next_page = request.args.get('next') return redirect(next_page) if next_page else redirect(url_for('home')) else: flash('Passwort ist falsch', 'error') else: flash('nur der finanzer kann sich einloggen !!', 'error') return render_template('login.html', title='Login', form=login_form) @app.route("/logout") def logout(): logout_user() return redirect(url_for('home')) @flask_sijax.route(app, "/uebersicht") @login_required def finanzen(): def supply(obj_response, arg1, arg2): list = zip(arg1, arg2) try: for value in arg2: if value: float(value) for user_id, value in list: if user_id and value: user_ = User.query.get(user_id) user_.add_sub(float(value)) db.session.commit() obj_response.attr(f'#{user_id}-input', 'value', '') obj_response.html('div#flash', "Alle Werte wurden übernommen") obj_response.attr('div#flash', 'class', "alert alert-success") for user_ in User.query.all(): obj_response.html(f'p#{user_.id}-jan-sub', "%.2f"%user_.jan_sub + " €") obj_response.html(f'p#{user_.id}-feb-sub', "%.2f"%user_.feb_sub + " €") obj_response.html(f'p#{user_.id}-maer-sub', "%.2f"%user_.maer_sub + " €") obj_response.html(f'p#{user_.id}-apr-sub', "%.2f"%user_.apr_sub + " €") obj_response.html(f'p#{user_.id}-mai-sub', "%.2f"%user_.mai_sub + " €") obj_response.html(f'p#{user_.id}-jun-sub', "%.2f"%user_.jun_sub + " €") obj_response.html(f'p#{user_.id}-jul-sub', "%.2f"%user_.jul_sub + " €") obj_response.html(f'p#{user_.id}-aug-sub', "%.2f"%user_.aug_sub + " €") obj_response.html(f'p#{user_.id}-sep-sub', "%.2f"%user_.sep_sub + " €") obj_response.html(f'p#{user_.id}-okt-sub', "%.2f"%user_.okt_sub + " €") obj_response.html(f'p#{user_.id}-nov-sub', "%.2f"%user_.nov_sub + " €") obj_response.html(f'p#{user_.id}-dez-sub', "%.2f"%user_.dez_sub + " €") obj_response.html(f'p#{user_.id}-sum', "%.2f"%user_.getsum() + " €") except ValueError: obj_response.html('div#flash', "Du hast irgendwo keine Zahl eingegeb!!") obj_response.attr('div#flash', "class", "alert alert-error") if g.sijax.is_sijax_request: g.sijax.register_callback('supply', supply) return g.sijax.process_request() return render_template('finanzer.html', users=User.query.all()) @flask_sijax.route(app, "/about") def about(): return render_template('about.html', title='about')