From: Christian Heller Date: Sun, 17 Mar 2024 05:04:29 +0000 (+0100) Subject: Add single-day view / retrieval. X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/%7B%7Bprefix%7D%7D/processes?a=commitdiff_plain;h=e51477ebea9fb701d88de85088b2fda1247c73a2;p=plomtask Add single-day view / retrieval. --- diff --git a/plomtask/days.py b/plomtask/days.py index 3abd769..f1920b4 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -47,6 +47,13 @@ class Day: 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.""" diff --git a/plomtask/http.py b/plomtask/http.py index 1af49d1..b6019ef 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -1,7 +1,7 @@ -"""plom's task manager""" +"""Web server stuff.""" from http.server import BaseHTTPRequestHandler from http.server import HTTPServer -from urllib.parse import urlparse +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 @@ -36,24 +36,29 @@ class TaskHandler(BaseHTTPRequestHandler): def do_GET(self): """Handle any GET request.""" try: + conn = DatabaseConnection(self.server.db) parsed_url = urlparse(self.path) site = path_split(parsed_url.path)[1] + params = parse_qs(parsed_url.query) if 'calendar' == site: - html = self.do_GET_calendar() + html = self.do_GET_calendar(conn) + elif 'day' == site: + date = params.get('date', ['2024-01-01'])[0] + html = self.do_GET_day(conn, date) else: raise HandledException('Test!') + conn.commit() + conn.close() self.send_html(html) except HandledException as error: self.send_msg(error) - def do_GET_calendar(self): - """Show sorted Days.""" - conn = DatabaseConnection(self.server.db) - Day('2024-01-03').save(conn) - Day('2024-01-01').save(conn) - Day('2024-01-02').save(conn) - days = Day.all(conn) - conn.commit() - conn.close() + def do_GET_calendar(self, conn: DatabaseConnection): + """Show Days.""" return self.server.jinja.get_template('calendar.html').render( - days=days) + days=Day.all(conn)) + + def do_GET_day(self, conn: DatabaseConnection, date: str): + """Show single Day.""" + day = Day.by_date(conn, date) + return self.server.jinja.get_template('day.html').render(day=day) diff --git a/templates/base.html b/templates/base.html index a3975e4..0068d11 100644 --- a/templates/base.html +++ b/templates/base.html @@ -2,7 +2,7 @@ -calendar | error +calendar
{% block content %} {% endblock %} diff --git a/templates/calendar.html b/templates/calendar.html index 0f18203..832e038 100644 --- a/templates/calendar.html +++ b/templates/calendar.html @@ -3,7 +3,7 @@ {% block content %} {% endblock %} diff --git a/templates/day.html b/templates/day.html new file mode 100644 index 0000000..51829b2 --- /dev/null +++ b/templates/day.html @@ -0,0 +1,6 @@ +{% extends 'base.html' %} + +{% block content %} +

{{day.date}} / {{day.weekday}}

+{% endblock %} + diff --git a/tests/test_days.py b/tests/test_days.py index 86be2ef..5e37138 100644 --- a/tests/test_days.py +++ b/tests/test_days.py @@ -36,6 +36,18 @@ class DayTests(TestCase): """Test Day.weekday.""" self.assertEqual(Day('2024-03-17').weekday, 'Sunday') + def test_Day_by_date(self): + """Test Day.by_date().""" + timestamp = datetime.now().timestamp() + db_file = DatabaseFile(f'test_db:{timestamp}') + db_file.remake() + conn = DatabaseConnection(db_file) + self.assertEqual(None, Day.by_date(conn, '2024-01-01')) + Day('2024-01-01').save(conn) + self.assertEqual(Day('2024-01-01'), Day.by_date(conn, '2024-01-01')) + conn.close() + remove_file(db_file.path) + def test_Day_all(self): """Test Day.all(), especially in regards to date range filtering.""" timestamp = datetime.now().timestamp()