X-Git-Url: https://apps.delaporte.us/gitweb/matrix-goal-bot.git/blobdiff_plain/303394a7dcea600b7f4e9db20a7056c8433eef75..d1fa45339a7512476475a53880712c64f84f7b3a:/matrix_goal_bot/db.py diff --git a/matrix_goal_bot/db.py b/matrix_goal_bot/db.py index fa3f082..a9d1b87 100644 --- a/matrix_goal_bot/db.py +++ b/matrix_goal_bot/db.py @@ -2,6 +2,7 @@ import logging import os from datetime import datetime as dt from datetime import timedelta +from typing import List from sqlmodel import Field, Session, SQLModel, create_engine, select, text, Relationship @@ -56,11 +57,11 @@ class Task(SQLModel, table=True): 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) + vision: str | None = 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})' + return f'{self.goal} ({completed} {self.vision})' def pretty_print(goal, tasks): results = [] @@ -83,6 +84,22 @@ def _get_with_name(cls, name): return select(cls).where(func.lower(cls.name) == func.lower(name)) +def _get_goals_by_vision(goals) -> List[str]: + results = [] + visions = {} + + for goal in goals: + vision = str(goal.vision) + if not vision in visions: + visions[vision] = [] + visions[vision].append(str(goal)) + for vision in visions: + results.append(f' ## {vision}') + for item in visions[vision]: + results.append(item) + return results + + class Data(): def __init__(self, db_file=None): if not db_file: @@ -136,26 +153,30 @@ class Data(): cmd = select(Vision).where(Vision.complete_date == None) return session.exec(cmd).all() - def get_all_goals(self): + def get_all_goals(self) -> List[str]: '''List all goals''' + results = [] with Session(self.engine) as session: cmd = select(Goal).where(Goal.complete_date == None) - return session.exec(cmd).all() + goals = session.exec(cmd).all() + results.append(_get_goals_by_vision(goals)) + return results + 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 + goals = session.exec(cmd).all() + logger.debug(f'Current goals: {goals}') + return _get_goals_by_vision(goals) def get_done_goals(self): '''List completed goals''' with Session(self.engine) as session: cmd = select(Goal).where(Goal.complete_date != None) - return session.exec(cmd).all() - + goals = session.exec(cmd).all() + return _get_goals_by_vision(goals) def get_goal(self, goal_id): self._set_pref_goal_id(goal_id) @@ -203,15 +224,15 @@ class Data(): return vision_id - def put_off_goal(self, goal_id): - vision_id = -1 + def put_off_vision(self, vision_id): 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 + # vision = session.exec(cmd).first() + cmd = select(Goal).where(Goal.vision_id == vision_id) + goals = session.exec(cmd).all() + for goal in goals: + goal.start_date = dt.today() + timedelta(days=14) session.commit() - return vision_id + return True def start_goal(self, goal_id): vision = None @@ -299,11 +320,12 @@ class Data(): goal = session.exec(cmd).first() goal.vision_id = vision_id session.commit() + return True def visionless(self): '''Get goals with no vision''' with Session(self.engine) as session: - cmd = select(Goal).where(Goal.vision_id == None) + cmd = select(Goal).where(Goal.vision_id == None).where(Goal.complete_date == None) return session.exec(cmd).all()