From: Christian Heller Date: Wed, 20 Mar 2024 02:25:08 +0000 (+0100) Subject: Allow 'today' as calendar range limit, and default to today in GET /day of empty... X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/static/%7B%7B%20web_path%20%7D%7D/do_day?a=commitdiff_plain;h=736d41c2813776569a37805e4637a380290a1656;p=plomtask Allow 'today' as calendar range limit, and default to today in GET /day of empty "?date=". --- diff --git a/plomtask/days.py b/plomtask/days.py index 89c957e..776658e 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -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)): diff --git a/plomtask/http.py b/plomtask/http.py index 794ce1f..0af96e7 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -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!') diff --git a/tests/test_days.py b/tests/test_days.py index 08ce637..e25eafd 100644 --- a/tests/test_days.py +++ b/tests/test_days.py @@ -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()."""