From: Christian Heller Date: Wed, 20 Mar 2024 00:20:25 +0000 (+0100) Subject: Add Day POSTing. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/static/ledger?a=commitdiff_plain;h=abef15c28141490af71a1b8a11882aaabc0d3775;p=plomtask Add Day POSTing. --- diff --git a/plomtask/http.py b/plomtask/http.py index afcaa11..d9fa6a0 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -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) diff --git a/templates/day.html b/templates/day.html index 5547f45..0fff9e0 100644 --- a/templates/day.html +++ b/templates/day.html @@ -2,6 +2,9 @@ {% block content %}

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

-comment: {{day.comment|e}} +
+comment: + +
{% endblock %}