]> apps.delaporte.us Git - matrix-goal-bot.git/blobdiff - matrix_goal_bot/db.py
Update later command to act on entire visions
[matrix-goal-bot.git] / matrix_goal_bot / db.py
index 3f833416a983a18aff675a4416b09c6919369de7..22dca51b66a62e1136691c6698abb547bfb166f8 100644 (file)
@@ -1,6 +1,8 @@
 import logging
 import os
 from datetime import datetime as dt
 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
 
 
 from sqlmodel import Field, Session, SQLModel, create_engine, select, text, Relationship
 
@@ -41,6 +43,7 @@ class Goal(SQLModel, table=True):
             completed = self.complete_date.strftime(DATE_FORMAT)
             return f'(Done {completed}) {self.name} (Goal {self.id})'
 
             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)
 class Task(SQLModel, table=True):
     id: int | None = Field(default=None, primary_key=True)
     name: str = Field(default=None, unique=True)
@@ -51,6 +54,15 @@ class Task(SQLModel, table=True):
         return f'{self.name} ({self.id})'
 
 
         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 | 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} {self.vision})'
+
 def pretty_print(goal, tasks):
     results = []
     results.append(f' ## {goal}')
 def pretty_print(goal, tasks):
     results = []
     results.append(f' ## {goal}')
@@ -72,6 +84,22 @@ def _get_with_name(cls, name):
     return select(cls).where(func.lower(cls.name) == func.lower(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:
 class Data():
     def __init__(self, db_file=None):
         if not db_file:
@@ -125,26 +153,30 @@ class Data():
             cmd = select(Vision).where(Vision.complete_date == None)
             return session.exec(cmd).all()
 
             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'''
         '''List all goals'''
+        results = []
         with Session(self.engine) as session:
             cmd = select(Goal).where(Goal.complete_date == None)
         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())
 
     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)
 
     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)
 
     def get_goal(self, goal_id):
         self._set_pref_goal_id(goal_id)
@@ -178,19 +210,27 @@ class Data():
             goal = session.exec(cmd).first()
             goal.complete_date = dt.today()
             vision_id = goal.vision_id
             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
 
 
             session.commit()
         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:
         with Session(self.engine) as session:
-            cmd = select(Goal).where(Goal.id == goal_id)
+            cmd = select(Goal).where(Vision.id == vision_id)
             goal = session.exec(cmd).first()
             goal.start_date = dt.today() + timedelta(days=14)
             goal = session.exec(cmd).first()
             goal.start_date = dt.today() + timedelta(days=14)
-            vision_id = goal.vision_id
             session.commit()
             session.commit()
-        return vision_id
+        return True
 
     def start_goal(self, goal_id):
         vision = None
 
     def start_goal(self, goal_id):
         vision = None
@@ -278,11 +318,12 @@ class Data():
             goal = session.exec(cmd).first()
             goal.vision_id = vision_id
             session.commit()
             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:
 
     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()
 
 
             return session.exec(cmd).all()
 
 
@@ -290,5 +331,12 @@ class Data():
         '''Notes print with the goal.'''
         pass
 
         '''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
 
     # Someday: Notes on each