home · contact · privacy
Add single-day view / retrieval.
[plomtask] / plomtask / days.py
index 17bb46329760beb078a6497d68bd7b0a39a05e3e..f1920b4d1343bbb486cd537b2c77102e22756a37 100644 (file)
@@ -35,13 +35,25 @@ class Day:
         return cls(row[0])
 
     @classmethod
-    def all(cls, db_conn: DatabaseConnection):
-        """Return list of all Days in database."""
+    def all(cls, db_conn: DatabaseConnection,
+            date_range: tuple[str, str] = ('', '')):
+        """Return list of Days in database within date_range."""
+        start_date = date_range[0] if date_range[0] else '2024-01-01'
+        end_date = date_range[1] if date_range[1] else '2030-12-31'
         days = []
-        for row in db_conn.exec('SELECT * FROM days'):
+        sql = 'SELECT * FROM days WHERE date >= ? AND date <= ?'
+        for row in db_conn.exec(sql, (start_date, end_date)):
             days += [cls.from_table_row(row)]
+        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."""