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 = []
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:
# day
if habit.unit == 0:
if tracking.created_at.date() == datetime.date.today():
tracking.delete()
unchecked = True
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
}

View File

@ -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

View File

@ -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)

View File

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