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