X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/copy_structured?a=blobdiff_plain;f=plomtask%2Fdays.py;h=3abd76974107d379813a888a8f6f39b7c44afdc8;hb=a8a7b5d0a5007274539e528968c68fe9f25e422e;hp=4e0c4747809bc1e352e492cf619fafb41f3f0bbb;hpb=96b8f35df2ed6f7db0c7c8ffa8234e2925e8756c;p=plomtask diff --git a/plomtask/days.py b/plomtask/days.py index 4e0c474..3abd769 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -1,6 +1,8 @@ """Collecting Day and date-related items.""" from datetime import datetime +from sqlite3 import Row from plomtask.misc import HandledException +from plomtask.db import DatabaseConnection DATE_FORMAT = '%Y-%m-%d' @@ -23,6 +25,28 @@ class Day: if not self.datetime: raise HandledException(f'Given date of wrong format: {self.date}') + def save(self, db_conn: DatabaseConnection): + """Add (or re-write) self to database.""" + db_conn.exec('REPLACE INTO days VALUES (?)', (self.date,)) + + @classmethod + def from_table_row(cls, row: Row): + """Make new Day from database row.""" + return cls(row[0]) + + @classmethod + def all(cls, db_conn: DatabaseConnection, + date_range: tuple[str, str] = ('', '')): + """Return list of Days in database within date_range.""" + start_date = date_range[0] if date_range[0] else '2024-01-01' + end_date = date_range[1] if date_range[1] else '2030-12-31' + days = [] + sql = 'SELECT * FROM days WHERE date >= ? AND date <= ?' + for row in db_conn.exec(sql, (start_date, end_date)): + days += [cls.from_table_row(row)] + days.sort() + return days + @property def weekday(self): """Return what weekday matches self.date."""