From 395133728f81effb56a256fde35f483e9c27cfd2 Mon Sep 17 00:00:00 2001 From: Verox001 Date: Fri, 26 Jan 2024 10:01:02 +0100 Subject: [PATCH] Finished checkbox checking of habits --- app.py | 59 ++++++++++++++++++++++++++++++++++++---- models/Habit.py | 12 ++++++-- models/HabitTrackings.py | 7 +++-- templates/index.html | 42 +++++++++++++--------------- 4 files changed, 86 insertions(+), 34 deletions(-) diff --git a/app.py b/app.py index 123178a..9345001 100644 --- a/app.py +++ b/app.py @@ -127,6 +127,34 @@ def index(): habits = [] name = "Bitte melde dich an, du Vollhorst." + # Add checked attribute to habits (if they have been checked today) + for habit in habits: + trackings = habit.get_habitTrackings() + for tracking in trackings: + # day + if habit.unit == 0: + if tracking.created_at.date() == datetime.date.today(): + habit.checked = True + break + # week + elif habit.unit == 1: + if tracking.created_at.date().isocalendar()[1] == datetime.date.today().isocalendar()[1]: + habit.checked = True + break + # month + elif habit.unit == 2: + if tracking.created_at.date().month == datetime.date.today().month: + habit.checked = True + break + # year + elif habit.unit == 3: + if tracking.created_at.date().year == datetime.date.today().year: + habit.checked = True + break + + else: + habit.checked = False + # habits = [("lesen", "eine Seite vor dem Schlafen gehen"), ("sport", "3x Gym")] return render_template( 'index.html', @@ -238,18 +266,37 @@ def check_habit(): trackings = habit.get_habitTrackings() # Check if habit has been tracked today - unchecked = False + delete_tracking = None for tracking in trackings: - if tracking.created_at.date() == datetime.date.today(): - tracking.delete() - unchecked = True + # day + if habit.unit == 0: + if tracking.created_at.date() == datetime.date.today(): + delete_tracking = tracking + break + # week + elif habit.unit == 1: + if tracking.created_at.date().isocalendar()[1] == datetime.date.today().isocalendar()[1]: + delete_tracking = tracking + break + # month + elif habit.unit == 2: + if tracking.created_at.date().month == datetime.date.today().month: + delete_tracking = tracking + break + # year + elif habit.unit == 3: + if tracking.created_at.date().year == datetime.date.today().year: + delete_tracking = tracking + break - if not unchecked: + if not delete_tracking: HabitTrackings.create(habit_id, 1) + else: + delete_tracking.delete() return { "habitId": habit_id, - "unchecked": unchecked, + "unchecked": not delete_tracking } diff --git a/models/Habit.py b/models/Habit.py index 30f2b31..c8da7f4 100644 --- a/models/Habit.py +++ b/models/Habit.py @@ -1,7 +1,9 @@ from dataclasses import dataclass +from datetime import datetime + from models.HabitTrackings import HabitTrackings -from db.SQLiteClient import create_habit, get_habit,update_habit, delete_habit, get_next_slot, \ - get_habitTrackings_by_habit_id, get_slots, update_slot +from db.SQLiteClient import update_slot, create_habit, get_habit, delete_habit, get_next_slot, \ + get_habitTrackings_by_habit_id, get_slots, update_habit # Unit wird als Integers wie folgt gemessen: @@ -62,4 +64,8 @@ class Habit: def get_habitTrackings(self) -> list[HabitTrackings]: - return get_habitTrackings_by_habit_id(self.id) + trackings = [] + for rawTracking in get_habitTrackings_by_habit_id(self.id): + trackings.append(HabitTrackings(rawTracking[0], rawTracking[1], rawTracking[2], datetime.strptime(rawTracking[3], "%Y-%m-%dT%H:%M:%S.%f"))) + + return trackings diff --git a/models/HabitTrackings.py b/models/HabitTrackings.py index 1287cf7..33c03b6 100644 --- a/models/HabitTrackings.py +++ b/models/HabitTrackings.py @@ -1,4 +1,6 @@ from dataclasses import dataclass +from datetime import date, datetime + from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings @@ -7,16 +9,17 @@ class HabitTrackings: id: int habit_id: int times: int + created_at: date @staticmethod def create(habit_id: int, times: int): id = create_habitTrackings(habit_id, times) - return HabitTrackings(id, habit_id, times) + return HabitTrackings(id, habit_id, times, datetime.now()) @staticmethod def get(id: int): habitTrackings = get_habitTrackings(id) - return HabitTrackings(habitTrackings[0], habitTrackings[1], habitTrackings[2]) if habitTrackings else None + return HabitTrackings(habitTrackings[0], habitTrackings[1], habitTrackings[2], datetime.strptime(habitTrackings[3], "%Y-%m-%dT%H:%M:%S.%f")) if habitTrackings else None def delete(self): delete_habitTrackings(self.id) diff --git a/templates/index.html b/templates/index.html index 057afa4..af33d6e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -24,7 +24,7 @@
-
+
-