diff --git a/app.py b/app.py index 6bc5be6..2808b81 100644 --- a/app.py +++ b/app.py @@ -5,6 +5,7 @@ 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.HabitList import HabitList from models.HabitTrackings import HabitTrackings from models.User import User from utils import anonymous_required @@ -120,20 +121,20 @@ def logout(): @app.route('/') def index(): if current_user.is_authenticated: - habits = current_user.get_habits() + habit_lists = current_user.get_habitLists() name = "Hallo " + current_user.name else: - habits = [] + habit_lists = [] name = "Bitte melde dich an." # Sort habits by whether they have been checked today and then by slot - habits.sort(key=lambda habit: (habit.checked, habit.slot)) + # habits.sort(key=lambda habit: (habit.checked, habit.slot)) return render_template( 'index.html', title=name, utc_dt=datetime.datetime.now().strftime("%d.%m.%Y %H:%M %A"), - habits=habits, + habit_lists=habit_lists, errors={}, ) @@ -156,6 +157,7 @@ def habit_create(): note = request.form.get('note') times = request.form.get('times') unit = request.form.get('unit') + list_id = request.form.get('list_query') # Check for errors errors = {} @@ -167,6 +169,8 @@ def habit_create(): note = '' if not unit: errors['unit'] = 'Die Einheit ist erforderlich.' + if not list_id: + errors['list_query'] = 'Die Habitliste ist erforderlich.' # Check if times is an integer try: @@ -190,7 +194,8 @@ def habit_create(): note=note, times=times, unit=unit, - errors=errors + errors=errors, + list_id=list_id ) # Map unit to integer @@ -212,6 +217,45 @@ def habit_create(): return redirect(url_for('index')) +@app.route('/habit-list') +@login_required +def habit_list_creation(): + return render_template( + 'habit-list.html', + title='Erstelle eine Habitliste', + errors={}, + ) + + +@app.route('/habit-list', methods=['POST']) +@login_required +def habit_list_create(): + name = request.form.get('name') + description = request.form.get('description') + + # Check for errors + errors = {} + if not name: + errors['name'] = 'Der Name ist erforderlich.' + if not description: + note = '' + + if errors: + return render_template( + 'habit-list.html', + title='Erstelle eine Habitliste', + name=name, + description=description, + errors=errors + ) + + # Save habit to database + habit = HabitList.create(current_user.id, name, description) + + # Back to index + return redirect(url_for('index')) + + @app.route('/profile') @login_required def profile(): diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index d31ac8d..dec84f5 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -102,24 +102,46 @@ def get_habits(list_id: int): def get_heatmap_value(user_id: int, days: int): + # Berechnet das Datum, ab dem die Habits gezählt werden sollen date = (datetime.now() - timedelta(days=days)).date() print(date) - query = f"SELECT id FROM habits WHERE (SELECT id FROM habit_lists WHERE (SELECT list_id FROM habit_users WHERE user_id = {user_id}) = id) = list_id;" - query2 = (f"SELECT habits.id FROM habits, habit_trackings WHERE (SELECT id FROM habit_lists WHERE (SELECT list_id FROM habit_users WHERE user_id = {user_id}) = id) = list_id " - f"AND habits.created_at LIKE '{date}%' AND habit_trackings.habit_id = habits.id;") - print(query2) + + # Uses JOINs to get all Habits + query = (f"SELECT habits.id FROM habits " + f"JOIN habit_lists ON habits.list_id = habit_lists.id " + f"JOIN habit_users ON habit_lists.id = habit_users.list_id " + f"WHERE habit_users.user_id = {user_id};") + + # Uses JOINs to get all checked Habits on a specific date + query2 = (f"SELECT habits.id FROM habits " + f"JOIN habit_lists ON habits.list_id = habit_lists.id " + f"JOIN habit_users ON habit_lists.id = habit_users.list_id " + f"JOIN habit_trackings ON habits.id = habit_trackings.habit_id " + f"WHERE habit_users.user_id = {user_id} AND DATE(habit_trackings.created_at) = '{date}';") + conn = con3() cursor = conn.cursor() + + # Execute the queries cursor.execute(query) all_habits = cursor.fetchall() cursor.execute(query2) checked_habits = cursor.fetchall() + + # Close the database connection count = len(all_habits) print(count) count2 = len(checked_habits) print(count2) + + # Close the database connection conn.close() - return int(count2 / count * 100) + + # Calculate the percentage of checked Habits + if count > 0: + return int(count2 / count * 100) + else: + return 0 def get_next_slot(list_id: int): @@ -219,17 +241,22 @@ def delete_habitTrackings(id: int): def create_habitList(user_id: int, name: str, description: str): now = datetime.now().isoformat() query = ( - f"INSERT INTO habit_lists (user_id, name, description, created_at, updated_at) VALUES ('{user_id}', '{name}', '{description}', '{now}');") + f"INSERT INTO habit_lists (name, description, created_at, updated_at) VALUES ('{name}', '{description}', '{now}', '{now}');") + conn = con3() cursor = conn.cursor() cursor.execute(query) + + query1 = f"INSERT INTO habit_users (user_id, list_id, created_at, updated_at) VALUES ('{user_id}', '{cursor.lastrowid}', '{now}', '{now}');" + + cursor.execute(query1) conn.commit() conn.close() return cursor.lastrowid def get_habitList(id: int): - query = f"SELECT * FROM habit_list WHERE id = {id};" + query = f"SELECT * FROM habit_lists WHERE id = {id};" conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -239,7 +266,7 @@ def get_habitList(id: int): def get_habitLists(user_id: int): - query = f"SELECT * FROM habit_list WHERE user_id = {user_id};" + query = f"SELECT habit_lists.* FROM habit_lists JOIN habit_users ON habit_lists.id = habit_users.list_id WHERE habit_users.user_id = {user_id};" conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -250,7 +277,7 @@ def get_habitLists(user_id: int): def update_habitList(id: int, name: str, description: str): now = datetime.now().isoformat() - query = f"UPDATE habit_list SET name = {name}, description = {description}, updated_at = '{now}' WHERE id = {id};" + query = f"UPDATE habit_lists SET name = {name}, description = {description}, updated_at = '{now}' WHERE id = {id};" conn = con3() cursor = conn.cursor() cursor.execute(query) @@ -260,7 +287,7 @@ def update_habitList(id: int, name: str, description: str): def delete_habitList(id: int): - query = f"DELETE FROM habit_list WHERE id = {id};" + query = f"DELETE FROM habit_lists WHERE id = {id};" conn = con3() cursor = conn.cursor() cursor.execute(query) diff --git a/db/migrations/1705145041_create_habits_table.sql b/db/migrations/1705145041_create_habits_table.sql deleted file mode 100644 index 7e5d01c..0000000 --- a/db/migrations/1705145041_create_habits_table.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE TABLE IF NOT EXISTS habits -( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - note TEXT NULL, - times INTEGER NOT NULL, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL -); diff --git a/db/migrations/1705398235_delete_habits_table.sql b/db/migrations/1705398235_delete_habits_table.sql deleted file mode 100644 index 8d667a0..0000000 --- a/db/migrations/1705398235_delete_habits_table.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE habits; diff --git a/db/migrations/1705398327_habits_table.sql b/db/migrations/1705398327_habits_table.sql deleted file mode 100644 index d92838b..0000000 --- a/db/migrations/1705398327_habits_table.sql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE TABLE IF NOT EXISTS habits -( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER, - name TEXT NOT NULL, - note TEXT, - times INTEGER NOT NULL, - unit INTEGER, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - FOREIGN KEY (user_id) REFERENCES users(id) -); \ No newline at end of file diff --git a/db/migrations/1705400256_delete_habits_table.sql b/db/migrations/1705400256_delete_habits_table.sql deleted file mode 100644 index 8d667a0..0000000 --- a/db/migrations/1705400256_delete_habits_table.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE habits; diff --git a/db/migrations/1705400283_create_habits_table.sql b/db/migrations/1705400283_create_habits_table.sql deleted file mode 100644 index 31c8272..0000000 --- a/db/migrations/1705400283_create_habits_table.sql +++ /dev/null @@ -1,13 +0,0 @@ -CREATE TABLE IF NOT EXISTS habits -( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER NOT NULL, - name TEXT NOT NULL, - note TEXT, - times INTEGER NOT NULL, - unit INTEGER, - list_index INTEGER NOT NULL, - created_at TEXT NOT NULL, - updated_at TEXT NOT NULL, - FOREIGN KEY (user_id) REFERENCES users(id) -); \ No newline at end of file diff --git a/db/migrations/1705434240_delete_habits_table.sql b/db/migrations/1705434240_delete_habits_table.sql deleted file mode 100644 index 8d667a0..0000000 --- a/db/migrations/1705434240_delete_habits_table.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE habits; diff --git a/db/migrations/1705485233_create_test_data.sql b/db/migrations/1705485233_create_test_data.sql deleted file mode 100644 index d8c6f06..0000000 --- a/db/migrations/1705485233_create_test_data.sql +++ /dev/null @@ -1,2 +0,0 @@ -INSERT INTO users (name, email, password, created_at, updated_at) - VALUES ('Nikolaus MikeyMouse', 'Nordpol@icloud.com', 'a36c101570cc4410993de5385ad7034adb2dae6a05139ac7672577803084634d', '23:00', '23:00'); diff --git a/db/migrations/1705485243_create_test_data.sql b/db/migrations/1705485243_create_test_data.sql deleted file mode 100644 index a082125..0000000 --- a/db/migrations/1705485243_create_test_data.sql +++ /dev/null @@ -1,2 +0,0 @@ -INSERT INTO habits (user_id, name, note, times, unit, slot, created_at, updated_at) - VALUES ('1', 'Sport', '10x Liegestutze', '1', '1', '1', '23:00', '23:00'); diff --git a/db/migrations/1705485247_create_test_data.sql b/db/migrations/1705485247_create_test_data.sql deleted file mode 100644 index 236873a..0000000 --- a/db/migrations/1705485247_create_test_data.sql +++ /dev/null @@ -1,2 +0,0 @@ -INSERT INTO habits (user_id, name, note, times, unit, slot, created_at, updated_at) - VALUES ('1', 'Sport', '10x Klimmzuge', '1', '1', '3', '23:00', '23:00'); diff --git a/db/migrations/1705485251_create_test_data.sql b/db/migrations/1705485251_create_test_data.sql deleted file mode 100644 index 1d47ed5..0000000 --- a/db/migrations/1705485251_create_test_data.sql +++ /dev/null @@ -1,2 +0,0 @@ -INSERT INTO habits (user_id, name, note, times, unit, slot, created_at, updated_at) - VALUES ('1', 'Essen', '1x Gemüse', '1', '2', '2', '23:00', '23:00'); \ No newline at end of file diff --git a/db/migrations/1705485255_create_test_data.sql b/db/migrations/1705485255_create_test_data.sql deleted file mode 100644 index 8584031..0000000 --- a/db/migrations/1705485255_create_test_data.sql +++ /dev/null @@ -1,2 +0,0 @@ -INSERT INTO habits (user_id, name, note, times, unit, slot, created_at, updated_at) - VALUES ('2', 'Sport', '10x Liegestutze', '1', '1', '2', '23:00', '23:00'); diff --git a/models/HabitList.py b/models/HabitList.py index 7cecc4b..d97b27d 100644 --- a/models/HabitList.py +++ b/models/HabitList.py @@ -9,7 +9,6 @@ from models.Habit import Habit @dataclass class HabitList: id: int - user_id: int name: str description: str created_at: date @@ -18,12 +17,12 @@ class HabitList: @staticmethod def create(user_id: int, name: str, description: str): id = create_habitList(user_id, name, description) - return HabitList(id, user_id, name, description, datetime.now(), datetime.now()) + return HabitList(id, name, description, datetime.now(), datetime.now()) @staticmethod def get(id: int): habitList = get_habitList(id) - return HabitList(habitList[0], habitList[1], habitList[2], habitList[3], datetime.strptime(habitList[4], "%Y-%m-%dT%H:%M:%S.%f"), datetime.strptime(habitList[5], "%Y-%m-%dT%H:%M:%S.%f")) if habitList else None + return HabitList(habitList[0], habitList[1], habitList[2], datetime.strptime(habitList[3], "%Y-%m-%dT%H:%M:%S.%f"), datetime.strptime(habitList[4], "%Y-%m-%dT%H:%M:%S.%f")) if habitList else None def delete(self): delete_habitTrackings(self.id) diff --git a/models/User.py b/models/User.py index 6d36d6a..95c3933 100644 --- a/models/User.py +++ b/models/User.py @@ -47,7 +47,7 @@ class User(UserMixin): raw_habitLists = get_habitLists(self.id) habitLists = [] for habitList in raw_habitLists: - habitList = HabitList(habitList[0], habitList[1], habitList[2], habitList[3], datetime.strptime(habitList[4], "%Y-%m-%dT%H:%M:%S.%f"), datetime.strptime(habitList[5], "%Y-%m-%dT%H:%M:%S.%f")) + habitList = HabitList(habitList[0], habitList[1], habitList[2], datetime.strptime(habitList[3], "%Y-%m-%dT%H:%M:%S.%f"), datetime.strptime(habitList[4], "%Y-%m-%dT%H:%M:%S.%f")) habitLists.append(habitList) return habitLists diff --git a/templates/habit-list.html b/templates/habit-list.html new file mode 100644 index 0000000..3550013 --- /dev/null +++ b/templates/habit-list.html @@ -0,0 +1,25 @@ +{% extends 'layouts/main.html' %} + +{% block content %} + +

