X-Git-Url: https://us-delaporte.pagekite.me/gitweb/matrix-goal-bot.git/blobdiff_plain/bb2ab70af3a361241b656903691e4c9828ac376c..f705a76f09f0eb602ffeb84532d75101f563b21f:/matrix_goal_bot/db.py diff --git a/matrix_goal_bot/db.py b/matrix_goal_bot/db.py index 0041222..cd2faa9 100644 --- a/matrix_goal_bot/db.py +++ b/matrix_goal_bot/db.py @@ -4,10 +4,6 @@ from datetime import datetime as dt from sqlmodel import Field, Session, SQLModel, create_engine, select, text, Relationship -## Todo List -# - [ ] goal / vision / task - -DB_FILE = os.path.join(os.environ.get('MATRIX_DATA_DIR'), 'goals.db') DATE_FORMAT = '%Y-%m-%d' @@ -16,7 +12,7 @@ def today(): logger = logging.getLogger(__name__) -logging.basicConfig(filename='chore.log', encoding='utf-8', level=logging.DEBUG) +logging.basicConfig(filename='goals.log', encoding='utf-8', level=logging.DEBUG) class Vision(SQLModel, table=True): @@ -37,7 +33,11 @@ class Goal(SQLModel, table=True): tasks: list["Task"] = Relationship(back_populates="goal") vision: Vision = Relationship(back_populates="goals") def __str__(self): - return f'{self.name} (Goal {self.id})' + if not self.complete_date: + return f'{self.name} (Goal {self.id})' + else: + 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) @@ -64,7 +64,10 @@ def pretty_print(goal, tasks): class Data(): - def __init__(self, db_file = DB_FILE): + def __init__(self, db_file=None): + if not db_file: + db_file = os.path.join(os.environ.get('MATRIX_DATA_DIR', ''), 'goals.db') + self.file = db_file self.engine = self.get_engine() @@ -93,7 +96,7 @@ class Data(): def get_done_goals(self): - '''List all goals''' + '''List completed goals''' with Session(self.engine) as session: cmd = select(Goal).where(Goal.complete_date != None) return session.exec(cmd).all() @@ -133,6 +136,17 @@ class Data(): session.commit() return vision_id + def rename_goal(self, goal_id, name): + vision_id = -1 + with Session(self.engine) as session: + cmd = select(Goal).where(Goal.id == goal_id) + goal = session.exec(cmd).first() + goal.name = name + vision_id = goal.vision_id + session.commit() + return vision_id + + def complete_task(self, id): with Session(self.engine) as session: cmd = select(Task).where(Task.id == id) @@ -174,21 +188,16 @@ class Data(): goal.vision_id = vision_id session.commit() + def visionless(self): + '''Get goals with no vision''' + with Session(self.engine) as session: + cmd = select(Goal).where(Goal.vision_id == None) + return session.exec(cmd).all() + + def add_note(self, note, goal): '''Notes print with the goal.''' pass # Someday: Notes on each - def list(self): - results = [] - with Session(self.engine) as session: - cmd = select(Chore) - chores = session.exec(cmd).all() - if(not chores): - retuls.append('None Found') - - for chore in chores: - results.append(Data.check_chore(session, chore.id)) - return results -