home · contact · privacy
Add Day POSTing.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 20 Mar 2024 00:20:25 +0000 (01:20 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 20 Mar 2024 00:20:25 +0000 (01:20 +0100)
plomtask/http.py
templates/day.html

index afcaa116173145bb2ab5b2547937d5458d3e2666..d9fa6a03194d1838fcf36f8cc860a4eab0baf6ac 100644 (file)
@@ -52,6 +52,40 @@ class TaskHandler(BaseHTTPRequestHandler):
         day = Day.by_date(conn, date)
         return self.server.jinja.get_template('day.html').render(day=day)
 
+    def do_POST(self):
+        """Handle any POST 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)
+            length = int(self.headers['content-length'])
+            postvars = parse_qs(self.rfile.read(length).decode(),
+                                keep_blank_values=1)
+            if 'day' == site:
+                date = params.get('date', ['2024-01-01'])[0]
+                self.do_POST_day(conn, date, postvars)
+            conn.commit()
+            conn.close()
+            self._redirect('/')
+        except HandledException as error:
+            self._send_msg(error)
+
+    def do_POST_day(self, conn: DatabaseConnection, date: str, postvars: dict):
+        """Update or insert Day of date and fields defined in postvars."""
+        comment = postvars['comment'][0]
+        day = Day.by_date(conn, date)
+        if day:
+            day.comment = comment
+        else:
+            day = Day(date, comment)
+        day.save(conn)
+
+    def _redirect(self, target: str):
+        self.send_response(302)
+        self.send_header('Location', target)
+        self.end_headers()
+
     def _send_html(self, html: str, code: int = 200):
         """Send HTML as proper HTTP response."""
         self.send_response(code)
index 5547f4534e94742600f0664aaece2b7cdc33afc3..0fff9e049792f695230947e56afc5be3b4d40060 100644 (file)
@@ -2,6 +2,9 @@
 
 {% block content %}
 <h3>{{day.date}} / {{day.weekday}}</h3>
-comment: {{day.comment|e}}
+<form action="day?date={{day.date}}" method="POST">
+comment: <input name="comment" value="{{day.comment|e}}" />
+<input type="submit" value="OK" />
+</form>
 {% endblock %}