]>
us-delaporte.pagekite.me Git - matrix-goal-bot.git/blob - matrix_goal_bot/db.py
1d80aaa5ca5bb70528a7f59164ede5b64369aa3e
3 from datetime
import datetime
as dt
4 from datetime
import timedelta
6 from sqlmodel
import Field
, Session
, SQLModel
, create_engine
, select
, text
, Relationship
8 logger
= logging
.getLogger(__name__
)
10 DATE_FORMAT
= '%Y-%m-%d'
13 return dt
.now().strftime(DATE_FORMAT
)
16 class Vision(SQLModel
, table
=True):
17 id: int |
None = Field(default
=None, primary_key
=True)
18 name
: str = Field(default
=None, unique
=True)
19 complete_date
: dt |
None = Field(default
=None)
20 goals
: list["Goal"] = Relationship(back_populates
="vision")
22 return f
'{self.name} (Vision {self.id})'
25 class PreferGoal(SQLModel
, table
=True):
26 goal_id
: int |
None = Field(default
=None, foreign_key
="goal.id", primary_key
=True)
29 class Goal(SQLModel
, table
=True):
30 id: int |
None = Field(default
=None, primary_key
=True)
31 name
: str = Field(default
=None, unique
=True)
32 start_date
: dt |
None = Field(default
=dt
.today())
33 complete_date
: dt |
None = Field(default
=None)
34 vision_id
: int |
None = Field(default
=None, foreign_key
="vision.id")
35 notes
: str |
None = Field(default
=None)
36 tasks
: list["Task"] = Relationship(back_populates
="goal")
37 vision
: Vision
= Relationship(back_populates
="goals")
39 if not self
.complete_date
:
40 return f
'{self.name} (Goal {self.id})'
42 completed
= self
.complete_date
.strftime(DATE_FORMAT
)
43 return f
'(Done {completed}) {self.name} (Goal {self.id})'
45 class Task(SQLModel
, table
=True):
46 id: int |
None = Field(default
=None, primary_key
=True)
47 name
: str = Field(default
=None, unique
=True)
48 goal_id
: int |
None = Field(default
=None, foreign_key
="goal.id")
49 complete_date
: dt |
None = Field(default
=None)
50 goal
: Goal
= Relationship(back_populates
="tasks")
52 return f
'{self.name} ({self.id})'
55 def pretty_print(goal
, tasks
):
57 results
.append(f
' ## {goal}')
59 results
.append('-- Nothing to list --')
62 if(task
.complete_date
):
63 results
.append(f
' - [x] {task}')
65 results
.append(f
' - [ ] {task}')
69 def _get_with_id(cls
, item_id
):
70 return select(cls
).where(cls
.id == item_id
)
72 def _get_with_name(cls
, name
):
73 return select(cls
).where(func
.lower(cls
.name
) == func
.lower(name
))
77 def __init__(self
, db_file
=None):
79 db_file
= os
.path
.join(os
.environ
.get('MATRIX_DATA_DIR', ''), 'goal.db')
82 self
.engine
= self
.get_engine()
86 return create_engine(f
"sqlite:///{self.file}")
89 SQLModel
.metadata
.create_all(self
.engine
)
93 with Session(self
.engine
) as session
:
94 session
.exec(text(''' alter table goal add start_date DATETIME; '''))
99 def _set_pref_goal_id(self
, goal_id
):
100 with Session(self
.engine
) as session
:
101 prefs
= session
.exec(select(PreferGoal
)).all()
106 pref
.goal_id
= goal_id
110 def _get_pref_goal_id(self
):
111 with Session(self
.engine
) as session
:
112 pref
= session
.exec(select(PreferGoal
)).one()
116 def clear(self
, goal_id
, clear_all
=False):
117 with Session(self
.engine
) as session
:
118 goal
= session
.exec(_get_with_id(Goal
, goal_id
)).first()
119 for item
in goal
.tasks
:
120 if clear_all
or item
.complete_date
:
124 def get_all_visions(self
):
125 with Session(self
.engine
) as session
:
126 cmd
= select(Vision
).where(Vision
.complete_date
== None)
127 return session
.exec(cmd
).all()
129 def get_all_goals(self
):
131 with Session(self
.engine
) as session
:
132 cmd
= select(Goal
).where(Goal
.complete_date
== None)
133 return session
.exec(cmd
).all()
135 def get_current_goals(self
):
136 '''List current goals'''
137 with Session(self
.engine
) as session
:
138 cmd
= select(Goal
).where(Goal
.complete_date
== None).where(Goal
.start_date
<= dt
.today())
139 results
= session
.exec(cmd
).all()
140 logger
.debug(f
'Current goals: {results}')
143 def get_done_goals(self
):
144 '''List completed goals'''
145 with Session(self
.engine
) as session
:
146 cmd
= select(Goal
).where(Goal
.complete_date
!= None)
147 return session
.exec(cmd
).all()
150 def get_goal(self
, goal_id
):
151 self
._set
_pref
_goal
_id
(goal_id
)
152 with Session(self
.engine
) as session
:
153 cmd
= select(Goal
).where(Goal
.id == goal_id
)
154 goal
= session
.exec(cmd
).first()
155 return (goal
, goal
.tasks
)
157 def get_vision(self
, id):
158 with Session(self
.engine
) as session
:
159 cmd
= select(Vision
).where(Vision
.id == id)
160 vision
= session
.exec(cmd
).first()
162 return (vision
, vision
.goals
)
164 return ('Not found', [])
167 def complete_vision(self
, id):
168 with Session(self
.engine
) as session
:
169 cmd
= select(Vision
).where(Vision
.id == id)
170 vision
= session
.exec(cmd
).first()
171 vision
.complete_date
= dt
.today()
175 def complete_goal(self
, goal_id
):
177 with Session(self
.engine
) as session
:
178 cmd
= select(Goal
).where(Goal
.id == goal_id
)
179 goal
= session
.exec(cmd
).first()
180 goal
.complete_date
= dt
.today()
181 vision_id
= goal
.vision_id
186 def put_off_goal(self
, goal_id
):
188 with Session(self
.engine
) as session
:
189 cmd
= select(Goal
).where(Goal
.id == goal_id
)
190 goal
= session
.exec(cmd
).first()
191 goal
.start_date
= dt
.today() + timedelta(days
=14)
192 vision_id
= goal
.vision_id
196 def start_goal(self
, goal_id
):
198 with Session(self
.engine
) as session
:
199 cmd
= select(Goal
).where(Goal
.id == goal_id
)
200 goal
= session
.exec(cmd
).first()
201 goal
.start_date
= dt
.today()
202 vision_id
= goal
.vision_id
205 return (vision
, vision
.goals
)
207 return ('No Vision Assigned', [])
210 def rename_goal(self
, goal_id
, name
):
211 self
._set
_pref
_goal
_id
(goal_id
)
213 with Session(self
.engine
) as session
:
214 cmd
= select(Goal
).where(Goal
.id == goal_id
)
215 goal
= session
.exec(cmd
).first()
217 vision_id
= goal
.vision_id
222 def complete_task(self
, id):
223 with Session(self
.engine
) as session
:
224 cmd
= select(Task
).where(Task
.id == id)
225 task
= session
.exec(cmd
).first()
226 task
.complete_date
= dt
.today()
227 goal_id
= task
.goal_id
229 self
._set
_pref
_goal
_id
(goal_id
)
232 def add_with_name(self
, cls
, name
):
234 with Session(self
.engine
) as session
:
241 def add_vision(self
, name
):
242 return self
.add_with_name(Vision
, name
)
244 def add_goal(self
, name
):
245 goal_id
= self
.add_with_name(Goal
, name
)
246 self
._set
_pref
_goal
_id
(goal_id
)
249 def quick_add_task(self
, name
):
250 goal_id
= self
._get
_pref
_goal
_id
()
251 task_id
= self
.add_with_name(Task
, name
)
252 with Session(self
.engine
) as session
:
253 cmd
= select(Task
).where(Task
.id == task_id
)
254 new
= session
.exec(cmd
).first()
255 new
.goal_id
= goal_id
259 def remove_task(self
, task_id
):
261 logger
.debug(f
'Removing task with id {task_id}')
262 with Session(self
.engine
) as session
:
263 cmd
= select(Task
).where(Task
.id == int(task_id
))
264 to_remove
= session
.exec(cmd
).first()
266 return f
'Unable to find task with id {task_id}'
268 logger
.debug(f
'Removing task with name {to_remove.name}')
269 task_name
= to_remove
.name
270 session
.delete(to_remove
)
272 return f
'Removed task "{task_name}"'
274 def assign_vision(self
, goal_id
, vision_id
):
275 '''Goals may move between visions'''
276 self
._set
_pref
_goal
_id
(goal_id
)
277 with Session(self
.engine
) as session
:
278 cmd
= select(Goal
).where(Goal
.id == goal_id
)
279 goal
= session
.exec(cmd
).first()
280 goal
.vision_id
= vision_id
283 def visionless(self
):
284 '''Get goals with no vision'''
285 with Session(self
.engine
) as session
:
286 cmd
= select(Goal
).where(Goal
.vision_id
== None)
287 return session
.exec(cmd
).all()
290 def add_note(self
, note
, goal
):
291 '''Notes print with the goal.'''
294 # Someday: Notes on each