]> apps.delaporte.us Git - matrix-goal-bot.git/blobdiff - matrix_goal_bot/db.py
Logging during invoke
[matrix-goal-bot.git] / matrix_goal_bot / db.py
index d7896e08d062c84797ba9e2dadf1defaad67f9f2..a746c597c3ad808d51297f76866733da4df96974 100644 (file)
@@ -65,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:
@@ -81,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)
@@ -98,7 +112,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:
@@ -121,7 +135,9 @@ class Data():
         '''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()
+            results = session.exec(cmd).all()
+            logger.debug(f'Current goals: {results}')
+            return results
 
     def get_done_goals(self):
         '''List completed goals'''
@@ -176,6 +192,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)