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)
{% 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 %}