]>
apps.delaporte.us Git - matrix-fridge-bot.git/blob - matrix_chore_bot/db.py
a0eae22f210f4374073a8ae41fa8c43a3053b57f
3 from datetime
import datetime
as dt
5 from sqlmodel
import Field
, Session
, SQLModel
, create_engine
, select
, text
9 # - [ ] add notes to each chore
11 DATE_FORMAT
= '%Y-%m-%d'
14 return dt
.now().strftime(DATE_FORMAT
)
16 logger
= logging
.getLogger(__name__
)
17 logging
.basicConfig(filename
='chore.log', encoding
='utf-8', level
=logging
.DEBUG
)
19 class Chore(SQLModel
, table
=True):
20 id: int |
None = Field(default
=None, primary_key
=True)
21 name
: str = Field(default
=None, unique
=True)
24 return f
'{self.name} ({self.id})'
27 class Record(SQLModel
, table
=True):
28 id: int |
None = Field(default
=None, primary_key
=True)
29 chore_id
: int |
None = Field(default
=None, foreign_key
="chore.id")
30 complete_date
: dt
= Field(default_factory
=lambda: dt
.now())
33 def get_chore(session
, chore_id
):
34 cmd
= select(Chore
).where(Chore
.id == chore_id
)
35 return session
.exec(cmd
).first()
38 def __init__(self
, db_file
):
41 self
.engine
= self
.get_engine()
43 def get_db_file(self
):
47 return create_engine(f
"sqlite:///{self.file}")
50 SQLModel
.metadata
.create_all(self
.engine
)
54 def check_chore(session
, chore_id
) -> str:
55 chore
= get_chore(session
, chore_id
)
57 return f
'Chore not found: {chore_id}'
61 WHERE chore_id = :chore_id
62 AND complete_date >= :today
63 ORDER BY complete_date DESC
67 params
['chore_id'] = chore_id
68 params
['today'] = today()
69 results
= session
.execute(sql
, params
).first()
73 return f
'- [x] {chore}'
75 return f
'- [ ] {chore}'
78 def check(self
, chore_id
):
79 with Session(self
.engine
) as session
:
80 return Data
.check_chore(session
, chore_id
)
84 with Session(self
.engine
) as session
:
86 chores
= session
.exec(cmd
).all()
88 results
.append('None Found')
91 results
.append(Data
.check_chore(session
, chore
.id))
94 def filter(self
, term
):
96 for item
in self
.list():
107 with Session(self
.engine
) as session
:
108 new_chore
= Chore(name
=name
)
109 session
.add(new_chore
)
111 return f
'Created chore {name}.'
113 return f
'Unable to create {name}. May be duplicate.'
117 def done(self
, chore_id
):
118 with Session(self
.engine
) as session
:
119 chore
= get_chore(session
, chore_id
)
121 return f
'Chore not found: {chore_id}'
124 session
.add(Record(chore_id
=chore
.id))
126 return f
'Marked chore as done: {chore.name}.'
127 except Exception as ex
:
128 return f
'Unable to mark chore as done: {ex}'
131 def undo(self
, chore_id
):
132 with Session(self
.engine
) as session
:
133 cmd
= select(Chore
).where(Chore
.id == chore_id
)
134 chore
= session
.exec(cmd
).first()
137 return f
'Not found: {chore_id}'
140 cmd
= select(Record
).where(Record
.chore_id
== chore_id
).where(Record
.complete_date
>= today())
141 results
= session
.exec(cmd
).all()
142 for result
in results
:
143 session
.delete(result
)
145 return f
'Marked chore as *not* done: {chore.name}.'
146 except Exception as ex
:
147 return f
'Unable to mark chore as done: {ex}'
149 def remove(self
, chore_id
):
151 with Session(self
.engine
) as session
:
152 chore
= get_chore(session
, chore_id
)
154 return f
'Chore not found: {chore_id}'
158 WHERE chore_id = :chore_id
161 params
['chore_id'] = chore_id
162 results
= session
.execute(sql
, params
)
163 logger
.debug(results
)
168 results
= session
.execute(sql
, params
)
169 logger
.debug(results
)
171 return f
'Removed chore {chore_id}'
172 except Exception as ex
:
173 return f
'Unable to remove chore: {ex}'