From 073676799f1b6cf73be5d133fe991a2ef8f3428d Mon Sep 17 00:00:00 2001 From: crimsen Date: Mon, 14 Jan 2019 00:25:25 +0100 Subject: [PATCH] =?UTF-8?q?Datenbank=20hinzugef=C3=BCgt,=20Preisen=20werde?= =?UTF-8?q?n=20eingetragen,=20Statusmeldungen=20werden=20gezeigt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geruecht/__init__.py | 3 +- geruecht/model.py | 34 ++++++++++++- geruecht/routes.py | 87 +++++++++++++++++++++----------- geruecht/static/js/scriptfile.js | 0 geruecht/static/master.css | 8 +++ geruecht/templates/home.html | 28 +++------- geruecht/templates/layout.html | 14 +++-- 7 files changed, 120 insertions(+), 54 deletions(-) delete mode 100644 geruecht/static/js/scriptfile.js diff --git a/geruecht/__init__.py b/geruecht/__init__.py index 5900adf..0948279 100644 --- a/geruecht/__init__.py +++ b/geruecht/__init__.py @@ -1,4 +1,4 @@ -from flask import Flask +from flask import Flask, g from flask_sqlalchemy import SQLAlchemy @@ -7,4 +7,5 @@ app.config['SECRET_KEY'] = '0a657b97ef546da90b2db91862ad4e29' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' db = SQLAlchemy(app) + from geruecht import routes \ No newline at end of file diff --git a/geruecht/model.py b/geruecht/model.py index 0ca57b2..79ca874 100644 --- a/geruecht/model.py +++ b/geruecht/model.py @@ -1,5 +1,5 @@ from geruecht import db - +from threading import Timer class User(db.Model): id = db.Column(db.Integer, primary_key=True) @@ -8,6 +8,38 @@ class User(db.Model): def add(self, value): self.sum += value + def storner(self, value): + self.sum = self.sum - value def __repr__(self): return f"User('{self.username}', '{self.sum}')" + + +class History: + history = [] + user = None + + def __init__(self, history, user, value): + self.history=history + self.history.append(self) + self.user = user + self.value = value + self.timer = Timer(60, self.kill) + self.timer.start() + + def exec(self): + self.timer.cancel() + User.query.filter_by(username=self.user.username).first().storner(self.value) + self.kill() + + def kill(self): + print("{} deledet from history".format(self.user)) + self.history.remove(self) + print(self.history) +''' + def __repr__(self): + print(f'self:{self}, history:{self.history}, user:{self.user}, value:{self.value}') + + def __str__(self): + print(f'self:{self}, history:{self.history}, user:{self.user}, value:{self.value}') +''' \ No newline at end of file diff --git a/geruecht/routes.py b/geruecht/routes.py index 96d5932..53a56bc 100644 --- a/geruecht/routes.py +++ b/geruecht/routes.py @@ -1,27 +1,66 @@ -from flask import render_template, url_for, redirect, flash, jsonify +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 +from geruecht.model import User, History +import flask_sijax +import os, sys -users = [ - { - 'name': 'Corey Schafer', - 'sum': '20€' - }, - { - 'name': 'Tim Gröger', - 'sum': '10€' - }, - { - 'name': 'Franziska Bobbe', - 'sum': '5,80€' - } -] +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 = [] -@app.route("/", methods=['GET', 'POST']) -@app.route("/home", methods=['GET', 'POST']) +@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) @@ -31,16 +70,6 @@ def home(): return redirect(url_for('home')) return render_template('home.html', users=User.query.all(), form=form) - -@app.route("//add-", methods=['POST']) -def add(username, value): - user = User.query.filter_by(username=username).first() - user.add(value) - db.session.commit() - flash(f'{username} wurde {value}€ hinzugefügt', 'success') - return jsonify({'data': user.sum}) - - -@app.route("/about") +@flask_sijax.route(app, "/about") def about(): return render_template('about.html', title='about') diff --git a/geruecht/static/js/scriptfile.js b/geruecht/static/js/scriptfile.js deleted file mode 100644 index e69de29..0000000 diff --git a/geruecht/static/master.css b/geruecht/static/master.css index 819da71..4a73801 100644 --- a/geruecht/static/master.css +++ b/geruecht/static/master.css @@ -36,6 +36,10 @@ body { color: black; } +.blue-button:active { + transform: translateY(4px); +} + .first { float: left;; margin-left: 10%; @@ -81,6 +85,10 @@ body { color: white; } +.button:active { + transform: translateY(4px); +} + .bottombar { background-color: #077BCB; overflow: hidden; diff --git a/geruecht/templates/home.html b/geruecht/templates/home.html index 5efac90..d43fafe 100644 --- a/geruecht/templates/home.html +++ b/geruecht/templates/home.html @@ -5,40 +5,28 @@

{{ user.username }}

- -
+
+

+1 €

-
+

+0,5 €

-
+

+0,4 €

-
+

+0,2 €

-
+

+0,1 €

-

{{ user.sum }}

+

{{ "%.2f"%user.sum }} €

- - {% endfor %} {% endblock %} diff --git a/geruecht/templates/layout.html b/geruecht/templates/layout.html index 3b957d7..8f41c42 100644 --- a/geruecht/templates/layout.html +++ b/geruecht/templates/layout.html @@ -13,6 +13,13 @@ + + + +
@@ -42,7 +49,9 @@ {% endfor %} {% endif %} {% endwith %} +
+ {% if form %}
{{ form.hidden_tag() }}
@@ -56,14 +65,13 @@ {{ form.submit(class="blue-button no-mg") }}
+ {% endif %}
-
+

Stornieren

-