Compare commits
No commits in common. "cb1991e2cc22fd7f8b523a415829cd32c4c2cff4" and "f07456a95bf4c1d0cc87ae5ebe7b52298b918dcc" have entirely different histories.
cb1991e2cc
...
f07456a95b
111
models/Habit.py
111
models/Habit.py
@ -2,16 +2,16 @@ import json
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from models.HabitTrackings import HabitTrackings
|
from models.HabitTracking import HabitTracking
|
||||||
from db.SQLiteClient import update_slot, create_habit, get_habit, delete_habit, get_next_slot, \
|
from db.SQLiteClient import (create_habit, get_habit, update_habit, delete_habit, get_next_slot, get_slots, update_slot,
|
||||||
get_habitTrackings_by_habit_id, get_slots, update_habit, get_habitList
|
get_habitTrackings, get_habitList)
|
||||||
|
|
||||||
|
|
||||||
# Unit wird als Integers wie folgt gemessen:
|
# unit will be represented by integers like this:
|
||||||
# 0: Tag
|
# 0: day
|
||||||
# 1: Woche (Default)
|
# 1: week (default)
|
||||||
# 2: Monat
|
# 2: month
|
||||||
# 3: Jahr
|
# 3: year
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Habit:
|
class Habit:
|
||||||
@ -28,83 +28,74 @@ class Habit:
|
|||||||
self.fill_statistics()
|
self.fill_statistics()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(list_id: int, name: str, times: int, note: str | None = None, unit: int | None = 1):
|
def create(list_id: int, name: str, times: int, note: str = None, unit: int = 1):
|
||||||
slot = get_next_slot(list_id)
|
slot = get_next_slot(list_id)
|
||||||
id = create_habit(list_id, name, times, unit, slot, note)
|
id = create_habit(list_id, name, note, times, unit, slot)
|
||||||
return Habit(id, list_id, name, note, times, unit, slot)
|
return Habit(id, list_id, name, note, times, unit, slot)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get(id: int):
|
def get(id: int):
|
||||||
habit = get_habit(id)
|
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(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):
|
# Updates: name, note, times, unit
|
||||||
update_habit(self.id, name, note, times, unit)
|
def update(self):
|
||||||
if name is not None:
|
update_habit(self.id, self.name, self.note, self.times, self.unit)
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
|
# Updates the slot and reorders the HabitList accordingly
|
||||||
def update_slot(self, new_slot: int):
|
def update_slot(self, new_slot: int):
|
||||||
|
# Fetches a list with the following structure [(id, slot), (id, slot), ...]
|
||||||
slots = get_slots(self.list_id)
|
slots = get_slots(self.list_id)
|
||||||
if new_slot > self.slot:
|
|
||||||
slots = slots[self.slot:new_slot]
|
# 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)]
|
||||||
for slot in slots:
|
for slot in slots:
|
||||||
update_slot(slot[0], slot[1]-1)
|
update_slot(slot[0], slot[1]-1)
|
||||||
if new_slot < self.slot:
|
if new_slot < self.slot: # Example self.slot=4 new_slot=1
|
||||||
slots = slots[new_slot-1:self.slot-1]
|
slots = slots[new_slot-1:self.slot-1] # Expected list: [(id, 1), (id, 2), (id, 3)]
|
||||||
for slot in slots:
|
for slot in slots:
|
||||||
update_slot(slot[0], slot[1]+1)
|
update_slot(slot[0], slot[1]+1)
|
||||||
|
|
||||||
|
# Update the slot of the current habit
|
||||||
update_slot(self.id, new_slot)
|
update_slot(self.id, new_slot)
|
||||||
|
|
||||||
|
|
||||||
|
# Deletes the Habit
|
||||||
def delete(self):
|
def delete(self):
|
||||||
|
# Reorders the slots
|
||||||
slots = get_slots(self.list_id)[self.slot+1:]
|
slots = get_slots(self.list_id)[self.slot+1:]
|
||||||
for slot in slots:
|
for slot in slots:
|
||||||
update_slot(slot[0], slot[1] - 1)
|
update_slot(slot[0], slot[1] - 1)
|
||||||
|
|
||||||
|
# Deletes all track-records associated with the Habit
|
||||||
|
trackings = self.get_habitTrackings()
|
||||||
|
for tracking in trackings:
|
||||||
|
tracking.delete()
|
||||||
|
|
||||||
|
# Deletes the current Habit
|
||||||
delete_habit(self.id)
|
delete_habit(self.id)
|
||||||
|
|
||||||
def get_habitTrackings(self) -> list[HabitTrackings]:
|
|
||||||
|
# Returns all track-records for a Habit
|
||||||
|
def get_habitTrackings(self) -> list:
|
||||||
trackings = []
|
trackings = []
|
||||||
for rawTracking in get_habitTrackings_by_habit_id(self.id):
|
for rawTracking in get_habitTrackings(self.id):
|
||||||
trackings.append(HabitTrackings(rawTracking[0], rawTracking[1],
|
trackings.append(HabitTracking(rawTracking[0], rawTracking[1],
|
||||||
datetime.strptime(rawTracking[2], "%Y-%m-%dT%H:%M:%S.%f")))
|
datetime.strptime(rawTracking[2], "%Y-%m-%dT%H:%M:%S.%f")))
|
||||||
return trackings
|
return trackings
|
||||||
|
|
||||||
def getStreak(self):
|
|
||||||
streak = 0
|
|
||||||
trackings = []
|
|
||||||
|
|
||||||
for rawTracking in get_habitTrackings_by_habit_id(self.id):
|
|
||||||
trackings.append(HabitTrackings(datetime.strptime(rawTracking[2], "%Y-%m-%d")))
|
|
||||||
|
|
||||||
trackings.sort(reverse=True)
|
|
||||||
if current_date == tracking[0]:
|
|
||||||
streak += 1
|
|
||||||
for habitdate in trackings[1:]:
|
|
||||||
future_date = habitdate - relativedelta(day=1)
|
|
||||||
if future_date - habitdate == 0-0-1:
|
|
||||||
streak + 1
|
|
||||||
|
|
||||||
|
|
||||||
future_date = habitdate - relativedelta(day=1)
|
|
||||||
|
|
||||||
for habitdate in trackings:
|
|
||||||
current_date = date.today()
|
|
||||||
if current_date == habitdate or current_date == habitdate - relativedelta(day=1):
|
|
||||||
streak += 1
|
|
||||||
|
|
||||||
future_date = habitdate - relativedelta(day=1)
|
|
||||||
|
|
||||||
return streak
|
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
||||||
|
# Saves the progress of the Habit in the attribute percentage
|
||||||
def fill_statistics(self):
|
def fill_statistics(self):
|
||||||
count = 0
|
count = 0
|
||||||
self.checked = False
|
self.checked = False
|
||||||
@ -131,13 +122,7 @@ class Habit:
|
|||||||
|
|
||||||
self.percentage = int(count / self.times * 100)
|
self.percentage = int(count / self.times * 100)
|
||||||
|
|
||||||
|
|
||||||
|
# Converts the Habit data to a json format
|
||||||
def to_json(self):
|
def to_json(self):
|
||||||
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
|
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
|
||||||
|
|
||||||
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
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user