Finished method for habit percentage and checked init
This commit is contained in:
parent
27fbebca98
commit
51f263061f
28
app.py
28
app.py
@ -126,34 +126,6 @@ def index():
|
||||
habits = []
|
||||
name = "Bitte melde dich an."
|
||||
|
||||
# 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
|
||||
|
||||
# Sort habits by whether they have been checked today and then by slot
|
||||
habits.sort(key=lambda habit: (habit.checked, habit.slot))
|
||||
|
||||
|
||||
@ -21,6 +21,10 @@ class Habit:
|
||||
times: int
|
||||
unit: int
|
||||
slot: int
|
||||
percentage: int = 0
|
||||
|
||||
def __post_init__(self):
|
||||
self.fill_statistics()
|
||||
|
||||
@staticmethod
|
||||
def create(user_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1):
|
||||
@ -31,7 +35,9 @@ class Habit:
|
||||
@staticmethod
|
||||
def get(id: int):
|
||||
habit = get_habit(id)
|
||||
return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None
|
||||
habit = Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None
|
||||
|
||||
return habit
|
||||
|
||||
def update(self, name: str=None, note: str=None, times: int=None, unit: int=None):
|
||||
update_habit(self.id, name, note, times, unit)
|
||||
@ -69,3 +75,33 @@ class Habit:
|
||||
trackings.append(HabitTrackings(rawTracking[0], rawTracking[1], rawTracking[2], datetime.strptime(rawTracking[3], "%Y-%m-%dT%H:%M:%S.%f")))
|
||||
|
||||
return trackings
|
||||
|
||||
|
||||
def fill_statistics(self):
|
||||
count = 0
|
||||
self.checked = False
|
||||
for tracking in self.get_habitTrackings():
|
||||
# day
|
||||
if self.unit == 0:
|
||||
if tracking.created_at == datetime.today():
|
||||
self.checked = True
|
||||
count += 1
|
||||
# week
|
||||
elif self.unit == 1:
|
||||
if tracking.created_at.isocalendar()[1] == datetime.today().isocalendar()[1]:
|
||||
self.checked = True
|
||||
count += 1
|
||||
# month
|
||||
elif self.unit == 2:
|
||||
if tracking.created_at.date().month == datetime.today().month:
|
||||
self.checked = True
|
||||
count += 1
|
||||
# year
|
||||
elif self.unit == 3:
|
||||
if tracking.created_at.year == datetime.today().year:
|
||||
self.checked = True
|
||||
count += 1
|
||||
|
||||
self.percentage = int(count / self.times * 100)
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user