]>
apps.delaporte.us Git - matrix-chore-bot.git/blob - matrix_chore_bot/db.py
536c9c08fb7640e056271763ab69d6bfb35acdfb
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 DEFAULT_DB_FILE
= os
.path
.join(os
.environ
.get('MATRIX_DATA_DIR', ''), 'chores.db')
17 DB_FILE
= os
.environ
.get('MATRIX_CHORE_DB_FILE', DEFAULT_DB_FILE
)
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
=None):
47 self
.engine
= self
.get_engine()
50 return create_engine(f
"sqlite:///{self.file}")
53 SQLModel
.metadata
.create_all(self
.engine
)
57 def check_chore(session
, chore_id
) -> str:
58 chore
= get_chore(session
, chore_id
)
60 return f
'Chore not found: {chore_id}'
64 WHERE chore_id = :chore_id
65 AND complete_date >= :today
66 ORDER BY complete_date DESC
70 params
['chore_id'] = chore_id
71 params
['today'] = today()
72 results
= session
.execute(sql
, params
).first()
76 return f
'- [x] {chore}'
78 return f
'- [ ] {chore}'
81 def check(self
, chore_id
):
82 with Session(self
.engine
) as session
:
83 return Data
.check_chore(session
, chore_id
)
87 with Session(self
.engine
) as session
:
89 chores
= session
.exec(cmd
).all()
91 results
.append('None Found')
94 results
.append(Data
.check_chore(session
, chore
.id))
99 with Session(self
.engine
) as session
:
100 new_chore
= Chore(name
=name
)
101 session
.add(new_chore
)
103 return f
'Created chore {name}.'
105 return f
'Unable to create {name}. May be duplicate.'
109 def done(self
, chore_id
):
110 with Session(self
.engine
) as session
:
111 chore
= get_chore(session
, chore_id
)
113 return f
'Chore not found: {chore_id}'
116 session
.add(Record(chore_id
=chore
.id))
118 return f
'Marked chore as done: {chore.name}.'
119 except Exception as ex
:
120 return f
'Unable to mark chore as done: {ex}'
123 def undo(self
, chore_id
):
124 with Session(self
.engine
) as session
:
125 cmd
= select(Chore
).where(Chore
.id == chore_id
)
126 chore
= session
.exec(cmd
).first()
129 return f
'Not found: {chore_id}'
132 cmd
= select(Record
).where(Record
.chore_id
== chore_id
).where(Record
.complete_date
>= today())
133 results
= session
.exec(cmd
).all()
134 for result
in results
:
135 session
.delete(result
)
137 return f
'Marked chore as *not* done: {chore.name}.'
138 except Exception as ex
:
139 return f
'Unable to mark chore as done: {ex}'
141 def remove(self
, chore_id
):
143 with Session(self
.engine
) as session
:
144 chore
= get_chore(session
, chore_id
)
146 return f
'Chore not found: {chore_id}'
150 WHERE chore_id = :chore_id
153 params
['chore_id'] = chore_id
154 results
= session
.execute(sql
, params
)
155 logger
.debug(results
)
160 results
= session
.execute(sql
, params
)
161 logger
.debug(results
)
163 return f
'Removed chore {chore_id}'
164 except Exception as ex
:
165 return f
'Unable to remove chore: {ex}'