2024-02-02 09:03:40 +01:00
|
|
|
import json
|
2024-01-13 13:30:29 +01:00
|
|
|
from dataclasses import dataclass
|
2024-01-26 10:01:02 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2024-02-20 10:12:20 +01:00
|
|
|
from models.HabitTracking import HabitTracking
|
|
|
|
|
from db.SQLiteClient import (create_habit, get_habit, update_habit, delete_habit, get_next_slot, get_slots, update_slot,
|
|
|
|
|
get_habitTrackings, get_habitList)
|
2024-01-17 11:46:07 +01:00
|
|
|
|
2024-01-13 13:30:29 +01:00
|
|
|
|
2024-02-20 10:12:20 +01:00
|
|
|
# unit will be represented by integers like this:
|
|
|
|
|
# 0: day
|
|
|
|
|
# 1: week (default)
|
|
|
|
|
# 2: month
|
|
|
|
|
# 3: year
|
2024-01-16 10:58:25 +01:00
|
|
|
|
2024-01-13 13:30:29 +01:00
|
|
|
@dataclass
|
|
|
|
|
class Habit:
|
|
|
|
|
id: int
|
2024-02-12 21:07:55 +01:00
|
|
|
list_id: int
|
2024-01-13 13:30:29 +01:00
|
|
|
name: str
|
|
|
|
|
note: str
|
|
|
|
|
times: int
|
2024-01-16 10:58:25 +01:00
|
|
|
unit: int
|
2024-01-17 10:32:52 +01:00
|
|
|
slot: int
|
2024-01-30 10:34:59 +01:00
|
|
|
percentage: int = 0
|
|
|
|
|
|
|
|
|
|
def __post_init__(self):
|
|
|
|
|
self.fill_statistics()
|
2024-01-13 13:30:29 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
2024-02-20 10:12:20 +01:00
|
|
|
def create(list_id: int, name: str, times: int, note: str = None, unit: int = 1):
|
2024-02-12 21:07:55 +01:00
|
|
|
slot = get_next_slot(list_id)
|
2024-02-20 10:12:20 +01:00
|
|
|
id = create_habit(list_id, name, note, times, unit, slot)
|
2024-02-12 21:07:55 +01:00
|
|
|
return Habit(id, list_id, name, note, times, unit, slot)
|
2024-01-13 13:30:29 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get(id: int):
|
|
|
|
|
habit = get_habit(id)
|
2024-02-20 10:12:20 +01:00
|
|
|
return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None
|
2024-02-16 08:28:47 +01:00
|
|
|
|
2024-01-13 13:30:29 +01:00
|
|
|
|
2024-02-20 10:12:20 +01:00
|
|
|
# Updates: name, note, times, unit
|
|
|
|
|
def update(self):
|
|
|
|
|
update_habit(self.id, self.name, self.note, self.times, self.unit)
|
2024-01-26 09:29:47 +01:00
|
|
|
|
2024-02-20 10:12:20 +01:00
|
|
|
|
|
|
|
|
# Updates the slot and reorders the HabitList accordingly
|
2024-01-26 09:29:47 +01:00
|
|
|
def update_slot(self, new_slot: int):
|
2024-02-20 10:12:20 +01:00
|
|
|
# Fetches a list with the following structure [(id, slot), (id, slot), ...]
|
2024-02-12 21:07:55 +01:00
|
|
|
slots = get_slots(self.list_id)
|
2024-02-20 10:12:20 +01:00
|
|
|
|
|
|
|
|
# Splits the list depending on whether the new slot is higher or lower than the current one
|
|
|
|
|
if new_slot > self.slot: # Example self.slot=1 new_slot=4
|
|
|
|
|
slots = slots[self.slot:new_slot] # Expected list: [(id, 2), (id, 3), (id, 4)]
|
2024-01-26 09:29:47 +01:00
|
|
|
for slot in slots:
|
|
|
|
|
update_slot(slot[0], slot[1]-1)
|
2024-02-20 10:12:20 +01:00
|
|
|
if new_slot < self.slot: # Example self.slot=4 new_slot=1
|
|
|
|
|
slots = slots[new_slot-1:self.slot-1] # Expected list: [(id, 1), (id, 2), (id, 3)]
|
2024-01-26 09:29:47 +01:00
|
|
|
for slot in slots:
|
|
|
|
|
update_slot(slot[0], slot[1]+1)
|
2024-02-20 10:12:20 +01:00
|
|
|
|
|
|
|
|
# Update the slot of the current habit
|
2024-02-02 08:30:34 +01:00
|
|
|
update_slot(self.id, new_slot)
|
2024-01-26 09:29:47 +01:00
|
|
|
|
2024-02-20 10:12:20 +01:00
|
|
|
|
|
|
|
|
# Deletes the Habit
|
2024-01-26 09:29:47 +01:00
|
|
|
def delete(self):
|
2024-02-20 10:12:20 +01:00
|
|
|
# Reorders the slots
|
2024-02-12 21:07:55 +01:00
|
|
|
slots = get_slots(self.list_id)[self.slot+1:]
|
2024-02-02 09:20:19 +01:00
|
|
|
for slot in slots:
|
|
|
|
|
update_slot(slot[0], slot[1] - 1)
|
2024-02-20 10:12:20 +01:00
|
|
|
|
|
|
|
|
# Deletes all track-records associated with the Habit
|
|
|
|
|
trackings = self.get_habitTrackings()
|
|
|
|
|
for tracking in trackings:
|
|
|
|
|
tracking.delete()
|
|
|
|
|
|
|
|
|
|
# Deletes the current Habit
|
2024-01-26 09:29:47 +01:00
|
|
|
delete_habit(self.id)
|
2024-01-26 09:06:47 +01:00
|
|
|
|
2024-02-20 10:12:20 +01:00
|
|
|
|
|
|
|
|
# Returns all track-records for a Habit
|
|
|
|
|
def get_habitTrackings(self) -> list:
|
2024-01-26 10:01:02 +01:00
|
|
|
trackings = []
|
2024-02-20 10:12:20 +01:00
|
|
|
for rawTracking in get_habitTrackings(self.id):
|
|
|
|
|
trackings.append(HabitTracking(rawTracking[0], rawTracking[1],
|
2024-02-16 08:28:47 +01:00
|
|
|
datetime.strptime(rawTracking[2], "%Y-%m-%dT%H:%M:%S.%f")))
|
2024-01-26 10:01:02 +01:00
|
|
|
return trackings
|
2024-01-30 10:34:59 +01:00
|
|
|
|
2024-02-16 09:28:00 +01:00
|
|
|
|
2024-02-20 10:12:20 +01:00
|
|
|
# Returns the HabitList in which the Habit is located
|
|
|
|
|
def habit_list(self) -> list:
|
|
|
|
|
from models.HabitList import HabitList
|
|
|
|
|
raw_habitLists = get_habitList(self.list_id)
|
|
|
|
|
return HabitList(raw_habitLists[0], raw_habitLists[1], raw_habitLists[2]) if raw_habitLists else None
|
2024-02-16 09:28:00 +01:00
|
|
|
|
|
|
|
|
|
2024-02-20 10:12:20 +01:00
|
|
|
# Saves the progress of the Habit in the attribute percentage
|
2024-01-30 10:34:59 +01:00
|
|
|
def fill_statistics(self):
|
|
|
|
|
count = 0
|
2024-02-14 10:36:02 +01:00
|
|
|
self.checked = False
|
2024-01-30 10:34:59 +01:00
|
|
|
for tracking in self.get_habitTrackings():
|
2024-02-14 10:41:03 +01:00
|
|
|
if tracking.created_at.date() == datetime.today().date():
|
|
|
|
|
self.checked = True
|
|
|
|
|
|
2024-01-30 10:34:59 +01:00
|
|
|
# day
|
|
|
|
|
if self.unit == 0:
|
2024-02-13 11:17:06 +01:00
|
|
|
if tracking.created_at.date() == datetime.today().date():
|
2024-01-30 10:34:59 +01:00
|
|
|
count += 1
|
|
|
|
|
# week
|
|
|
|
|
elif self.unit == 1:
|
|
|
|
|
if tracking.created_at.isocalendar()[1] == datetime.today().isocalendar()[1]:
|
|
|
|
|
count += 1
|
|
|
|
|
# month
|
|
|
|
|
elif self.unit == 2:
|
2024-01-30 10:37:38 +01:00
|
|
|
if tracking.created_at.month == datetime.today().month:
|
2024-01-30 10:34:59 +01:00
|
|
|
count += 1
|
|
|
|
|
# year
|
|
|
|
|
elif self.unit == 3:
|
|
|
|
|
if tracking.created_at.year == datetime.today().year:
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
|
|
self.percentage = int(count / self.times * 100)
|
2024-02-02 09:20:19 +01:00
|
|
|
|
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)
|
2024-02-16 08:28:47 +01:00
|
|
|
|
|
|
|
|
def habit_list(self):
|
|
|
|
|
from models.HabitList import HabitList
|
|
|
|
|
raw_habitLists = get_habitList(self.list_id)
|
|
|
|
|
return HabitList(raw_habitLists[0], raw_habitLists[1], raw_habitLists[2],
|
|
|
|
|
datetime.strptime(raw_habitLists[3], "%Y-%m-%dT%H:%M:%S.%f"),
|
|
|
|
|
datetime.strptime(raw_habitLists[4], "%Y-%m-%dT%H:%M:%S.%f")) \
|
|
|
|
|
if raw_habitLists else None
|