Habitliste erstellen📋

+ +
+
+ + +
+ {{ errors.get('name', '') }} +
+
+
+ + +
+ {{ errors.get('description', '') }} +
+
+ +
+ +{% endblock %} \ No newline at end of file diff --git a/templates/habit.html b/templates/habit.html index 847dad7..ecaffe7 100644 --- a/templates/habit.html +++ b/templates/habit.html @@ -54,6 +54,28 @@ + +
+ {{ errors.get('list_query', '') }} +
+ diff --git a/templates/index.html b/templates/index.html index 3391eb3..70f3859 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,252 +1,269 @@ {% extends 'layouts/main.html' %} {% block content %} -

{{ title }}

-

{{ utc_dt }}

+

{{ title }}

+

{{ utc_dt }}

- - -
- -
-
-
- - + -
+
+
+

Gewohnheiten

+ Neue Liste erstellen +
-
-

Gewohnheiten

- Gewohnheit erstellen -
+ {% for habit_list in habit_lists %} -
    - {% for habit in habits %} -
  • -
    - -
    -
    - +
    +

    {{ habit_list.name }}

    + Gewohnheit erstellen
    -
    - {{ habit.name }} -
    +
      + {% for habit in habit_list.get_habits() %} +
    • +
      + +
      +
      + +
      -
      - {{ habit.note }} -
      +
      + {{ habit.name }} +
      -
      - {% if habit %} - 5 🔥 - {% endif %} +
      + {{ habit.note }} +
      -
      +
      + {% if habit %} + 5 🔥 + {% endif %} - -
      -
      -
      -
      -
      -
    • +
    + + +
    +
    +
    +
    +
    +
  • + + {% endfor %} +
{% endfor %} - -
+
- + function setSelectedHabitId(habitId) { + selectedHabitId = habitId; + } + -