X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=plomtask%2Fdays.py;h=ba466b52c792f81029a94f4bc9fcc21718dd500b;hb=abef15c28141490af71a1b8a11882aaabc0d3775;hp=3abd76974107d379813a888a8f6f39b7c44afdc8;hpb=a8a7b5d0a5007274539e528968c68fe9f25e422e;p=plomtask
diff --git a/plomtask/days.py b/plomtask/days.py
index 3abd769..ba466b5 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,
@@ -47,13 +50,19 @@ class Day:
days.sort()
return days
+ @classmethod
+ def by_date(cls, db_conn: DatabaseConnection, date: str):
+ """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)
+ return None
+
@property
def weekday(self):
"""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))