From 4648aa8b0ca5a8d8d8bfa5a9a25acf15f375b318 Mon Sep 17 00:00:00 2001 From: crimsen Date: Tue, 15 Jan 2019 00:25:42 +0100 Subject: [PATCH] =?UTF-8?q?login=20f=C3=BCr=20den=20finanzer=20=C3=BCbersi?= =?UTF-8?q?cht=20f=C3=BCr=20den=20finanzer=20finanzer=20kann=20ausgleiche?= =?UTF-8?q?=20hinzuf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: der Code sieht ekelhaft aus bis zum gehtnicht mehr ... vltt verbessern? --- geruecht/__init__.py | 6 + geruecht/forms.py | 9 +- geruecht/model.py | 310 ++++++++++++++++++++++++++++++- geruecht/routes.py | 88 ++++++++- geruecht/static/master.css | 46 ++++- geruecht/templates/finanzer.html | 119 ++++++++++++ geruecht/templates/home.html | 37 +++- geruecht/templates/layout.html | 72 ++++--- geruecht/templates/login.html | 41 ++++ 9 files changed, 664 insertions(+), 64 deletions(-) create mode 100644 geruecht/templates/finanzer.html create mode 100644 geruecht/templates/login.html diff --git a/geruecht/__init__.py b/geruecht/__init__.py index 0948279..a514acd 100644 --- a/geruecht/__init__.py +++ b/geruecht/__init__.py @@ -1,11 +1,17 @@ from flask import Flask, g from flask_sqlalchemy import SQLAlchemy +from flask_bcrypt import Bcrypt +from flask_login import LoginManager app = Flask(__name__) app.config['SECRET_KEY'] = '0a657b97ef546da90b2db91862ad4e29' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' db = SQLAlchemy(app) +bcrypt = Bcrypt(app) +login_manager = LoginManager(app) +login_manager.login_view = 'login' +login_manager.login_message_category = 'info' from geruecht import routes \ No newline at end of file diff --git a/geruecht/forms.py b/geruecht/forms.py index 3940087..c13803b 100644 --- a/geruecht/forms.py +++ b/geruecht/forms.py @@ -1,5 +1,5 @@ from flask_wtf import FlaskForm -from wtforms import StringField, SubmitField +from wtforms import StringField, SubmitField, PasswordField; from wtforms.validators import DataRequired, Length, ValidationError from geruecht.model import User @@ -14,3 +14,10 @@ class RegistrationForm(FlaskForm): if user: raise ValidationError('Bist du behindert!? Der Name ist vergeben!!') + + +class FinanzerLogin(FlaskForm): + username = StringField('Name', validators=[DataRequired(), Length(min=2, max=20)]) + password = PasswordField('Password', validators=[DataRequired()]) + submit = SubmitField('Login') + diff --git a/geruecht/model.py b/geruecht/model.py index 79ca874..9e73343 100644 --- a/geruecht/model.py +++ b/geruecht/model.py @@ -1,45 +1,341 @@ -from geruecht import db +from geruecht import db, login_manager from threading import Timer +from flask_login import UserMixin +from datetime import date + + +@login_manager.user_loader +def load_finanzer(finanzer_id): + return Finanzer.query.get(int(finanzer_id)) + class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) sum = db.Column(db.Float, nullable=False, default=0.0) + jan = db.Column(db.Float, nullable=False, default=0) + jan_sub = db.Column(db.Float, nullable=False, default=0) + jan_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + feb = db.Column(db.Float, nullable=False, default=0) + feb_sub = db.Column(db.Float, nullable=False, default=0) + feb_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + maer = db.Column(db.Float, nullable=False, default=0) + maer_sub = db.Column(db.Float, nullable=False, default=0) + maer_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + apr = db.Column(db.Float, nullable=False, default=0) + apr_sub = db.Column(db.Float, nullable=False, default=0) + apr_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + mai = db.Column(db.Float, nullable=False, default=0) + mai_sub = db.Column(db.Float, nullable=False, default=0) + mai_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + jun = db.Column(db.Float, nullable=False, default=0) + jun_sub = db.Column(db.Float, nullable=False, default=0) + jun_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + jul = db.Column(db.Float, nullable=False, default=0) + jul_sub = db.Column(db.Float, nullable=False, default=0) + jul_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + aug = db.Column(db.Float, nullable=False, default=0) + aug_sub = db.Column(db.Float, nullable=False, default=0) + aug_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + sep = db.Column(db.Float, nullable=False, default=0) + sep_sub = db.Column(db.Float, nullable=False, default=0) + sep_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + okt = db.Column(db.Float, nullable=False, default=0) + okt_sub = db.Column(db.Float, nullable=False, default=0) + okt_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + nov = db.Column(db.Float, nullable=False, default=0) + nov_sub = db.Column(db.Float, nullable=False, default=0) + nov_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + dez = db.Column(db.Float, nullable=False, default=0) + dez_sub = db.Column(db.Float, nullable=False, default=0) + dez_year = db.Column(db.Integer, nullable=False, default=date.today().year) + + + + def getsum(self): + jan = self.jan - self.jan_sub + feb = self.feb - self.feb_sub + maer = self.maer - self.maer_sub + apr = self.apr - self.apr_sub + mai = self.mai - self.mai_sub + jun = self.jun - self.jun_sub + jul = self.jul - self.jul_sub + aug = self.aug - self.aug_sub + sep = self.sep - self.sep_sub + okt = self.okt - self.okt_sub + nov = self.nov - self.nov_sub + dez = self.dez - self.dez_sub + + result = jan + feb + maer + apr + mai + jun + jul + aug + sep + okt + nov + dez + return result + + def add_sub(self, value): + year = date.today().year + month = date.today().month + + if month == 1: + if year == self.jan_year: + self.jan_sub += value + else: + self.jan = 0 + self.jan_sub = value + self.jan_year = year; + elif month == 2: + if year == self.feb_year: + self.feb_sub += value + else: + self.feb = 0 + self.feb_sub = value + self.feb_year = year + elif month == 3: + if year == self.maer_year: + self.maer_sub += value + else: + self.maer = 0 + self.maer_sub = value + self.maer_year = year + elif month == 4: + if year == self.apr_year: + self.apr_sub += value + else: + self.apr = 0 + self.apr_sub = value + self.apr_year = year + elif month == 5: + if year == self.mai_year: + self.mai_sub += value + else: + self.mai = 0 + self.mai_sub = value + self.mai_year = year + elif month == 6: + if year == self.jun_year: + self.jun_sub += value + else: + self.jun = 0 + self.jun_sub = value + self.jun_year = year + elif month == 7: + if year == self.jul_year: + self.jul_sub += value + else: + self.jul = 0 + self.jul_sub = value + self.jul_year = year + elif month == 8: + if year == self.aug_year: + self.aug_sub += value + else: + self.aug = 0 + self.aug_sub = value + self.aug_year = year + elif month == 9: + if year == self.sep_year: + self.sep_sub += value + else: + self.sep = 0 + self.sep_sub = value + self.sep_year = year + elif month == 10: + if year == self.okt_year: + self.okt_sub += value + else: + self.okt = 0 + self.okt_sub = value + self.okt_year = year + elif month == 11: + if year == self.nov_year: + self.nov_sub += value + else: + self.nov = 0 + self.nov_sub = value + self.nov_year = year + elif month == 12: + if year == self.dez_year: + self.dez_sub += value + else: + self.dez = 0 + self.dez_sub = value + self.dez_year = year + else: + raise IndexError('Mehr monate gibt es nicht') + + def add(self, value): - self.sum += value - def storner(self, value): - self.sum = self.sum - value + + year = date.today().year + month = date.today().month + + if month == 1 : + if year == self.jan_year: + self.jan += value + else: + self.jan = value + self.jan_sub = 0 + self.jan_year = year + elif month == 2: + if year == self.feb_year: + self.feb += value + else: + self.feb = value + self.feb_sub = 0 + self.feb_year = year + elif month == 3: + if year == self.maer_year: + self.maer += value + else: + self.maer = value + self.mear_sub = 0 + self.mear_year = year + elif month == 4: + if year == self.apr_year: + self.apr += value + else: + self.apr = value + self.apr_sub = 0 + self.apr_year = year + elif month == 5: + if year == self.mai_year: + self.mai += value + else: + self.mai = value + self.mai_sub = 0 + self.mai_year = year + elif month == 6: + if year == self.jun_year: + self.jun += value + else: + self.jun = value + self.jun_sub = 0 + self.jun_year = year + elif month == 7: + if year == self.jul_year: + self.jul += value + else: + self.jul = value + self.jul_sub = 0 + self.jul_year = year + elif month == 8: + if year == self.aug_year: + self.aug += value + else: + self.aug = value + self.aug_sub = 0 + self.aug_year = year + elif month == 9: + if year == self.sep_year: + self.sep += value + else: + self.sep = value + self.sep_sub = 0 + self.sep_year = year + elif month == 10: + if year == self.okt_year: + self.okt += value + else: + self.okt = value + self.okt_sub = 0 + self.okt_year = year + elif month == 11: + if year == self.nov_year: + self.nov += value + else: + self.nov = value + self.nov_sub = 0 + self.nov_year = year + elif month == 12: + if year == self.dez_year: + self.dez += value + else: + self.dez = value + self.dez_sub = 0 + self.dez_year = year + else: + raise IndexError('Mehr monate gibt es nicht') + + + def storner(self, value, month): + + if month == 1: + self.jan = self.jan - value + elif month == 2: + self.feb = self.feb - value + elif month == 3: + self.maer = self.maer - value + elif month == 4: + self.apr = self.apr - value + elif month == 5: + self.mai = self.mai - value + elif month == 6: + self.jun = self.jun - value + elif month == 7: + self.jul = self.jul - value + elif month == 8: + self.aug = self.aug - value + elif month == 9: + self.sep = self.sep - value + elif month == 10: + self.okt = self.okt - value + elif month == 11: + self.nov = self.nov - value + elif month == 12: + self.dez = self.dez - value + else: + raise IndexError('Mehr monate gibt es nicht') def __repr__(self): return f"User('{self.username}', '{self.sum}')" +class Finanzer(db.Model, UserMixin): + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(20), unique=True, nullable=False) + password = db.Column(db.String(60), nullable=False) + + def __repr__(self): + return f"Finanzer('{self.username}')" + + class History: history = [] user = None def __init__(self, history, user, value): - self.history=history + self.history = history self.history.append(self) self.user = user + self.month = date.today().month 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) + User.query.filter_by(username=self.user.username).first().storner(self.value, self.month) 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 0660718..d2333e3 100644 --- a/geruecht/routes.py +++ b/geruecht/routes.py @@ -1,7 +1,8 @@ -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 +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 @@ -36,7 +37,7 @@ def home(): 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 + " €")) + obj_response.html(f'#{user_2.id}-sum', str("%.2f" % user_2.getsum() + " €")) def storner(obj_response): try: @@ -49,7 +50,7 @@ def home(): 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 + " €")) + obj_response.html(f'#{user_2.id}-sum', str("%.2f" % user_2.getsum() + " €")) except IndexError: print("history: {} is empty".format(history)) @@ -70,10 +71,85 @@ def home(): 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") +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') diff --git a/geruecht/static/master.css b/geruecht/static/master.css index ae03eae..3c3c336 100644 --- a/geruecht/static/master.css +++ b/geruecht/static/master.css @@ -13,10 +13,10 @@ body { background-color: #055288; text-align: center; padding: 5px 5px; - margin: 17px 17px; + margin: 7px 17px; text-decoration: none; font-size: 17px; - border: 0px solid white; + border: 5px solid #055288; border-style: solid; border-radius: 10px; transition-duration: 0.4s; @@ -33,6 +33,7 @@ body { .blue-button:hover { background: white; + border: 5px solid #055288; color: black; } @@ -58,6 +59,17 @@ body { transform: translateY(4px) } +.logo-nav img { + position: absolute; + transition-duration: 0.4s; +} + +.logo-nav img.main-logo:hover { + opacity: 0; +} + + + .geruecht { margin: 5px auto; display: table; @@ -78,28 +90,44 @@ body { width: 20%; } +.auto-width { + width:6.66%; +} + +.width-auto { + width:80%; +} + .g-item p { font-size: 15px; } .button { width: 70px; + heigt: 30px; border-radius: 10px; + border: 5px solid green; background: green; color: white; transition-duration: 0.4s; } +.button:hover { + color: green; + background: white; +} + .button:active { transform: translateY(4px); } .bottombar { background-color: #077BCB; - overflow: hidden; + overflow: scroll; position: fixed; bottom: 0; width: 100%; + height: 62px; display: table; } @@ -152,3 +180,15 @@ body { border-radius: 5px; padding: 6px; } + +.container { + margin-bottom: 50%; +} + +.schulden { + color: red; +} + +.bezahlt { + color: green; +} diff --git a/geruecht/templates/finanzer.html b/geruecht/templates/finanzer.html new file mode 100644 index 0000000..def5748 --- /dev/null +++ b/geruecht/templates/finanzer.html @@ -0,0 +1,119 @@ +{% extends 'layout.html' %} + +{% block content %} +
+
+ Username +
+
+ Januar +
+
+ Februar +
+
+ März +
+
+ April +
+
+ Mai +
+
+ Juni +
+
+ July +
+
+ August +
+
+ September +
+
+ Oktober +
+
+ November +
+
+ Dezember +
+
+ Schulden +
+
+ Ausgleich +
+
+ {% for user in users %} +
+
+

