X-Git-Url: https://plomlompom.com/repos/feed.xml?a=blobdiff_plain;f=plomtask%2Fdays.py;h=89c957eef7ee49746e079d91ac3db3fa8d178760;hb=b615b5b98b7363a0987ec35e669187e4de1a8c12;hp=f1920b4d1343bbb486cd537b2c77102e22756a37;hpb=e51477ebea9fb701d88de85088b2fda1247c73a2;p=plomtask diff --git a/plomtask/days.py b/plomtask/days.py index f1920b4..89c957e 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -19,20 +19,23 @@ 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 save(self, db_conn: DatabaseConnection): - """Add (or re-write) self to database.""" - db_conn.exec('REPLACE INTO days VALUES (?)', (self.date,)) + def __eq__(self, other: object): + return isinstance(other, self.__class__) and self.date == other.date + + def __lt__(self, other): + return self.date < other.date @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, @@ -48,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 @@ -59,8 +65,7 @@ class Day: """Return what weekday matches self.date.""" return self.datetime.strftime('%A') - def __eq__(self, other: object): - return isinstance(other, self.__class__) and self.date == other.date - - def __lt__(self, other): - return self.date < other.date + def save(self, db_conn: DatabaseConnection): + """Add (or re-write) self to database.""" + db_conn.exec('REPLACE INTO days VALUES (?, ?)', + (self.date, self.comment))