X-Git-Url: https://plomlompom.com/repos/bar%20baz.html?a=blobdiff_plain;f=plomtask%2Fdays.py;h=89c957eef7ee49746e079d91ac3db3fa8d178760;hb=df5c37b734bedcb5d026558c32b40977bd028a5a;hp=d64b34b6bc69c01fd6d6a17437bbc90f357d301a;hpb=9e32ae00d4435932d55695be4e757ff109c76f26;p=plomtask diff --git a/plomtask/days.py b/plomtask/days.py index d64b34b..89c957e 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -19,11 +19,12 @@ def date_valid(date: str): class Day: """Individual days defined by their dates.""" - def __init__(self, date: str): + def __init__(self, date: str, comment: str = ''): self.date = date self.datetime = date_valid(self.date) if not self.datetime: raise HandledException(f'Given date of wrong format: {self.date}') + self.comment = comment def __eq__(self, other: object): return isinstance(other, self.__class__) and self.date == other.date @@ -34,7 +35,7 @@ class Day: @classmethod def from_table_row(cls, row: Row): """Make new Day from database row.""" - return cls(row[0]) + return cls(row[0], row[1]) @classmethod def all(cls, db_conn: DatabaseConnection, @@ -50,10 +51,13 @@ class Day: return days @classmethod - def by_date(cls, db_conn: DatabaseConnection, date: str): + def by_date(cls, db_conn: DatabaseConnection, + date: str, create: bool = False): """Retrieve Day by date if in DB, else return None.""" for row in db_conn.exec('SELECT * FROM days WHERE date = ?', (date,)): return cls.from_table_row(row) + if create: + return cls(date) return None @property @@ -63,4 +67,5 @@ class Day: def save(self, db_conn: DatabaseConnection): """Add (or re-write) self to database.""" - db_conn.exec('REPLACE INTO days VALUES (?)', (self.date,)) + db_conn.exec('REPLACE INTO days VALUES (?, ?)', + (self.date, self.comment))