X-Git-Url: https://apps.delaporte.us/gitweb/matrix-goal-bot.git/blobdiff_plain/6c0064a26d3facaea4590bc9d02558e6e6fdb937..421f514aa0533cdcead40f2c689548b96a29885e:/matrix_goal_bot/db.py diff --git a/matrix_goal_bot/db.py b/matrix_goal_bot/db.py index bc8cdbc..3f83341 100644 --- a/matrix_goal_bot/db.py +++ b/matrix_goal_bot/db.py @@ -28,6 +28,7 @@ class PreferGoal(SQLModel, table=True): class Goal(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str = Field(default=None, unique=True) + start_date: dt | None = Field(default=dt.today()) complete_date: dt | None = Field(default=None) vision_id: int | None = Field(default=None, foreign_key="vision.id") notes: str | None = Field(default=None) @@ -64,6 +65,13 @@ def pretty_print(goal, tasks): return results +def _get_with_id(cls, item_id): + return select(cls).where(cls.id == item_id) + +def _get_with_name(cls, name): + return select(cls).where(func.lower(cls.name) == func.lower(name)) + + class Data(): def __init__(self, db_file=None): if not db_file: @@ -80,12 +88,19 @@ class Data(): SQLModel.metadata.create_all(self.engine) return 'Success' + def migrate(self): + with Session(self.engine) as session: + session.exec(text(''' alter table goal add start_date DATETIME; ''')) + session.commit() + return 'Success' + def _set_pref_goal_id(self, goal_id): with Session(self.engine) as session: prefs = session.exec(select(PreferGoal)).all() for pref in prefs: session.delete(pref) + session.commit() pref = PreferGoal() pref.goal_id = goal_id session.add(pref) @@ -97,6 +112,13 @@ class Data(): return pref.goal_id + def clear(self, goal_id, clear_all=False): + with Session(self.engine) as session: + goal = session.exec(_get_with_id(Goal, goal_id)).first() + for item in goal.tasks: + if clear_all or item.complete_date: + session.delete(item) + session.commit() def get_all_visions(self): with Session(self.engine) as session: @@ -109,6 +131,13 @@ class Data(): cmd = select(Goal).where(Goal.complete_date == None) return session.exec(cmd).all() + def get_current_goals(self): + '''List current goals''' + with Session(self.engine) as session: + cmd = select(Goal).where(Goal.complete_date == None).where(Goal.start_date <= dt.today()) + results = session.exec(cmd).all() + logger.debug(f'Current goals: {results}') + return results def get_done_goals(self): '''List completed goals''' @@ -143,7 +172,6 @@ class Data(): def complete_goal(self, goal_id): - self._set_pref_goal_id(goal_id) vision_id = -1 with Session(self.engine) as session: cmd = select(Goal).where(Goal.id == goal_id) @@ -153,6 +181,31 @@ class Data(): session.commit() return vision_id + + def put_off_goal(self, goal_id): + vision_id = -1 + with Session(self.engine) as session: + cmd = select(Goal).where(Goal.id == goal_id) + goal = session.exec(cmd).first() + goal.start_date = dt.today() + timedelta(days=14) + vision_id = goal.vision_id + session.commit() + return vision_id + + def start_goal(self, goal_id): + vision = None + with Session(self.engine) as session: + cmd = select(Goal).where(Goal.id == goal_id) + goal = session.exec(cmd).first() + goal.start_date = dt.today() + vision_id = goal.vision_id + session.commit() + if vision: + return (vision, vision.goals) + else: + return ('No Vision Assigned', []) + + def rename_goal(self, goal_id, name): self._set_pref_goal_id(goal_id) vision_id = -1 @@ -204,9 +257,14 @@ class Data(): def remove_task(self, task_id): task_name = '' + logger.debug(f'Removing task with id {task_id}') with Session(self.engine) as session: - cmd = select(Task).where(Task.id == task_id) + cmd = select(Task).where(Task.id == int(task_id)) to_remove = session.exec(cmd).first() + if not to_remove: + return f'Unable to find task with id {task_id}' + + logger.debug(f'Removing task with name {to_remove.name}') task_name = to_remove.name session.delete(to_remove) session.commit()