home · contact · privacy
Fix miswritten test.
[plomtask] / plomtask / http.py
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
10
11 TEMPLATES_DIR = 'templates'
12
13
14 class TaskServer(HTTPServer):
15     """Variant of HTTPServer that knows .jinja as Jinja Environment."""
16
17     def __init__(self, db_file, *args, **kwargs):
18         super().__init__(*args, **kwargs)
19         self.db = db_file
20         self.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
21
22
23 class TaskHandler(BaseHTTPRequestHandler):
24     """Handles single HTTP request."""
25     server: TaskServer
26
27     def do_GET(self):
28         """Handle any GET request."""
29         try:
30             conn, site, params = self._init_handling()
31             if 'calendar' == site:
32                 html = self.do_GET_calendar(conn)
33             elif 'day' == site:
34                 date = params.get('date', ['2024-01-01'])[0]
35                 html = self.do_GET_day(conn, date)
36             else:
37                 raise HandledException('Test!')
38             conn.commit()
39             conn.close()
40             self._send_html(html)
41         except HandledException as error:
42             self._send_msg(error)
43
44     def do_GET_calendar(self, conn: DatabaseConnection):
45         """Show Days."""
46         return self.server.jinja.get_template('calendar.html').render(
47                 days=Day.all(conn))
48
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)
53
54     def do_POST(self):
55         """Handle any POST request."""
56         try:
57             conn, site, params = self._init_handling()
58             length = int(self.headers['content-length'])
59             postvars = parse_qs(self.rfile.read(length).decode(),
60                                 keep_blank_values=1)
61             if 'day' == site:
62                 date = params.get('date', [None])[0]
63                 self.do_POST_day(conn, date, postvars)
64             conn.commit()
65             conn.close()
66             self._redirect('/')
67         except HandledException as error:
68             self._send_msg(error)
69
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)
74         day.comment = comment
75         day.save(conn)
76
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
83
84     def _redirect(self, target: str):
85         self.send_response(302)
86         self.send_header('Location', target)
87         self.end_headers()
88
89     def _send_html(self, html: str, code: int = 200):
90         """Send HTML as proper HTTP response."""
91         self.send_response(code)
92         self.end_headers()
93         self.wfile.write(bytes(html, 'utf-8'))
94
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)