flaschengeist/geruecht/routes.py

80 lines
3.1 KiB
Python

from flask import render_template, url_for, redirect, flash, jsonify, g
from geruecht import app, db
from geruecht.forms import RegistrationForm
from geruecht.model import User, History
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.sum + ""))
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.sum + ""))
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()
@flask_sijax.route(app, "/about")
def about():
return render_template('about.html', title='about')