Finished POST Request for habit creation
This commit is contained in:
parent
0922309531
commit
729d11f1c9
80
app.py
80
app.py
@ -4,6 +4,7 @@ import hashlib
|
|||||||
from flask import Flask, render_template, redirect, url_for, request
|
from flask import Flask, render_template, redirect, url_for, request
|
||||||
from flask_login import login_required, LoginManager, login_user, logout_user, current_user
|
from flask_login import login_required, LoginManager, login_user, logout_user, current_user
|
||||||
|
|
||||||
|
from models.Habit import Habit
|
||||||
from models.User import User
|
from models.User import User
|
||||||
from utils import anonymous_required
|
from utils import anonymous_required
|
||||||
|
|
||||||
@ -74,11 +75,11 @@ def signup_post():
|
|||||||
# Check for errors
|
# Check for errors
|
||||||
errors = {}
|
errors = {}
|
||||||
if not email:
|
if not email:
|
||||||
errors['email'] = 'Email is required.'
|
errors['email'] = 'Die E-Mail Adresse ist erforderlich.'
|
||||||
if not name:
|
if not name:
|
||||||
errors['name'] = 'Name is required.'
|
errors['name'] = 'Der Name ist erforderlich.'
|
||||||
if not password:
|
if not password:
|
||||||
errors['password'] = 'Password is required.'
|
errors['password'] = 'Das Passwort ist erforderlich.'
|
||||||
|
|
||||||
if errors:
|
if errors:
|
||||||
return render_template(
|
return render_template(
|
||||||
@ -105,17 +106,17 @@ def login_post():
|
|||||||
# Check for errors
|
# Check for errors
|
||||||
errors = {}
|
errors = {}
|
||||||
if not email:
|
if not email:
|
||||||
errors['email'] = 'Email is required.'
|
errors['email'] = 'Die E-Mail Adresse ist erforderlich.'
|
||||||
if not password:
|
if not password:
|
||||||
errors['password'] = 'Password is required.'
|
errors['password'] = 'Das Passwort ist erforderlich.'
|
||||||
|
|
||||||
# Check if user exists
|
# Check if user exists
|
||||||
user = User.get_by_email(email)
|
user = User.get_by_email(email)
|
||||||
|
|
||||||
if not user:
|
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:
|
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:
|
if errors:
|
||||||
return render_template(
|
return render_template(
|
||||||
@ -140,6 +141,69 @@ def logout():
|
|||||||
return redirect(url_for('index'))
|
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
|
# Run the application
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(port=5000, debug=True)
|
app.run(port=5000, debug=True)
|
||||||
|
|||||||
@ -63,6 +63,15 @@ def create_habit(name: str, user_id: int, times: int, unit: int, slot: int, note
|
|||||||
conn.close()
|
conn.close()
|
||||||
return cursor.lastrowid
|
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):
|
def get_habit(id: int):
|
||||||
query = f"SELECT * FROM habits WHERE id = {id};"
|
query = f"SELECT * FROM habits WHERE id = {id};"
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
from dataclasses import dataclass
|
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:
|
# Unit wird als Integers wie folgt gemessen:
|
||||||
# 0: Tag
|
# 0: Tag
|
||||||
# 1: Woche (Default)
|
# 1: Woche (Default)
|
||||||
# 2: Monal
|
# 2: Monat
|
||||||
# 3: Jahr
|
# 3: Jahr
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -19,8 +19,9 @@ class Habit:
|
|||||||
slot: int
|
slot: int
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(user_id: int, name: str, times: int, slot: int, note: str | None=None, unit: int | None=1):
|
def create(user_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1):
|
||||||
id = create_habit(user_id, name, note, times, unit, slot)
|
slot = get_next_slot()
|
||||||
|
id = create_habit(name, user_id, times, unit, slot, note)
|
||||||
return Habit(id, user_id, name, note, times, unit, slot)
|
return Habit(id, user_id, name, note, times, unit, slot)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user