home · contact · privacy
Add single-day view / retrieval.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 17 Mar 2024 05:04:29 +0000 (06:04 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 17 Mar 2024 05:04:29 +0000 (06:04 +0100)
plomtask/days.py
plomtask/http.py
templates/base.html
templates/calendar.html
templates/day.html [new file with mode: 0644]
tests/test_days.py

index 3abd76974107d379813a888a8f6f39b7c44afdc8..f1920b4d1343bbb486cd537b2c77102e22756a37 100644 (file)
@@ -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."""
index 1af49d16b197bc607310f0f7c710b407b93e905b..b6019ef0761b1dc3a86d3a12f395cd5b96f40c5c 100644 (file)
@@ -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)
index a3975e46833632b52e3f2d1ac21889b240e88b51..0068d1160a1c57b36a788763444e08a597b560e3 100644 (file)
@@ -2,7 +2,7 @@
 <html>
 <meta charset="UTF-8">
 <body>
-<a href="calendar">calendar</a> | <a href="error">error</a>
+<a href="calendar">calendar</a>
 <hr>
 {% block content %}
 {% endblock %}
index 0f18203b639eab7ada07d69a98d71d56330d489a..832e03867614ef0aa08d7694dbfd4b325b8ac298 100644 (file)
@@ -3,7 +3,7 @@
 {% block content %}
 <ul>
 {% for day in days %}
-<li>{{day.date}} ({{day.weekday}})
+<li><a href="day?date={{day.date}}">{{day.date}}</a> ({{day.weekday}})
 {% endfor %}
 </ul>
 {% endblock %}
diff --git a/templates/day.html b/templates/day.html
new file mode 100644 (file)
index 0000000..51829b2
--- /dev/null
@@ -0,0 +1,6 @@
+{% extends 'base.html' %}
+
+{% block content %}
+<h3>{{day.date}} / {{day.weekday}}</h3>
+{% endblock %}
+
index 86be2efc37a77760494017fbae1f6965ac3eb964..5e3713882ca622d207026792f2f47df23d807283 100644 (file)
@@ -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()