X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=plomtask%2Fdays.py;h=02ad1a8f5223e1dbaee1ef445fd4e9b211ad4842;hb=9c3ba57169fdc71d3ffefd4c7c20cae4e89f9c9c;hp=8db9f15b7362ede61c70265e4d5eb8a4c2c0c626;hpb=4546631ed7cc59f3e66a1902b28930f955b2b03f;p=plomtask
diff --git a/plomtask/days.py b/plomtask/days.py
index 8db9f15..02ad1a8 100644
--- a/plomtask/days.py
+++ b/plomtask/days.py
@@ -1,16 +1,47 @@
-#!/usr/bin/env python3
"""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'
+def date_valid(date: str):
+ """Validate date against DATE_FORMAT, return Datetime or None."""
+ try:
+ result = datetime.strptime(date, DATE_FORMAT)
+ except (ValueError, TypeError):
+ return None
+ return result
+
+
class Day:
"""Individual days defined by their dates."""
def __init__(self, date: str):
self.date = date
- self.datetime = datetime.strptime(date, DATE_FORMAT)
+ self.datetime = date_valid(self.date)
+ if not self.datetime:
+ raise HandledException(f'Given date of wrong format: {self.date}')
+
+ @classmethod
+ def add(cls, db_conn: DatabaseConnection, date: str):
+ """Add (or re-write) new Day(date) to database."""
+ db_conn.exec('REPLACE INTO days VALUES (?)', (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):
+ """Return list of all Days in database."""
+ days = []
+ for row in db_conn.exec('SELECT * FROM days'):
+ days += [cls.from_table_row(row)]
+ return days
@property
def weekday(self):