From 0b9590b2c15c246782d9a81e1df24baa73a09765 Mon Sep 17 00:00:00 2001 From: Christian Heller <c.heller@plomlompom.de> Date: Sun, 17 Mar 2024 00:24:01 +0100 Subject: [PATCH] Use different do_GET handlers for different paths. --- task.py | 24 +++++++++++++++++++----- templates/base.html | 2 ++ templates/msg.html | 6 ++++++ 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 templates/msg.html diff --git a/task.py b/task.py index d6128ee..0b88254 100755 --- a/task.py +++ b/task.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 -""" -plom's task manager -""" +"""plom's task manager""" from http.server import BaseHTTPRequestHandler from http.server import HTTPServer +from urllib.parse import urlparse +from os.path import split as path_split from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader from days import Day @@ -20,13 +20,27 @@ class TaskHandler(BaseHTTPRequestHandler): def do_GET(self): """Handle any GET request.""" + parsed_url = urlparse(self.path) + site = path_split(parsed_url.path)[1] + if 'calendar' == site: + html = self.do_GET_calendar() + else: + html = self.do_GET_msg('I have nothing to say at this point.') self.send_response(200) self.end_headers() + self.wfile.write(bytes(html, 'utf-8')) + + def do_GET_msg(self, msg): + """Show message.""" + return self.server.jinja.get_template('msg.html').render( + msg=msg) + + def do_GET_calendar(self): + """Show sorted Days.""" days = [Day('2024-01-03'), Day('2024-01-01'), Day('2024-01-02')] days.sort() - html = self.server.jinja.get_template('calendar.html').render( + return self.server.jinja.get_template('calendar.html').render( days=days) - self.wfile.write(bytes(html, 'utf-8')) def main(): diff --git a/templates/base.html b/templates/base.html index 508281e..427740b 100644 --- a/templates/base.html +++ b/templates/base.html @@ -2,6 +2,8 @@ <html> <meta charset="UTF-8"> <body> +<a href="calendar">calendar</a> | <a href="test">test</a> +<hr> {% block content %} {% endblock %} </body> diff --git a/templates/msg.html b/templates/msg.html new file mode 100644 index 0000000..90cd61c --- /dev/null +++ b/templates/msg.html @@ -0,0 +1,6 @@ +{% extends 'base.html' %} + +{% block content %} +<p>{{msg}}</p> +{% endblock %} + -- 2.30.2