Finished checkbox checking of habits
This commit is contained in:
parent
906473cb81
commit
395133728f
59
app.py
59
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
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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,27 +108,23 @@
|
||||
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) {
|
||||
// Get the habit id from the checkbox id attribute
|
||||
var habitId = checkboxId;
|
||||
// console.log(checkbox);
|
||||
|
||||
// Make a POST request to /check with the habit id
|
||||
axios.post('/check', { habitId: habitId }, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
.then(function (response) {
|
||||
// Handle the success response if needed
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(function (error) {
|
||||
// Handle the error if needed
|
||||
console.error('Error:', error);
|
||||
});
|
||||
}
|
||||
// Get the habit id from the checkbox id attribute
|
||||
var habitId = checkboxId;
|
||||
|
||||
// Make a POST request to /check with the habit id
|
||||
axios.post('/check', { habitId: habitId }, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}).then(function (response) {
|
||||
// Handle the success response if needed
|
||||
console.log(response.data);
|
||||
}).catch(function (error) {
|
||||
// Handle the error if needed
|
||||
console.error('Error:', error);
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</div>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user