X-Git-Url: https://us-delaporte.pagekite.me/gitweb/matrix-goal-bot.git/blobdiff_plain/81ed71ffac3a2a5e1d289043fa65022741027dd7..303394a7dcea600b7f4e9db20a7056c8433eef75:/matrix_goal_bot/db.py diff --git a/matrix_goal_bot/db.py b/matrix_goal_bot/db.py index d7896e0..fa3f082 100644 --- a/matrix_goal_bot/db.py +++ b/matrix_goal_bot/db.py @@ -1,6 +1,7 @@ import logging import os from datetime import datetime as dt +from datetime import timedelta from sqlmodel import Field, Session, SQLModel, create_engine, select, text, Relationship @@ -28,7 +29,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=today()) + 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) @@ -41,6 +42,7 @@ class Goal(SQLModel, table=True): completed = self.complete_date.strftime(DATE_FORMAT) return f'(Done {completed}) {self.name} (Goal {self.id})' + class Task(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) name: str = Field(default=None, unique=True) @@ -51,6 +53,15 @@ class Task(SQLModel, table=True): return f'{self.name} ({self.id})' +class Win(SQLModel, table=True): + id: int | None = Field(default=None, primary_key=True) + goal: str = Field(default=None, unique=False) + vision: str = Field(default=None, unique=False) + complete_date: dt | None = Field(default=dt.today()) + def __str__(self): + completed = self.complete_date.strftime(DATE_FORMAT) + return f'{self.goal} ({completed} {vision})' + def pretty_print(goal, tasks): results = [] results.append(f' ## {goal}') @@ -65,6 +76,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: @@ -81,12 +99,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) @@ -98,7 +123,7 @@ class Data(): return pref.goal_id - def clear_list(self, goal_id, clear_all=False): + 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: @@ -120,8 +145,10 @@ class Data(): 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 <= today()) - return session.exec(cmd).all() + 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''' @@ -162,6 +189,16 @@ class Data(): goal = session.exec(cmd).first() goal.complete_date = dt.today() vision_id = goal.vision_id + + cmd = select(Vision).where(Vision.id == vision_id) + win = Win() + win.goal = goal.name + + vision = session.exec(cmd).first() + if vision: + win.vision = vision.name + + session.add(win) session.commit() return vision_id @@ -176,6 +213,19 @@ class Data(): 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) @@ -261,5 +311,12 @@ class Data(): '''Notes print with the goal.''' pass + + def wins(self): + results = [] + with Session(self.engine) as session: + results = session.exec(select(Win)).all() + return results + # Someday: Notes on each