]> us-delaporte.pagekite.me Git - matrix-goal-bot.git/blob - matrix_goal_bot/db.py
fixuo
[matrix-goal-bot.git] / matrix_goal_bot / db.py
1 import logging
2 import os
3 from datetime import datetime as dt
4
5 from sqlmodel import Field, Session, SQLModel, create_engine, select, text, Relationship
6
7
8 DATE_FORMAT = '%Y-%m-%d'
9
10 def today():
11 return dt.now().strftime(DATE_FORMAT)
12
13
14 logger = logging.getLogger(__name__)
15 logging.basicConfig(filename='goals.log', encoding='utf-8', level=logging.DEBUG)
16
17
18 class Vision(SQLModel, table=True):
19 id: int | None = Field(default=None, primary_key=True)
20 name: str = Field(default=None, unique=True)
21 complete_date: dt | None = Field(default=None)
22 goals: list["Goal"] = Relationship(back_populates="vision")
23 def __str__(self):
24 return f'{self.name} (Vision {self.id})'
25
26
27 class Goal(SQLModel, table=True):
28 id: int | None = Field(default=None, primary_key=True)
29 name: str = Field(default=None, unique=True)
30 complete_date: dt | None = Field(default=None)
31 vision_id: int | None = Field(default=None, foreign_key="vision.id")
32 notes: str | None = Field(default=None)
33 tasks: list["Task"] = Relationship(back_populates="goal")
34 vision: Vision = Relationship(back_populates="goals")
35 def __str__(self):
36 return f'{self.name} (Goal {self.id})'
37
38 class Task(SQLModel, table=True):
39 id: int | None = Field(default=None, primary_key=True)
40 name: str = Field(default=None, unique=True)
41 goal_id: int | None = Field(default=None, foreign_key="goal.id")
42 complete_date: dt | None = Field(default=None)
43 goal: Goal = Relationship(back_populates="tasks")
44 def __str__(self):
45 return f'{self.name} ({self.id})'
46
47
48 def pretty_print(goal, tasks):
49 results = []
50 results.append(f' ## {goal}')
51 if not tasks:
52 results.append('-- Nothing to list --')
53
54 for task in tasks:
55 if(task.complete_date):
56 results.append(f' - [x] {task}')
57 else:
58 results.append(f' - [ ] {task}')
59 return results
60
61
62 class Data():
63 def __init__(self, db_file=None):
64 if not db_file:
65 db_file = os.path.join(os.environ.get('MATRIX_DATA_DIR', ''), 'goals.db')
66
67 self.file = db_file
68 self.engine = self.get_engine()
69
70
71 def get_engine(self):
72 return create_engine(f"sqlite:///{self.file}")
73
74 def setup(self):
75 SQLModel.metadata.create_all(self.engine)
76 return 'Success'
77
78
79 def get_all_visions(self):
80 with Session(self.engine) as session:
81 cmd = select(Vision).where(Vision.complete_date == None)
82 return session.exec(cmd).all()
83
84 def print_ophan_tasks(self):
85 pass
86
87 def get_all_goals(self):
88 '''List all goals'''
89 with Session(self.engine) as session:
90 cmd = select(Goal).where(Goal.complete_date == None)
91 return session.exec(cmd).all()
92
93
94 def get_done_goals(self):
95 '''List all goals'''
96 with Session(self.engine) as session:
97 cmd = select(Goal).where(Goal.complete_date != None)
98 return session.exec(cmd).all()
99
100
101 def get_goal(self, id):
102 with Session(self.engine) as session:
103 cmd = select(Goal).where(Goal.id == id)
104 goal = session.exec(cmd).first()
105 return (goal, goal.tasks)
106
107 def get_vision(self, id):
108 with Session(self.engine) as session:
109 cmd = select(Vision).where(Vision.id == id)
110 vision = session.exec(cmd).first()
111 if vision:
112 return (vision, vision.goals)
113 else:
114 return ('Not found', [])
115
116
117 def complete_vision(self, id):
118 with Session(self.engine) as session:
119 cmd = select(Vision).where(Vision.id == id)
120 vision = session.exec(cmd).first()
121 vision.complete_date = dt.today()
122 session.commit()
123
124
125 def complete_goal(self, id):
126 vision_id = -1
127 with Session(self.engine) as session:
128 cmd = select(Goal).where(Goal.id == id)
129 goal = session.exec(cmd).first()
130 goal.complete_date = dt.today()
131 vision_id = goal.vision_id
132 session.commit()
133 return vision_id
134
135 def complete_task(self, id):
136 with Session(self.engine) as session:
137 cmd = select(Task).where(Task.id == id)
138 task = session.exec(cmd).first()
139 task.complete_date = dt.today()
140 goal_id = task.goal_id
141 session.commit()
142 return goal_id
143
144 def add_with_name(self, cls, name):
145 id = -1
146 with Session(self.engine) as session:
147 new = cls(name=name)
148 session.add(new)
149 session.commit()
150 id = new.id
151 return id
152
153 def add_vision(self, name):
154 return self.add_with_name(Vision, name)
155
156 def add_goal(self, name):
157 return self.add_with_name(Goal, name)
158
159 def add_task(self, goal_id, name):
160 result = self.add_with_name(Task, name)
161 with Session(self.engine) as session:
162 cmd = select(Task).where(Task.name == name)
163 new = session.exec(cmd).first()
164 new.goal_id = goal_id
165 session.commit()
166 return result
167
168 def assign_vision(self, goal_id, vision_id):
169 '''Goals may move between visions'''
170 with Session(self.engine) as session:
171 cmd = select(Goal).where(Goal.id == goal_id)
172 goal = session.exec(cmd).first()
173 goal.vision_id = vision_id
174 session.commit()
175
176 def add_note(self, note, goal):
177 '''Notes print with the goal.'''
178 pass
179
180 # Someday: Notes on each
181
182 def list(self):
183 results = []
184 with Session(self.engine) as session:
185 cmd = select(Chore)
186 chores = session.exec(cmd).all()
187 if(not chores):
188 retuls.append('None Found')
189
190 for chore in chores:
191 results.append(Data.check_chore(session, chore.id))
192 return results
193