]>
us-delaporte.pagekite.me Git - matrix-chore-bot.git/blob - matrix_chore_bot/db.py
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 CHORE_DB
= os
.environ
.get('CHORE_DB')
13 DATE_FORMAT
= '%Y-%m-%d'
16 return dt
.now().strftime(DATE_FORMAT
)
19 logger
= logging
.getLogger(__name__
)
20 logging
.basicConfig(filename
='chore.log', encoding
='utf-8', level
=logging
.DEBUG
)
22 class Chore(SQLModel
, table
=True):
23 id: int |
None = Field(default
=None, primary_key
=True)
24 name
: str = Field(default
=None, unique
=True)
27 return f
'{self.name} ({self.id})'
30 class Record(SQLModel
, table
=True):
31 id: int |
None = Field(default
=None, primary_key
=True)
32 chore_id
: int |
None = Field(default
=None, foreign_key
="chore.id")
33 complete_date
: dt
= Field(default_factory
=lambda: dt
.now())
36 def get_chore(session
, chore_id
):
37 cmd
= select(Chore
).where(Chore
.id == chore_id
)
38 return session
.exec(cmd
).first()
41 def __init__(self
, db_file
= CHORE_DB
):
43 self
.engine
= self
.get_engine()
46 return create_engine(f
"sqlite:///{self.file}")
49 SQLModel
.metadata
.create_all(self
.engine
)
53 def check_chore(session
, chore_id
) -> str:
54 chore
= get_chore(session
, chore_id
)
56 return f
'Chore not found: {chore_id}'
60 WHERE chore_id = :chore_id
61 AND complete_date >= :today
62 ORDER BY complete_date DESC
66 params
['chore_id'] = chore_id
67 params
['today'] = today()
68 results
= session
.execute(sql
, params
).first()
72 return f
'- [x] {chore}'
74 return f
'- [ ] {chore}'
77 def check(self
, chore_id
):
78 with Session(self
.engine
) as session
:
79 return Data
.check_chore(session
, chore_id
)
83 with Session(self
.engine
) as session
:
85 chores
= session
.exec(cmd
).all()
87 retuls
.append('None Found')
90 results
.append(Data
.check_chore(session
, chore
.id))
95 with Session(self
.engine
) as session
:
96 new_chore
= Chore(name
=name
)
97 session
.add(new_chore
)
99 return f
'Created chore {name}.'
101 return f
'Unable to create {name}. May be duplicate.'
105 def done(self
, chore_id
):
106 with Session(self
.engine
) as session
:
107 chore
= get_chore(session
, chore_id
)
109 return f
'Chore not found: {chore_id}'
112 session
.add(Record(chore_id
=chore
.id))
114 return f
'Marked chore as done: {chore.name}.'
115 except Exception as ex
:
116 return f
'Unable to mark chore as done: {ex}'
119 def undo(self
, chore_id
):
120 with Session(self
.engine
) as session
:
121 cmd
= select(Chore
).where(Chore
.id == chore_id
)
122 chore
= session
.exec(cmd
).first()
125 return f
'Not found: {chore_id}'
128 cmd
= select(Record
).where(Record
.chore_id
== chore_id
).where(Record
.complete_date
>= today())
129 results
= session
.exec(cmd
).all()
130 for result
in results
:
131 session
.delete(result
)
133 return f
'Marked chore as *not* done: {chore.name}.'
134 except Exception as ex
:
135 return f
'Unable to mark chore as done: {ex}'
137 def remove(self
, chore_id
):
139 with Session(self
.engine
) as session
:
140 chore
= get_chore(session
, chore_id
)
142 return f
'Chore not found: {chore_id}'
146 WHERE chore_id = :chore_id
149 params
['chore_id'] = chore_id
150 results
= session
.execute(sql
, params
)
151 logger
.debug(results
)
156 results
= session
.execute(sql
, params
)
157 logger
.debug(results
)
159 return f
'Removed chore {chore_id}'
160 except Exception as ex
:
161 return f
'Unable to remove chore: {ex}'