X-Git-Url: https://plomlompom.com/repos//%22https:/validator.w3.org/check?a=blobdiff_plain;f=task.py;h=4774c8c51597c934b73be4462b2a1d2688ab0b03;hb=bf0868a41a86ea5952d5fc1a8fca6621e89ad42e;hp=2a6c699f99cb956192a33727a69cb7f808cd8ff2;hpb=802fad1325647a1e842dcfbdcaff3a391ac3b04c;p=plomtask diff --git a/task.py b/task.py index 2a6c699..4774c8c 100755 --- a/task.py +++ b/task.py @@ -1,10 +1,11 @@ #!/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 HTTP_PORT = 8082 TEMPLATES_DIR = 'templates' @@ -17,13 +18,37 @@ class HandledException(Exception): class TaskHandler(BaseHTTPRequestHandler): """Handles single HTTP request.""" - def do_GET(self): - """Handle any GET request.""" - self.send_response(200) + def send_html(self, html:str, code:int=200): + """Send HTML as proper HTTP response.""" + self.send_response(code) self.end_headers() - html = self.server.jinja.get_template('index.html').render() 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: + parsed_url = urlparse(self.path) + site = path_split(parsed_url.path)[1] + if 'calendar' == site: + html = self.do_GET_calendar() + else: + raise HandledException('Test!') + self.send_html(html) + except HandledException as error: + self.send_msg(error) + + def do_GET_calendar(self): + """Show sorted Days.""" + days = [Day('2024-01-03'), Day('2024-01-01'), Day('2024-01-02')] + days.sort() + return self.server.jinja.get_template('calendar.html').render( + days=days) + def main(): """Main loop."""