home · contact · privacy
Allow 'today' as calendar range limit, and default to today in GET /day of empty...
authorChristian Heller <c.heller@plomlompom.de>
Wed, 20 Mar 2024 02:25:08 +0000 (03:25 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 20 Mar 2024 02:25:08 +0000 (03:25 +0100)
plomtask/days.py
plomtask/http.py
tests/test_days.py

index 89c957eef7ee49746e079d91ac3db3fa8d178760..776658e14aa6f7f488eeda7546eb8612c2f50ee0 100644 (file)
@@ -16,6 +16,11 @@ def date_valid(date: str):
     return result
 
 
+def todays_date():
+    """Return current date in DATE_FORMAT."""
+    return str(datetime.now())[:10]
+
+
 class Day:
     """Individual days defined by their dates."""
 
@@ -41,8 +46,16 @@ class Day:
     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'
+        def date_from_range_element(date_str: str, default: str):
+            if date_str == '':
+                date_str = default
+            elif date_str == 'today':
+                date_str = todays_date()
+            elif not date_valid(date_str):
+                raise HandledException(f'Bad date: {date_str}')
+            return date_str
+        start_date = date_from_range_element(date_range[0], '2024-01-01')
+        end_date = date_from_range_element(date_range[1], '2030-01-01')
         days = []
         sql = 'SELECT * FROM days WHERE date >= ? AND date <= ?'
         for row in db_conn.exec(sql, (start_date, end_date)):
index 794ce1f7139c85512b37238defba243d18f42c35..0af96e7538c2cefd9febd1ac55dedc6fd95f4f90 100644 (file)
@@ -4,7 +4,7 @@ from http.server import HTTPServer
 from urllib.parse import urlparse, parse_qs
 from os.path import split as path_split
 from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
-from plomtask.days import Day
+from plomtask.days import Day, todays_date
 from plomtask.misc import HandledException
 from plomtask.db import DatabaseConnection
 
@@ -31,7 +31,7 @@ class TaskHandler(BaseHTTPRequestHandler):
             if 'calendar' == site:
                 html = self.do_GET_calendar(conn)
             elif 'day' == site:
-                date = params.get('date', ['2024-01-01'])[0]
+                date = params.get('date', [todays_date()])[0]
                 html = self.do_GET_day(conn, date)
             else:
                 raise HandledException('Test!')
index 08ce637501887b86bb626a465a19a1e1cae4ece5..e25eafdbf00856a85738d00a49907ebac2a7c642 100644 (file)
@@ -6,7 +6,7 @@ from urllib.parse import urlencode
 from datetime import datetime
 from os import remove as remove_file
 from plomtask.http import TaskHandler, TaskServer
-from plomtask.days import Day
+from plomtask.days import Day, todays_date
 from plomtask.misc import HandledException
 from plomtask.db import DatabaseFile, DatabaseConnection
 
@@ -89,6 +89,9 @@ class TestsWithDB(TestCase):
                          [day1, day2])
         self.assertEqual(Day.all(self.db_conn, ('2024-01-03', '2024-01-01')),
                          [])
+        today = Day(todays_date())
+        today.save(self.db_conn)
+        self.assertEqual(Day.all(self.db_conn, ('today', 'today')), [today])
 
     def test_Day_comment(self):
         """Test Day.by_date()."""