home · contact · privacy
Add database connection, read and write Days through them.
[plomtask] / plomtask / days.py
index 4e0c4747809bc1e352e492cf619fafb41f3f0bbb..02ad1a8f5223e1dbaee1ef445fd4e9b211ad4842 100644 (file)
@@ -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,24 @@ class Day:
         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):
         """Return what weekday matches self.date."""