{{ user.username }}

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

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

+

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

+
+
+

{{ "%.2f"%user.getsum() }} €

+
+
+ +
+
+ {% endfor %} +{% endblock %} +{% block bottombar %} + + +
+ Anwenden +
+{% endblock %} diff --git a/geruecht/templates/home.html b/geruecht/templates/home.html index d43fafe..7045b52 100644 --- a/geruecht/templates/home.html +++ b/geruecht/templates/home.html @@ -6,27 +6,48 @@

{{ user.username }}

-

+2 €

+ +2 €
-

+1 €

+ +1 €
-

+0,5 €

+ +0,5 €
-

+0,4 €

+ +0,4 €
-

+0,2 €

+ +0,2 €
-

+0,1 €

+ +0,1 €
-

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

+

{{ "%.2f"%user.getsum() }} €

{% endfor %} {% endblock %} - +{% block bottombar %} +
+ {% if form %} +
+ {{ form.hidden_tag() }} +
+ +
+ {{ form.username.label(class="reg-label") }} + {{ form.username() }} +
+
+
+ {{ form.submit(class="blue-button no-mg") }} +
+
+ {% endif %} +
+
+ Stornieren +
+{% endblock %} diff --git a/geruecht/templates/layout.html b/geruecht/templates/layout.html index 949114d..a115b51 100644 --- a/geruecht/templates/layout.html +++ b/geruecht/templates/layout.html @@ -19,24 +19,20 @@ -
- - + + + - - Finanzer + {% if current_user.is_authenticated %} + Logout + Übersicht + {% else %} + Finanzer + {% endif %}
{% block content %} @@ -44,6 +40,9 @@ {% endblock %}
+ + +
{% with messages = get_flashed_messages(with_categories=true) %} {% if messages %} @@ -53,38 +52,33 @@
{% endfor %} {% endif %} - {% if form.username.errors %} - {% for error in form.username.errors %} -
- {{ error }} -
- {% endfor %} + {% if form %} + {% if form.username.errors %} + {% for error in form.username.errors %} +
+ {{ error }} +
+ {% endfor %} + {% endif %} + {% if form.password %} + {% if form.password.errors %} + {% for error in form.password.errors %} +
+ {{ error }} +
+ {% endfor %} + {% endif %} + {% endif %} {% endif %} {% endwith %}
-
- {% if form %} -
- {{ form.hidden_tag() }} -
- -
- {{ form.username.label(class="reg-label") }} - {{ form.username() }} -
-
-
- {{ form.submit(class="blue-button no-mg") }} -
-
- {% endif %} -
-
-

Stornieren

-
+ {% block bottombar %} + + {% endblock %} +