Finished checkbox checking of habits

This commit is contained in:
Verox001 2024-01-26 10:01:02 +01:00
parent 906473cb81
commit 395133728f
4 changed files with 86 additions and 34 deletions

57
app.py
View File

@ -127,6 +127,34 @@ def index():
habits = [] habits = []
name = "Bitte melde dich an, du Vollhorst." 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")] # habits = [("lesen", "eine Seite vor dem Schlafen gehen"), ("sport", "3x Gym")]
return render_template( return render_template(
'index.html', 'index.html',
@ -238,18 +266,37 @@ def check_habit():
trackings = habit.get_habitTrackings() trackings = habit.get_habitTrackings()
# Check if habit has been tracked today # Check if habit has been tracked today
unchecked = False delete_tracking = None
for tracking in trackings: for tracking in trackings:
# day
if habit.unit == 0:
if tracking.created_at.date() == datetime.date.today(): if tracking.created_at.date() == datetime.date.today():
tracking.delete() delete_tracking = tracking
unchecked = True 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) HabitTrackings.create(habit_id, 1)
else:
delete_tracking.delete()
return { return {
"habitId": habit_id, "habitId": habit_id,
"unchecked": unchecked, "unchecked": not delete_tracking
} }

View File

@ -1,7 +1,9 @@
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime
from models.HabitTrackings import HabitTrackings from models.HabitTrackings import HabitTrackings
from db.SQLiteClient import create_habit, get_habit,update_habit, delete_habit, get_next_slot, \ from db.SQLiteClient import update_slot, create_habit, get_habit, delete_habit, get_next_slot, \
get_habitTrackings_by_habit_id, get_slots, update_slot get_habitTrackings_by_habit_id, get_slots, update_habit
# Unit wird als Integers wie folgt gemessen: # Unit wird als Integers wie folgt gemessen:
@ -62,4 +64,8 @@ class Habit:
def get_habitTrackings(self) -> list[HabitTrackings]: 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

View File

@ -1,4 +1,6 @@
from dataclasses import dataclass from dataclasses import dataclass
from datetime import date, datetime
from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings from db.SQLiteClient import create_habitTrackings, get_habitTrackings, delete_habitTrackings
@ -7,16 +9,17 @@ class HabitTrackings:
id: int id: int
habit_id: int habit_id: int
times: int times: int
created_at: date
@staticmethod @staticmethod
def create(habit_id: int, times: int): def create(habit_id: int, times: int):
id = create_habitTrackings(habit_id, times) id = create_habitTrackings(habit_id, times)
return HabitTrackings(id, habit_id, times) return HabitTrackings(id, habit_id, times, datetime.now())
@staticmethod @staticmethod
def get(id: int): def get(id: int):
habitTrackings = get_habitTrackings(id) 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): def delete(self):
delete_habitTrackings(self.id) delete_habitTrackings(self.id)

View File

@ -24,7 +24,7 @@
<div class="row"> <div class="row">
<div class="col-md-4 col-12" id="heatmap"></div> <div class="col-sm-12 col-md-5 col-12" id="heatmap"></div>
<script> <script>
// Funktion zur Rückgabe des Montagsdatums // Funktion zur Rückgabe des Montagsdatums
function getMonday(date) { function getMonday(date) {
@ -76,7 +76,7 @@
createHeatmap(activityData); createHeatmap(activityData);
</script> </script>
<div class="col-md-8 col-12"> <div class="col-sm-12 col-md-7 col-12">
<div class="row"> <div class="row">
<h2 class="col-10">Task List</h2> <h2 class="col-10">Task List</h2>
<a class="col-2 btn btn-primary" role="button" href="/habit"> <a class="col-2 btn btn-primary" role="button" href="/habit">
@ -88,7 +88,7 @@
{% for habit in habits %} {% for habit in habits %}
<li class="row col-md-4"> <li class="row col-md-4">
<div class="col-auto"> <div class="col-auto">
<input type="checkbox" class="task-checkbox" id="{{habit.id}}" onclick="sendPostRequest('{{habit.id}}')"> <input {% if habit.checked %} checked {% endif %} type="checkbox" class="task-checkbox" id="{{habit.id}}" onclick="sendPostRequest('{{habit.id}}')">
</div> </div>
<div class="col" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis"> <div class="col" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis">
@ -108,9 +108,8 @@
function sendPostRequest(checkboxId) { function sendPostRequest(checkboxId) {
// Get the checkbox element using the provided ID // Get the checkbox element using the provided ID
var checkbox = document.getElementById(checkboxId); var checkbox = document.getElementById(checkboxId);
console.log(checkbox); // console.log(checkbox);
// Check if the checkbox is checked
if (checkbox.checked) {
// Get the habit id from the checkbox id attribute // Get the habit id from the checkbox id attribute
var habitId = checkboxId; var habitId = checkboxId;
@ -119,17 +118,14 @@
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
}) }).then(function (response) {
.then(function (response) {
// Handle the success response if needed // Handle the success response if needed
console.log(response.data); console.log(response.data);
}) }).catch(function (error) {
.catch(function (error) {
// Handle the error if needed // Handle the error if needed
console.error('Error:', error); console.error('Error:', error);
}); });
} }
}
</script> </script>
</div> </div>