diff --git a/app.py b/app.py index 8318025..55d4a04 100644 --- a/app.py +++ b/app.py @@ -4,6 +4,7 @@ import hashlib from flask import Flask, render_template, redirect, url_for, request from flask_login import login_required, LoginManager, login_user, logout_user, current_user +from models.Habit import Habit from models.User import User from utils import anonymous_required @@ -74,11 +75,11 @@ def signup_post(): # Check for errors errors = {} if not email: - errors['email'] = 'Email is required.' + errors['email'] = 'Die E-Mail Adresse ist erforderlich.' if not name: - errors['name'] = 'Name is required.' + errors['name'] = 'Der Name ist erforderlich.' if not password: - errors['password'] = 'Password is required.' + errors['password'] = 'Das Passwort ist erforderlich.' if errors: return render_template( @@ -105,17 +106,17 @@ def login_post(): # Check for errors errors = {} if not email: - errors['email'] = 'Email is required.' + errors['email'] = 'Die E-Mail Adresse ist erforderlich.' if not password: - errors['password'] = 'Password is required.' + errors['password'] = 'Das Passwort ist erforderlich.' # Check if user exists user = User.get_by_email(email) if not user: - errors['email'] = 'User does not exist.' + errors['email'] = 'E-Mail Adresse nicht gefunden.' elif user.password is None or hashlib.sha256(password.encode()).hexdigest() != user.password: - errors['password'] = 'Password incorrect.' + errors['password'] = 'Das Passwort ist falsch.' if errors: return render_template( @@ -140,6 +141,69 @@ def logout(): return redirect(url_for('index')) +@app.route('/habit', methods=['POST']) +@login_required +def habit_create(): + name = request.form.get('name') + note = request.form.get('note') + times = request.form.get('times') + unit = request.form.get('unit') + + # Check for errors + errors = {} + if not name: + errors['name'] = 'Der Name ist erforderlich.' + if not times: + errors['times'] = 'Die Anzahl ist erforderlich.' + if not note: + note = '' + if not unit: + errors['unit'] = 'Die Einheit ist erforderlich.' + + # Check if times is an integer + try: + times = int(times) + except ValueError: + errors['times'] = 'Die Anzahl muss eine Zahl sein.' + + if errors: + return render_template( + 'index.html', + name=name, + note=note, + times=times, + unit=unit, + errors=errors, + habits=current_user.get_habits() + ) + + # Map unit to integer + if unit == 'Tag': + unit = 0 + elif unit == 'Woche': + unit = 1 + elif unit == 'Monat': + unit = 2 + elif unit == 'Jahr': + unit = 3 + else: + unit = 1 + + # Save habit to database + habit = Habit.create(current_user.id, name, times, note, unit) + + # Back to index + return render_template( + 'index.html', + name=name, + note=note, + times=times, + unit=unit, + errors=errors, + habits=current_user.get_habits() + ) + + # Run the application if __name__ == '__main__': - app.run(port=5000, debug=True) \ No newline at end of file + app.run(port=5000, debug=True) diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index 28bb449..c28540f 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -63,6 +63,15 @@ def create_habit(name: str, user_id: int, times: int, unit: int, slot: int, note conn.close() return cursor.lastrowid +def get_next_slot(): + query = f"SELECT slot FROM habits ORDER BY slot DESC LIMIT 1;" + conn = con3() + cursor = conn.cursor() + cursor.execute(query) + slot = cursor.fetchone() + conn.close() + return slot[0] + 1 if slot else 0 + def get_habit(id: int): query = f"SELECT * FROM habits WHERE id = {id};" diff --git a/models/Habit.py b/models/Habit.py index d65be2b..652eabb 100644 --- a/models/Habit.py +++ b/models/Habit.py @@ -1,11 +1,11 @@ from dataclasses import dataclass -from db.SQLiteClient import create_habit, get_habit, delete_habit +from db.SQLiteClient import create_habit, get_habit, delete_habit, get_next_slot # Unit wird als Integers wie folgt gemessen: # 0: Tag # 1: Woche (Default) -# 2: Monal +# 2: Monat # 3: Jahr @dataclass @@ -19,8 +19,9 @@ class Habit: slot: int @staticmethod - def create(user_id: int, name: str, times: int, slot: int, note: str | None=None, unit: int | None=1): - id = create_habit(user_id, name, note, times, unit, slot) + def create(user_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1): + slot = get_next_slot() + id = create_habit(name, user_id, times, unit, slot, note) return Habit(id, user_id, name, note, times, unit, slot) @staticmethod