]>
apps.delaporte.us Git - matrix-chore-bot.git/blob - matrix_chore_bot/db.py
315d2793682a5d8210d944c8f35bbe8bac0249ca
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
)
17 logger
= logging
.getLogger(__name__
)
18 logging
.basicConfig(filename
='chore.log', encoding
='utf-8', level
=logging
.DEBUG
)
20 class Chore(SQLModel
, table
=True):
21 id: int |
None = Field(default
=None, primary_key
=True)
22 name
: str = Field(default
=None, unique
=True)
25 return f
'{self.name} ({self.id})'
28 class Record(SQLModel
, table
=True):
29 id: int |
None = Field(default
=None, primary_key
=True)
30 chore_id
: int |
None = Field(default
=None, foreign_key
="chore.id")
31 complete_date
: dt
= Field(default_factory
=lambda: dt
.now())
34 def get_chore(session
, chore_id
):
35 cmd
= select(Chore
).where(Chore
.id == chore_id
)
36 return session
.exec(cmd
).first()
39 def __init__(self
, db_file
=None):
42 db_file
= os
.path
.join(os
.environ
.get('MATRIX_DATA_DIR'), 'chores.db')
45 self
.engine
= self
.get_engine()
48 return create_engine(f
"sqlite:///{self.file}")
51 SQLModel
.metadata
.create_all(self
.engine
)
55 def check_chore(session
, chore_id
) -> str:
56 chore
= get_chore(session
, chore_id
)
58 return f
'Chore not found: {chore_id}'
62 WHERE chore_id = :chore_id
63 AND complete_date >= :today
64 ORDER BY complete_date DESC
68 params
['chore_id'] = chore_id
69 params
['today'] = today()
70 results
= session
.execute(sql
, params
).first()
74 return f
'- [x] {chore}'
76 return f
'- [ ] {chore}'
79 def check(self
, chore_id
):
80 with Session(self
.engine
) as session
:
81 return Data
.check_chore(session
, chore_id
)
85 with Session(self
.engine
) as session
:
87 chores
= session
.exec(cmd
).all()
89 retuls
.append('None Found')
92 results
.append(Data
.check_chore(session
, chore
.id))
97 with Session(self
.engine
) as session
:
98 new_chore
= Chore(name
=name
)
99 session
.add(new_chore
)
101 return f
'Created chore {name}.'
103 return f
'Unable to create {name}. May be duplicate.'
107 def done(self
, chore_id
):
108 with Session(self
.engine
) as session
:
109 chore
= get_chore(session
, chore_id
)
111 return f
'Chore not found: {chore_id}'
114 session
.add(Record(chore_id
=chore
.id))
116 return f
'Marked chore as done: {chore.name}.'
117 except Exception as ex
:
118 return f
'Unable to mark chore as done: {ex}'
121 def undo(self
, chore_id
):
122 with Session(self
.engine
) as session
:
123 cmd
= select(Chore
).where(Chore
.id == chore_id
)
124 chore
= session
.exec(cmd
).first()
127 return f
'Not found: {chore_id}'
130 cmd
= select(Record
).where(Record
.chore_id
== chore_id
).where(Record
.complete_date
>= today())
131 results
= session
.exec(cmd
).all()
132 for result
in results
:
133 session
.delete(result
)
135 return f
'Marked chore as *not* done: {chore.name}.'
136 except Exception as ex
:
137 return f
'Unable to mark chore as done: {ex}'
139 def remove(self
, chore_id
):
141 with Session(self
.engine
) as session
:
142 chore
= get_chore(session
, chore_id
)
144 return f
'Chore not found: {chore_id}'
148 WHERE chore_id = :chore_id
151 params
['chore_id'] = chore_id
152 results
= session
.execute(sql
, params
)
153 logger
.debug(results
)
158 results
= session
.execute(sql
, params
)
159 logger
.debug(results
)
161 return f
'Removed chore {chore_id}'
162 except Exception as ex
:
163 return f
'Unable to remove chore: {ex}'