1 """Web server stuff."""
2 from http.server import BaseHTTPRequestHandler
3 from http.server import HTTPServer
4 from urllib.parse import urlparse, parse_qs
5 from os.path import split as path_split
6 from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
7 from plomtask.days import Day
8 from plomtask.misc import HandledException
9 from plomtask.db import DatabaseConnection
11 TEMPLATES_DIR = 'templates'
14 class TaskServer(HTTPServer):
15 """Variant of HTTPServer that knows .jinja as Jinja Environment."""
17 def __init__(self, db_file, *args, **kwargs):
18 super().__init__(*args, **kwargs)
20 self.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
23 class TaskHandler(BaseHTTPRequestHandler):
24 """Handles single HTTP request."""
28 """Handle any GET request."""
30 conn, site, params = self._init_handling()
31 if 'calendar' == site:
32 html = self.do_GET_calendar(conn)
34 date = params.get('date', ['2024-01-01'])[0]
35 html = self.do_GET_day(conn, date)
37 raise HandledException('Test!')
41 except HandledException as error:
44 def do_GET_calendar(self, conn: DatabaseConnection):
46 return self.server.jinja.get_template('calendar.html').render(
49 def do_GET_day(self, conn: DatabaseConnection, date: str):
50 """Show single Day."""
51 day = Day.by_date(conn, date, create=True)
52 return self.server.jinja.get_template('day.html').render(day=day)
55 """Handle any POST request."""
57 conn, site, params = self._init_handling()
58 length = int(self.headers['content-length'])
59 postvars = parse_qs(self.rfile.read(length).decode(),
62 date = params.get('date', ['2024-01-01'])[0]
63 self.do_POST_day(conn, date, postvars)
67 except HandledException as error:
70 def do_POST_day(self, conn: DatabaseConnection, date: str, postvars: dict):
71 """Update or insert Day of date and fields defined in postvars."""
72 comment = postvars['comment'][0]
73 day = Day.by_date(conn, date, create=True)
77 def _init_handling(self):
78 conn = DatabaseConnection(self.server.db)
79 parsed_url = urlparse(self.path)
80 site = path_split(parsed_url.path)[1]
81 params = parse_qs(parsed_url.query)
82 return conn, site, params
84 def _redirect(self, target: str):
85 self.send_response(302)
86 self.send_header('Location', target)
89 def _send_html(self, html: str, code: int = 200):
90 """Send HTML as proper HTTP response."""
91 self.send_response(code)
93 self.wfile.write(bytes(html, 'utf-8'))
95 def _send_msg(self, msg: str, code: int = 400):
96 """Send message in HTML formatting as HTTP response."""
97 html = self.server.jinja.get_template('msg.html').render(msg=msg)
98 self._send_html(html, code)