home · contact · privacy
537f21fff50f5dea2a75daf66edc368107ab0902
[plomtask] / plomtask / http.py
1 """Web server stuff."""
2 from typing import Any
3 from http.server import BaseHTTPRequestHandler
4 from http.server import HTTPServer
5 from urllib.parse import urlparse, parse_qs
6 from os.path import split as path_split
7 from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
8 from plomtask.dating import date_in_n_days
9 from plomtask.days import Day
10 from plomtask.exceptions import HandledException, BadFormatException, \
11         NotFoundException
12 from plomtask.db import DatabaseConnection, DatabaseFile
13 from plomtask.processes import Process
14 from plomtask.conditions import Condition
15 from plomtask.todos import Todo
16
17 TEMPLATES_DIR = 'templates'
18
19
20 class TaskServer(HTTPServer):
21     """Variant of HTTPServer that knows .jinja as Jinja Environment."""
22
23     def __init__(self, db_file: DatabaseFile,
24                  *args: Any, **kwargs: Any) -> None:
25         super().__init__(*args, **kwargs)
26         self.db = db_file
27         self.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
28
29
30 class InputsParser:
31     """Wrapper for validating and retrieving dict-like HTTP inputs."""
32
33     def __init__(self, dict_: dict[str, list[str]],
34                  strictness: bool = True) -> None:
35         self.inputs = dict_
36         self.strict = strictness
37
38     def get_str(self, key: str, default: str = '',
39                 ignore_strict: bool = False) -> str:
40         """Retrieve single/first string value of key, or default."""
41         if key not in self.inputs.keys() or 0 == len(self.inputs[key]):
42             if self.strict and not ignore_strict:
43                 raise BadFormatException(f'no value found for key {key}')
44             return default
45         return self.inputs[key][0]
46
47     def get_int(self, key: str) -> int:
48         """Retrieve single/first value of key as int, error if empty."""
49         val = self.get_int_or_none(key)
50         if val is None:
51             raise BadFormatException(f'unexpected empty value for: {key}')
52         return val
53
54     def get_int_or_none(self, key: str) -> int | None:
55         """Retrieve single/first value of key as int, return None if empty."""
56         val = self.get_str(key, ignore_strict=True)
57         if val == '':
58             return None
59         try:
60             return int(val)
61         except ValueError as e:
62             msg = f'cannot int form field value for key {key}: {val}'
63             raise BadFormatException(msg) from e
64
65     def get_float(self, key: str) -> float:
66         """Retrieve float value of key from self.postvars."""
67         val = self.get_str(key)
68         try:
69             return float(val)
70         except ValueError as e:
71             msg = f'cannot float form field value for key {key}: {val}'
72             raise BadFormatException(msg) from e
73
74     def get_all_str(self, key: str) -> list[str]:
75         """Retrieve list of string values at key."""
76         if key not in self.inputs.keys():
77             return []
78         return self.inputs[key]
79
80     def get_all_int(self, key: str) -> list[int]:
81         """Retrieve list of int values at key."""
82         all_str = self.get_all_str(key)
83         try:
84             return [int(s) for s in all_str if len(s) > 0]
85         except ValueError as e:
86             msg = f'cannot int a form field value for key {key} in: {all_str}'
87             raise BadFormatException(msg) from e
88
89
90 class TaskHandler(BaseHTTPRequestHandler):
91     """Handles single HTTP request."""
92     server: TaskServer
93
94     def do_GET(self) -> None:
95         """Handle any GET request."""
96         try:
97             self._init_handling()
98             if hasattr(self, f'do_GET_{self.site}'):
99                 template = f'{self.site}.html'
100                 ctx = getattr(self, f'do_GET_{self.site}')()
101                 html = self.server.jinja.get_template(template).render(**ctx)
102                 self._send_html(html)
103             elif '' == self.site:
104                 self._redirect('/day')
105             else:
106                 raise NotFoundException(f'Unknown page: /{self.site}')
107         except HandledException as error:
108             self._send_msg(error, code=error.http_code)
109         finally:
110             self.conn.close()
111
112     def do_GET_calendar(self) -> dict[str, object]:
113         """Show Days from ?start= to ?end=."""
114         start = self.params.get_str('start')
115         end = self.params.get_str('end')
116         if not end:
117             end = date_in_n_days(60)
118         ret = Day.by_date_range_with_limits(self.conn, (start, end), 'id')
119         days, start, end = ret
120         days = Day.with_filled_gaps(days, start, end)
121         for day in days:
122             day.collect_calendarized_todos(self.conn)
123         today = date_in_n_days(0)
124         return {'start': start, 'end': end, 'days': days, 'today': today}
125
126     def do_GET_day(self) -> dict[str, object]:
127         """Show single Day of ?date=."""
128         date = self.params.get_str('date', date_in_n_days(0))
129         todays_todos = Todo.by_date(self.conn, date)
130         conditions_present = []
131         enablers_for = {}
132         disablers_for = {}
133         for todo in todays_todos:
134             for condition in todo.conditions + todo.blockers:
135                 if condition not in conditions_present:
136                     conditions_present += [condition]
137                     enablers_for[condition.id_] = [p for p in
138                                                    Process.all(self.conn)
139                                                    if condition in p.enables]
140                     disablers_for[condition.id_] = [p for p in
141                                                     Process.all(self.conn)
142                                                     if condition in p.disables]
143         seen_todos: set[int] = set()
144         top_nodes = [t.get_step_tree(seen_todos)
145                      for t in todays_todos if not t.parents]
146         return {'day': Day.by_id(self.conn, date, create=True),
147                 'top_nodes': top_nodes,
148                 'enablers_for': enablers_for,
149                 'disablers_for': disablers_for,
150                 'conditions_present': conditions_present,
151                 'processes': Process.all(self.conn)}
152
153     def do_GET_todo(self) -> dict[str, object]:
154         """Show single Todo of ?id=."""
155         id_ = self.params.get_int('id')
156         todo = Todo.by_id(self.conn, id_)
157         return {'todo': todo,
158                 'todo_candidates': Todo.by_date(self.conn, todo.date),
159                 'condition_candidates': Condition.all(self.conn)}
160
161     def do_GET_todos(self) -> dict[str, object]:
162         """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern"""
163         sort_by = self.params.get_str('sort_by')
164         start = self.params.get_str('start')
165         end = self.params.get_str('end')
166         process_id = self.params.get_int_or_none('process_id')
167         comment_pattern = self.params.get_str('comment_pattern')
168         todos = []
169         ret = Todo.by_date_range_with_limits(self.conn, (start, end))
170         todos_by_date_range, start, end = ret
171         todos = [t for t in todos_by_date_range
172                  if comment_pattern in t.comment
173                  and ((not process_id) or t.process.id_ == process_id)]
174         if sort_by == 'doneness':
175             todos.sort(key=lambda t: t.is_done)
176         elif sort_by == '-doneness':
177             todos.sort(key=lambda t: t.is_done, reverse=True)
178         elif sort_by == 'process':
179             todos.sort(key=lambda t: t.title_then)
180         elif sort_by == '-process':
181             todos.sort(key=lambda t: t.title_then, reverse=True)
182         elif sort_by == 'comment':
183             todos.sort(key=lambda t: t.comment)
184         elif sort_by == '-comment':
185             todos.sort(key=lambda t: t.comment, reverse=True)
186         elif sort_by == '-date':
187             todos.sort(key=lambda t: t.date, reverse=True)
188         else:
189             todos.sort(key=lambda t: t.date)
190         return {'start': start, 'end': end, 'process_id': process_id,
191                 'comment_pattern': comment_pattern, 'todos': todos,
192                 'all_processes': Process.all(self.conn), 'sort_by': sort_by}
193
194     def do_GET_conditions(self) -> dict[str, object]:
195         """Show all Conditions."""
196         pattern = self.params.get_str('pattern')
197         conditions = Condition.matching(self.conn, pattern)
198         sort_by = self.params.get_str('sort_by')
199         if sort_by == 'is_active':
200             conditions.sort(key=lambda c: c.is_active)
201         elif sort_by == '-is_active':
202             conditions.sort(key=lambda c: c.is_active, reverse=True)
203         elif sort_by == '-title':
204             conditions.sort(key=lambda c: c.title.newest, reverse=True)
205         else:
206             conditions.sort(key=lambda c: c.title.newest)
207         return {'conditions': conditions,
208                 'sort_by': sort_by,
209                 'pattern': pattern}
210
211     def do_GET_condition(self) -> dict[str, object]:
212         """Show Condition of ?id=."""
213         id_ = self.params.get_int_or_none('id')
214         c = Condition.by_id(self.conn, id_, create=True)
215         ps = Process.all(self.conn)
216         return {'condition': c,
217                 'enabled_processes': [p for p in ps if c in p.conditions],
218                 'disabled_processes': [p for p in ps if c in p.blockers],
219                 'enabling_processes': [p for p in ps if c in p.enables],
220                 'disabling_processes': [p for p in ps if c in p.disables]}
221
222     def do_GET_condition_titles(self) -> dict[str, object]:
223         """Show title history of Condition of ?id=."""
224         id_ = self.params.get_int_or_none('id')
225         condition = Condition.by_id(self.conn, id_)
226         return {'condition': condition}
227
228     def do_GET_condition_descriptions(self) -> dict[str, object]:
229         """Show description historys of Condition of ?id=."""
230         id_ = self.params.get_int_or_none('id')
231         condition = Condition.by_id(self.conn, id_)
232         return {'condition': condition}
233
234     def do_GET_process(self) -> dict[str, object]:
235         """Show Process of ?id=."""
236         id_ = self.params.get_int_or_none('id')
237         process = Process.by_id(self.conn, id_, create=True)
238         return {'process': process,
239                 'steps': process.get_steps(self.conn),
240                 'owners': process.used_as_step_by(self.conn),
241                 'n_todos': len(Todo.by_process_id(self.conn, process.id_)),
242                 'step_candidates': Process.all(self.conn),
243                 'condition_candidates': Condition.all(self.conn)}
244
245     def do_GET_process_titles(self) -> dict[str, object]:
246         """Show title history of Process of ?id=."""
247         id_ = self.params.get_int_or_none('id')
248         process = Process.by_id(self.conn, id_)
249         return {'process': process}
250
251     def do_GET_process_descriptions(self) -> dict[str, object]:
252         """Show description historys of Process of ?id=."""
253         id_ = self.params.get_int_or_none('id')
254         process = Process.by_id(self.conn, id_)
255         return {'process': process}
256
257     def do_GET_process_efforts(self) -> dict[str, object]:
258         """Show default effort history of Process of ?id=."""
259         id_ = self.params.get_int_or_none('id')
260         process = Process.by_id(self.conn, id_)
261         return {'process': process}
262
263     def do_GET_processes(self) -> dict[str, object]:
264         """Show all Processes."""
265         pattern = self.params.get_str('pattern')
266         processes = Process.matching(self.conn, pattern)
267         sort_by = self.params.get_str('sort_by')
268         if sort_by == 'steps':
269             processes.sort(key=lambda p: len(p.explicit_steps))
270         elif sort_by == '-steps':
271             processes.sort(key=lambda p: len(p.explicit_steps), reverse=True)
272         elif sort_by == '-title':
273             processes.sort(key=lambda p: p.title.newest, reverse=True)
274         else:
275             processes.sort(key=lambda p: p.title.newest)
276         return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern}
277
278     def do_POST(self) -> None:
279         """Handle any POST request."""
280         # pylint: disable=attribute-defined-outside-init
281         try:
282             self._init_handling()
283             length = int(self.headers['content-length'])
284             postvars = parse_qs(self.rfile.read(length).decode(),
285                                 keep_blank_values=True, strict_parsing=True)
286             self.form_data = InputsParser(postvars)
287             if hasattr(self, f'do_POST_{self.site}'):
288                 redir_target = getattr(self, f'do_POST_{self.site}')()
289                 self.conn.commit()
290             else:
291                 msg = f'Page not known as POST target: /{self.site}'
292                 raise NotFoundException(msg)
293             self._redirect(redir_target)
294         except HandledException as error:
295             self._send_msg(error, code=error.http_code)
296         finally:
297             self.conn.close()
298
299     def do_POST_day(self) -> str:
300         """Update or insert Day of date and Todos mapped to it."""
301         date = self.params.get_str('date')
302         day = Day.by_id(self.conn, date, create=True)
303         day.comment = self.form_data.get_str('day_comment')
304         day.save(self.conn)
305         Todo.create_with_children(self.conn, date,
306                                   self.form_data.get_all_int('new_todo'))
307         done_ids = self.form_data.get_all_int('done')
308         comments = self.form_data.get_all_str('comment')
309         efforts = self.form_data.get_all_str('effort')
310         for i, todo_id in enumerate(self.form_data.get_all_int('todo_id')):
311             todo = Todo.by_id(self.conn, todo_id)
312             todo.is_done = todo_id in done_ids
313             if len(comments) > 0:
314                 todo.comment = comments[i]
315             if len(efforts) > 0:
316                 todo.effort = float(efforts[i]) if efforts[i] else None
317             todo.save(self.conn)
318             for condition in todo.enables:
319                 condition.save(self.conn)
320             for condition in todo.disables:
321                 condition.save(self.conn)
322         return f'/day?date={date}'
323
324     def do_POST_todo(self) -> str:
325         """Update Todo and its children."""
326         id_ = self.params.get_int('id')
327         for _ in self.form_data.get_all_str('delete'):
328             todo = Todo .by_id(self.conn, id_)
329             todo.remove(self.conn)
330             return '/'
331         todo = Todo.by_id(self.conn, id_)
332         adopted_child_ids = self.form_data.get_all_int('adopt')
333         for child in todo.children:
334             if child.id_ not in adopted_child_ids:
335                 assert isinstance(child.id_, int)
336                 child = Todo.by_id(self.conn, child.id_)
337                 todo.remove_child(child)
338         for child_id in adopted_child_ids:
339             if child_id in [c.id_ for c in todo.children]:
340                 continue
341             child = Todo.by_id(self.conn, child_id)
342             todo.add_child(child)
343         effort = self.form_data.get_str('effort', ignore_strict=True)
344         todo.effort = float(effort) if effort else None
345         todo.set_conditions(self.conn, self.form_data.get_all_int('condition'))
346         todo.set_blockers(self.conn, self.form_data.get_all_int('blocker'))
347         todo.set_enables(self.conn, self.form_data.get_all_int('enables'))
348         todo.set_disables(self.conn, self.form_data.get_all_int('disables'))
349         todo.is_done = len(self.form_data.get_all_str('done')) > 0
350         todo.calendarize = len(self.form_data.get_all_str('calendarize')) > 0
351         todo.comment = self.form_data.get_str('comment', ignore_strict=True)
352         todo.save(self.conn)
353         for condition in todo.enables:
354             condition.save(self.conn)
355         for condition in todo.disables:
356             condition.save(self.conn)
357         return f'/todo?id={todo.id_}'
358
359     def do_POST_process(self) -> str:
360         """Update or insert Process of ?id= and fields defined in postvars."""
361         id_ = self.params.get_int_or_none('id')
362         for _ in self.form_data.get_all_str('delete'):
363             process = Process.by_id(self.conn, id_)
364             process.remove(self.conn)
365             return '/processes'
366         process = Process.by_id(self.conn, id_, create=True)
367         process.title.set(self.form_data.get_str('title'))
368         process.description.set(self.form_data.get_str('description'))
369         process.effort.set(self.form_data.get_float('effort'))
370         process.set_conditions(self.conn,
371                                self.form_data.get_all_int('condition'))
372         process.set_blockers(self.conn, self.form_data.get_all_int('blocker'))
373         process.set_enables(self.conn, self.form_data.get_all_int('enables'))
374         process.set_disables(self.conn, self.form_data.get_all_int('disables'))
375         process.calendarize = self.form_data.get_all_str('calendarize') != []
376         process.save(self.conn)
377         steps: list[tuple[int | None, int, int | None]] = []
378         for step_id in self.form_data.get_all_int('keep_step'):
379             if step_id not in self.form_data.get_all_int('steps'):
380                 raise BadFormatException('trying to keep unknown step')
381         for step_id in self.form_data.get_all_int('steps'):
382             for step_process_id in self.form_data.get_all_int(
383                     f'new_step_to_{step_id}'):
384                 steps += [(None, step_process_id, step_id)]
385             if step_id not in self.form_data.get_all_int('keep_step'):
386                 continue
387             step_process_id = self.form_data.get_int(
388                     f'step_{step_id}_process_id')
389             parent_id = self.form_data.get_int_or_none(
390                     f'step_{step_id}_parent_id')
391             steps += [(step_id, step_process_id, parent_id)]
392         for step_process_id in self.form_data.get_all_int('new_top_step'):
393             steps += [(None, step_process_id, None)]
394         process.set_steps(self.conn, steps)
395         process.save(self.conn)
396         return f'/process?id={process.id_}'
397
398     def do_POST_condition(self) -> str:
399         """Update/insert Condition of ?id= and fields defined in postvars."""
400         id_ = self.params.get_int_or_none('id')
401         for _ in self.form_data.get_all_str('delete'):
402             condition = Condition.by_id(self.conn, id_)
403             condition.remove(self.conn)
404             return '/conditions'
405         condition = Condition.by_id(self.conn, id_, create=True)
406         condition.is_active = self.form_data.get_all_str('is_active') != []
407         condition.title.set(self.form_data.get_str('title'))
408         condition.description.set(self.form_data.get_str('description'))
409         condition.save(self.conn)
410         return f'/condition?id={condition.id_}'
411
412     def _init_handling(self) -> None:
413         # pylint: disable=attribute-defined-outside-init
414         self.conn = DatabaseConnection(self.server.db)
415         parsed_url = urlparse(self.path)
416         self.site = path_split(parsed_url.path)[1]
417         params = parse_qs(parsed_url.query, strict_parsing=True)
418         self.params = InputsParser(params, False)
419
420     def _redirect(self, target: str) -> None:
421         self.send_response(302)
422         self.send_header('Location', target)
423         self.end_headers()
424
425     def _send_html(self, html: str, code: int = 200) -> None:
426         """Send HTML as proper HTTP response."""
427         self.send_response(code)
428         self.end_headers()
429         self.wfile.write(bytes(html, 'utf-8'))
430
431     def _send_msg(self, msg: Exception, code: int = 400) -> None:
432         """Send message in HTML formatting as HTTP response."""
433         html = self.server.jinja.get_template('msg.html').render(msg=msg)
434         self._send_html(html, code)