HabitTracker/models/Habit.py

118 lines
3.5 KiB
Python
Raw Normal View History

2024-02-02 09:03:40 +01:00
import json
from dataclasses import dataclass
2024-01-26 10:01:02 +01:00
from datetime import datetime
from models.HabitTrackings import HabitTrackings
2024-01-26 10:01:02 +01:00
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:
# 0: Tag
2024-01-17 10:32:52 +01:00
# 1: Woche (Default)
# 2: Monat
# 3: Jahr
@dataclass
class Habit:
id: int
2024-02-12 21:07:55 +01:00
list_id: int
name: str
note: str
times: int
unit: int
2024-01-17 10:32:52 +01:00
slot: int
percentage: int = 0
def __post_init__(self):
self.fill_statistics()
@staticmethod
2024-02-12 21:07:55 +01:00
def create(list_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1):
slot = get_next_slot(list_id)
id = create_habit(list_id, name, times, unit, slot, note)
return Habit(id, list_id, name, note, times, unit, slot)
@staticmethod
def get(id: int):
habit = get_habit(id)
habit = Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None
return habit
2024-01-26 09:29:47 +01:00
def update(self, name: str=None, note: str=None, times: int=None, unit: int=None):
update_habit(self.id, name, note, times, unit)
if name is not None:
self.name = name
if note is not None:
self.note = note
if times is not None:
self.times = times
if unit is not None:
self.unit = unit
2024-01-26 09:29:47 +01:00
def update_slot(self, new_slot: int):
2024-02-12 21:07:55 +01:00
slots = get_slots(self.list_id)
2024-01-26 09:29:47 +01:00
if new_slot > self.slot:
slots = slots[self.slot:new_slot]
for slot in slots:
update_slot(slot[0], slot[1]-1)
if new_slot < self.slot:
slots = slots[new_slot-1:self.slot-1]
for slot in slots:
update_slot(slot[0], slot[1]+1)
update_slot(self.id, new_slot)
2024-01-26 09:29:47 +01:00
def delete(self):
2024-02-12 21:07:55 +01:00
slots = get_slots(self.list_id)[self.slot+1:]
print(slots)
for slot in slots:
update_slot(slot[0], slot[1] - 1)
2024-01-26 09:29:47 +01:00
delete_habit(self.id)
def get_habitTrackings(self) -> list[HabitTrackings]:
2024-01-26 10:01:02 +01:00
trackings = []
for rawTracking in get_habitTrackings_by_habit_id(self.id):
2024-02-12 21:07:55 +01:00
trackings.append(HabitTrackings(rawTracking[0], rawTracking[1], datetime.strptime(rawTracking[2], "%Y-%m-%dT%H:%M:%S.%f")))
2024-01-26 10:01:02 +01:00
return trackings
def fill_statistics(self):
count = 0
self.checked = False
for tracking in self.get_habitTrackings():
2024-02-02 08:07:47 +01:00
if tracking.created_at == datetime.today():
self.checked = True
# day
if self.unit == 0:
2024-02-02 08:07:47 +01:00
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.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)
2024-02-02 08:56:04 +01:00
def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)