return result
+def todays_date():
+ """Return current date in DATE_FORMAT."""
+ return str(datetime.now())[:10]
+
+
class Day:
"""Individual days defined by their dates."""
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)):
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
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!')
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
[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()."""