home · contact · privacy
Minor class method reorganizations.
[plomtask] / plomtask / http.py
index 2039cf91351db9c866fa75c3ec4c974d388490ce..afcaa116173145bb2ab5b2547937d5458d3e2666 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
@@ -22,39 +22,43 @@ class TaskHandler(BaseHTTPRequestHandler):
     """Handles single HTTP request."""
     server: TaskServer
 
-    def send_html(self, html: str, code: int = 200):
-        """Send HTML as proper HTTP response."""
-        self.send_response(code)
-        self.end_headers()
-        self.wfile.write(bytes(html, 'utf-8'))
-
-    def send_msg(self, msg: str, code: int = 400):
-        """Send message in HTML formatting as HTTP response."""
-        html = self.server.jinja.get_template('msg.html').render(msg=msg)
-        self.send_html(html, code)
-
     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!')
-            self.send_html(html)
+            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.add(conn, '2024-01-03')
-        Day.add(conn, '2024-01-01')
-        Day.add(conn, '2024-01-02')
-        days = Day.all(conn)
-        conn.commit()
-        conn.close()
-        days.sort()
+            self._send_msg(error)
+
+    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)
+
+    def _send_html(self, html: str, code: int = 200):
+        """Send HTML as proper HTTP response."""
+        self.send_response(code)
+        self.end_headers()
+        self.wfile.write(bytes(html, 'utf-8'))
+
+    def _send_msg(self, msg: str, code: int = 400):
+        """Send message in HTML formatting as HTTP response."""
+        html = self.server.jinja.get_template('msg.html').render(msg=msg)
+        self._send_html(html, code)