1 """Web server stuff."""
2 from __future__ import annotations
3 from dataclasses import dataclass
4 from typing import Any, Callable, Mapping
5 from base64 import b64encode, b64decode
6 from http.server import BaseHTTPRequestHandler
7 from http.server import HTTPServer
8 from urllib.parse import urlparse, parse_qs
9 from os.path import split as path_split
10 from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
11 from plomtask.dating import date_in_n_days
12 from plomtask.days import Day
13 from plomtask.exceptions import HandledException, BadFormatException, \
15 from plomtask.db import DatabaseConnection, DatabaseFile
16 from plomtask.processes import Process, ProcessStep, ProcessStepsNode
17 from plomtask.conditions import Condition
18 from plomtask.todos import Todo
20 TEMPLATES_DIR = 'templates'
23 class TaskServer(HTTPServer):
24 """Variant of HTTPServer that knows .jinja as Jinja Environment."""
26 def __init__(self, db_file: DatabaseFile,
27 *args: Any, **kwargs: Any) -> None:
28 super().__init__(*args, **kwargs)
30 self.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
34 """Wrapper for validating and retrieving dict-like HTTP inputs."""
36 def __init__(self, dict_: dict[str, list[str]],
37 strictness: bool = True) -> None:
39 self.strict = strictness
41 def get_str(self, key: str, default: str = '',
42 ignore_strict: bool = False) -> str:
43 """Retrieve single/first string value of key, or default."""
44 if key not in self.inputs.keys() or 0 == len(self.inputs[key]):
45 if self.strict and not ignore_strict:
46 raise BadFormatException(f'no value found for key {key}')
48 return self.inputs[key][0]
50 def get_first_strings_starting(self, prefix: str) -> dict[str, str]:
51 """Retrieve dict of (first) strings at key starting with prefix."""
53 for key in [k for k in self.inputs.keys() if k.startswith(prefix)]:
54 ret[key] = self.inputs[key][0]
57 def get_int(self, key: str) -> int:
58 """Retrieve single/first value of key as int, error if empty."""
59 val = self.get_int_or_none(key)
61 raise BadFormatException(f'unexpected empty value for: {key}')
64 def get_int_or_none(self, key: str) -> int | None:
65 """Retrieve single/first value of key as int, return None if empty."""
66 val = self.get_str(key, ignore_strict=True)
71 except ValueError as e:
72 msg = f'cannot int form field value for key {key}: {val}'
73 raise BadFormatException(msg) from e
75 def get_float(self, key: str) -> float:
76 """Retrieve float value of key from self.postvars."""
77 val = self.get_str(key)
80 except ValueError as e:
81 msg = f'cannot float form field value for key {key}: {val}'
82 raise BadFormatException(msg) from e
84 def get_all_str(self, key: str) -> list[str]:
85 """Retrieve list of string values at key."""
86 if key not in self.inputs.keys():
88 return self.inputs[key]
90 def get_all_int(self, key: str) -> list[int]:
91 """Retrieve list of int values at key."""
92 all_str = self.get_all_str(key)
94 return [int(s) for s in all_str if len(s) > 0]
95 except ValueError as e:
96 msg = f'cannot int a form field value for key {key} in: {all_str}'
97 raise BadFormatException(msg) from e
100 class TaskHandler(BaseHTTPRequestHandler):
101 """Handles single HTTP request."""
102 # pylint: disable=too-many-public-methods
104 conn: DatabaseConnection
106 _form_data: InputsParser
107 _params: InputsParser
111 ctx: Mapping[str, object],
112 code: int = 200) -> None:
113 """Send HTML as proper HTTP response."""
114 tmpl = self.server.jinja.get_template(tmpl_name)
115 html = tmpl.render(ctx)
116 self.send_response(code)
118 self.wfile.write(bytes(html, 'utf-8'))
121 def _request_wrapper(http_method: str, not_found_msg: str
122 ) -> Callable[..., Callable[[TaskHandler], None]]:
123 def decorator(f: Callable[..., str | None]
124 ) -> Callable[[TaskHandler], None]:
125 def wrapper(self: TaskHandler) -> None:
126 # pylint: disable=protected-access
127 # (because pylint here fails to detect the use of wrapper as a
128 # method to self with respective access privileges)
130 self.conn = DatabaseConnection(self.server.db)
131 parsed_url = urlparse(self.path)
132 self._site = path_split(parsed_url.path)[1]
133 params = parse_qs(parsed_url.query, strict_parsing=True)
134 self._params = InputsParser(params, False)
135 handler_name = f'do_{http_method}_{self._site}'
136 if hasattr(self, handler_name):
137 handler = getattr(self, handler_name)
138 redir_target = f(self, handler)
140 self.send_response(302)
141 self.send_header('Location', redir_target)
144 msg = f'{not_found_msg}: {self._site}'
145 raise NotFoundException(msg)
146 except HandledException as error:
147 for cls in (Day, Todo, Condition, Process, ProcessStep):
148 assert hasattr(cls, 'empty_cache')
151 self._send_html('msg.html', ctx, error.http_code)
157 @_request_wrapper('GET', 'Unknown page')
158 def do_GET(self, handler: Callable[[], str | dict[str, object]]
160 """Render page with result of handler, or redirect if result is str."""
161 tmpl_name = f'{self._site}.html'
162 ctx_or_redir = handler()
163 if isinstance(ctx_or_redir, str):
165 self._send_html(tmpl_name, ctx_or_redir)
168 @_request_wrapper('POST', 'Unknown POST target')
169 def do_POST(self, handler: Callable[[], str]) -> str:
170 """Handle POST with handler, prepare redirection to result."""
171 length = int(self.headers['content-length'])
172 postvars = parse_qs(self.rfile.read(length).decode(),
173 keep_blank_values=True, strict_parsing=True)
174 self._form_data = InputsParser(postvars)
175 redir_target = handler()
181 def do_GET_(self) -> str:
182 """Return redirect target on GET /."""
185 def _do_GET_calendar(self) -> dict[str, object]:
186 """Show Days from ?start= to ?end=.
188 Both .do_GET_calendar and .do_GET_calendar_txt refer to this to do the
189 same, the only difference being the HTML template they are rendered to,
190 which .do_GET selects from their method name.
192 start = self._params.get_str('start')
193 end = self._params.get_str('end')
195 end = date_in_n_days(366)
196 ret = Day.by_date_range_with_limits(self.conn, (start, end), 'id')
197 days, start, end = ret
198 days = Day.with_filled_gaps(days, start, end)
199 today = date_in_n_days(0)
200 return {'start': start, 'end': end, 'days': days, 'today': today}
202 def do_GET_calendar(self) -> dict[str, object]:
203 """Show Days from ?start= to ?end= – normal view."""
204 return self._do_GET_calendar()
206 def do_GET_calendar_txt(self) -> dict[str, object]:
207 """Show Days from ?start= to ?end= – minimalist view."""
208 return self._do_GET_calendar()
210 def do_GET_day(self) -> dict[str, object]:
211 """Show single Day of ?date=."""
212 date = self._params.get_str('date', date_in_n_days(0))
213 day = Day.by_id(self.conn, date, create=True)
214 make_type = self._params.get_str('make_type')
215 conditions_present = []
218 for todo in day.todos:
219 for condition in todo.conditions + todo.blockers:
220 if condition not in conditions_present:
221 conditions_present += [condition]
222 enablers_for[condition.id_] = [p for p in
223 Process.all(self.conn)
224 if condition in p.enables]
225 disablers_for[condition.id_] = [p for p in
226 Process.all(self.conn)
227 if condition in p.disables]
228 seen_todos: set[int] = set()
229 top_nodes = [t.get_step_tree(seen_todos)
230 for t in day.todos if not t.parents]
232 'top_nodes': top_nodes,
233 'make_type': make_type,
234 'enablers_for': enablers_for,
235 'disablers_for': disablers_for,
236 'conditions_present': conditions_present,
237 'processes': Process.all(self.conn)}
239 def do_GET_todo(self) -> dict[str, object]:
240 """Show single Todo of ?id=."""
244 """Collect what's useful for Todo steps tree display."""
247 process: Process | None
248 children: list[TodoStepsNode] # pylint: disable=undefined-variable
249 fillable: bool = False
251 def walk_process_steps(id_: int,
252 process_step_nodes: list[ProcessStepsNode],
253 steps_nodes: list[TodoStepsNode]) -> None:
254 for process_step_node in process_step_nodes:
256 node = TodoStepsNode(id_, None, process_step_node.process, [])
257 steps_nodes += [node]
258 walk_process_steps(id_, list(process_step_node.steps.values()),
261 def walk_todo_steps(id_: int, todos: list[Todo],
262 steps_nodes: list[TodoStepsNode]) -> None:
265 for match in [item for item in steps_nodes
267 and item.process == todo.process]:
270 for child in match.children:
271 child.fillable = True
272 walk_todo_steps(id_, todo.children, match.children)
275 node = TodoStepsNode(id_, todo, None, [])
276 steps_nodes += [node]
277 walk_todo_steps(id_, todo.children, node.children)
279 def collect_adoptables_keys(steps_nodes: list[TodoStepsNode]
282 for node in steps_nodes:
284 assert isinstance(node.process, Process)
285 assert isinstance(node.process.id_, int)
286 ids.add(node.process.id_)
287 ids = ids | collect_adoptables_keys(node.children)
290 id_ = self._params.get_int('id')
291 todo = Todo.by_id(self.conn, id_)
292 todo_steps = [step.todo for step in todo.get_step_tree(set()).children]
293 process_tree = todo.process.get_steps(self.conn, None)
294 steps_todo_to_process: list[TodoStepsNode] = []
295 walk_process_steps(0, list(process_tree.values()),
296 steps_todo_to_process)
297 for steps_node in steps_todo_to_process:
298 steps_node.fillable = True
299 walk_todo_steps(len(steps_todo_to_process), todo_steps,
300 steps_todo_to_process)
301 adoptables: dict[int, list[Todo]] = {}
302 any_adoptables = [Todo.by_id(self.conn, t.id_)
303 for t in Todo.by_date(self.conn, todo.date)
305 for id_ in collect_adoptables_keys(steps_todo_to_process):
306 adoptables[id_] = [t for t in any_adoptables
307 if t.process.id_ == id_]
308 return {'todo': todo, 'steps_todo_to_process': steps_todo_to_process,
309 'adoption_candidates_for': adoptables,
310 'process_candidates': Process.all(self.conn),
311 'todo_candidates': any_adoptables,
312 'condition_candidates': Condition.all(self.conn)}
314 def do_GET_todos(self) -> dict[str, object]:
315 """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern"""
316 sort_by = self._params.get_str('sort_by')
317 start = self._params.get_str('start')
318 end = self._params.get_str('end')
319 process_id = self._params.get_int_or_none('process_id')
320 comment_pattern = self._params.get_str('comment_pattern')
322 ret = Todo.by_date_range_with_limits(self.conn, (start, end))
323 todos_by_date_range, start, end = ret
324 todos = [t for t in todos_by_date_range
325 if comment_pattern in t.comment
326 and ((not process_id) or t.process.id_ == process_id)]
327 if sort_by == 'doneness':
328 todos.sort(key=lambda t: t.is_done)
329 elif sort_by == '-doneness':
330 todos.sort(key=lambda t: t.is_done, reverse=True)
331 elif sort_by == 'title':
332 todos.sort(key=lambda t: t.title_then)
333 elif sort_by == '-title':
334 todos.sort(key=lambda t: t.title_then, reverse=True)
335 elif sort_by == 'comment':
336 todos.sort(key=lambda t: t.comment)
337 elif sort_by == '-comment':
338 todos.sort(key=lambda t: t.comment, reverse=True)
339 elif sort_by == '-date':
340 todos.sort(key=lambda t: t.date, reverse=True)
342 todos.sort(key=lambda t: t.date)
343 return {'start': start, 'end': end, 'process_id': process_id,
344 'comment_pattern': comment_pattern, 'todos': todos,
345 'all_processes': Process.all(self.conn), 'sort_by': sort_by}
347 def do_GET_conditions(self) -> dict[str, object]:
348 """Show all Conditions."""
349 pattern = self._params.get_str('pattern')
350 conditions = Condition.matching(self.conn, pattern)
351 sort_by = self._params.get_str('sort_by')
352 if sort_by == 'is_active':
353 conditions.sort(key=lambda c: c.is_active)
354 elif sort_by == '-is_active':
355 conditions.sort(key=lambda c: c.is_active, reverse=True)
356 elif sort_by == '-title':
357 conditions.sort(key=lambda c: c.title.newest, reverse=True)
359 conditions.sort(key=lambda c: c.title.newest)
360 return {'conditions': conditions,
364 def do_GET_condition(self) -> dict[str, object]:
365 """Show Condition of ?id=."""
366 id_ = self._params.get_int_or_none('id')
367 c = Condition.by_id(self.conn, id_, create=True)
368 ps = Process.all(self.conn)
369 return {'condition': c, 'is_new': c.id_ is None,
370 'enabled_processes': [p for p in ps if c in p.conditions],
371 'disabled_processes': [p for p in ps if c in p.blockers],
372 'enabling_processes': [p for p in ps if c in p.enables],
373 'disabling_processes': [p for p in ps if c in p.disables]}
375 def do_GET_condition_titles(self) -> dict[str, object]:
376 """Show title history of Condition of ?id=."""
377 id_ = self._params.get_int_or_none('id')
378 condition = Condition.by_id(self.conn, id_)
379 return {'condition': condition}
381 def do_GET_condition_descriptions(self) -> dict[str, object]:
382 """Show description historys of Condition of ?id=."""
383 id_ = self._params.get_int_or_none('id')
384 condition = Condition.by_id(self.conn, id_)
385 return {'condition': condition}
387 def do_GET_process(self) -> dict[str, object]:
388 """Show Process of ?id=."""
389 id_ = self._params.get_int_or_none('id')
390 process = Process.by_id(self.conn, id_, create=True)
391 title_64 = self._params.get_str('title_b64')
393 title = b64decode(title_64.encode()).decode()
394 process.title.set(title)
395 owners = process.used_as_step_by(self.conn)
396 for step_id in self._params.get_all_int('step_to'):
397 owners += [Process.by_id(self.conn, step_id)]
398 preset_top_step = None
399 for process_id in self._params.get_all_int('has_step'):
400 preset_top_step = process_id
401 return {'process': process, 'is_new': process.id_ is None,
402 'preset_top_step': preset_top_step,
403 'steps': process.get_steps(self.conn), 'owners': owners,
404 'n_todos': len(Todo.by_process_id(self.conn, process.id_)),
405 'process_candidates': Process.all(self.conn),
406 'condition_candidates': Condition.all(self.conn)}
408 def do_GET_process_titles(self) -> dict[str, object]:
409 """Show title history of Process of ?id=."""
410 id_ = self._params.get_int_or_none('id')
411 process = Process.by_id(self.conn, id_)
412 return {'process': process}
414 def do_GET_process_descriptions(self) -> dict[str, object]:
415 """Show description historys of Process of ?id=."""
416 id_ = self._params.get_int_or_none('id')
417 process = Process.by_id(self.conn, id_)
418 return {'process': process}
420 def do_GET_process_efforts(self) -> dict[str, object]:
421 """Show default effort history of Process of ?id=."""
422 id_ = self._params.get_int_or_none('id')
423 process = Process.by_id(self.conn, id_)
424 return {'process': process}
426 def do_GET_processes(self) -> dict[str, object]:
427 """Show all Processes."""
428 pattern = self._params.get_str('pattern')
429 processes = Process.matching(self.conn, pattern)
430 sort_by = self._params.get_str('sort_by')
431 if sort_by == 'steps':
432 processes.sort(key=lambda p: len(p.explicit_steps))
433 elif sort_by == '-steps':
434 processes.sort(key=lambda p: len(p.explicit_steps), reverse=True)
435 elif sort_by == 'owners':
436 processes.sort(key=lambda p: p.n_owners or 0)
437 elif sort_by == '-owners':
438 processes.sort(key=lambda p: p.n_owners or 0, reverse=True)
439 elif sort_by == 'effort':
440 processes.sort(key=lambda p: p.effort.newest)
441 elif sort_by == '-effort':
442 processes.sort(key=lambda p: p.effort.newest, reverse=True)
443 elif sort_by == '-title':
444 processes.sort(key=lambda p: p.title.newest, reverse=True)
446 processes.sort(key=lambda p: p.title.newest)
447 return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern}
451 def _change_versioned_timestamps(self, cls: Any, attr_name: str) -> str:
452 """Update history timestamps for VersionedAttribute."""
453 id_ = self._params.get_int_or_none('id')
454 item = cls.by_id(self.conn, id_)
455 attr = getattr(item, attr_name)
456 for k, v in self._form_data.get_first_strings_starting('at:').items():
459 attr.reset_timestamp(old, f'{v}.0')
461 cls_name = cls.__name__.lower()
462 return f'/{cls_name}_{attr_name}s?id={item.id_}'
464 def do_POST_day(self) -> str:
465 """Update or insert Day of date and Todos mapped to it."""
466 date = self._params.get_str('date')
467 day = Day.by_id(self.conn, date, create=True)
468 day.comment = self._form_data.get_str('day_comment')
470 make_type = self._form_data.get_str('make_type')
471 for process_id in sorted(self._form_data.get_all_int('new_todo')):
472 if 'empty' == make_type:
473 process = Process.by_id(self.conn, process_id)
474 todo = Todo(None, process, False, date)
477 Todo.create_with_children(self.conn, process_id, date)
478 done_ids = self._form_data.get_all_int('done')
479 comments = self._form_data.get_all_str('comment')
480 efforts = self._form_data.get_all_str('effort')
481 for i, todo_id in enumerate(self._form_data.get_all_int('todo_id')):
482 todo = Todo.by_id(self.conn, todo_id)
483 todo.is_done = todo_id in done_ids
484 if len(comments) > 0:
485 todo.comment = comments[i]
487 todo.effort = float(efforts[i]) if efforts[i] else None
489 return f'/day?date={date}&make_type={make_type}'
491 def do_POST_todo(self) -> str:
492 """Update Todo and its children."""
493 # pylint: disable=too-many-locals
494 # pylint: disable=too-many-branches
495 id_ = self._params.get_int('id')
496 for _ in self._form_data.get_all_str('delete'):
497 todo = Todo .by_id(self.conn, id_)
498 todo.remove(self.conn)
500 todo = Todo.by_id(self.conn, id_)
501 adopted_child_ids = self._form_data.get_all_int('adopt')
502 processes_to_make_full = self._form_data.get_all_int('make_full')
503 processes_to_make_empty = self._form_data.get_all_int('make_empty')
504 fill_fors = self._form_data.get_first_strings_starting('fill_for_')
505 for v in fill_fors.values():
506 if v.startswith('make_empty_'):
507 processes_to_make_empty += [int(v[11:])]
508 elif v.startswith('make_full_'):
509 processes_to_make_full += [int(v[10:])]
511 adopted_child_ids += [int(v)]
513 for child in todo.children:
514 assert isinstance(child.id_, int)
515 if child.id_ not in adopted_child_ids:
516 to_remove += [child.id_]
517 for id_ in to_remove:
518 child = Todo.by_id(self.conn, id_)
519 todo.remove_child(child)
520 for child_id in adopted_child_ids:
521 if child_id in [c.id_ for c in todo.children]:
523 child = Todo.by_id(self.conn, child_id)
524 todo.add_child(child)
525 for process_id in processes_to_make_empty:
526 process = Process.by_id(self.conn, process_id)
527 made = Todo(None, process, False, todo.date)
530 for process_id in processes_to_make_full:
531 made = Todo.create_with_children(self.conn, process_id, todo.date)
533 effort = self._form_data.get_str('effort', ignore_strict=True)
534 todo.effort = float(effort) if effort else None
535 todo.set_conditions(self.conn,
536 self._form_data.get_all_int('condition'))
537 todo.set_blockers(self.conn, self._form_data.get_all_int('blocker'))
538 todo.set_enables(self.conn, self._form_data.get_all_int('enables'))
539 todo.set_disables(self.conn, self._form_data.get_all_int('disables'))
540 todo.is_done = len(self._form_data.get_all_str('done')) > 0
541 todo.calendarize = len(self._form_data.get_all_str('calendarize')) > 0
542 todo.comment = self._form_data.get_str('comment', ignore_strict=True)
544 return f'/todo?id={todo.id_}'
546 def do_POST_process_descriptions(self) -> str:
547 """Update history timestamps for Process.description."""
548 return self._change_versioned_timestamps(Process, 'description')
550 def do_POST_process_efforts(self) -> str:
551 """Update history timestamps for Process.effort."""
552 return self._change_versioned_timestamps(Process, 'effort')
554 def do_POST_process_titles(self) -> str:
555 """Update history timestamps for Process.title."""
556 return self._change_versioned_timestamps(Process, 'title')
558 def do_POST_process(self) -> str:
559 """Update or insert Process of ?id= and fields defined in postvars."""
560 # pylint: disable=too-many-branches
561 id_ = self._params.get_int_or_none('id')
562 for _ in self._form_data.get_all_str('delete'):
563 process = Process.by_id(self.conn, id_)
564 process.remove(self.conn)
566 process = Process.by_id(self.conn, id_, create=True)
567 process.title.set(self._form_data.get_str('title'))
568 process.description.set(self._form_data.get_str('description'))
569 process.effort.set(self._form_data.get_float('effort'))
570 process.set_conditions(self.conn,
571 self._form_data.get_all_int('condition'))
572 process.set_blockers(self.conn, self._form_data.get_all_int('blocker'))
573 process.set_enables(self.conn, self._form_data.get_all_int('enables'))
574 process.set_disables(self.conn,
575 self._form_data.get_all_int('disables'))
576 process.calendarize = self._form_data.get_all_str('calendarize') != []
577 process.save(self.conn)
578 assert isinstance(process.id_, int)
579 steps: list[ProcessStep] = []
580 for step_id in self._form_data.get_all_int('keep_step'):
581 if step_id not in self._form_data.get_all_int('steps'):
582 raise BadFormatException('trying to keep unknown step')
583 for step_id in self._form_data.get_all_int('steps'):
584 if step_id not in self._form_data.get_all_int('keep_step'):
586 step_process_id = self._form_data.get_int(
587 f'step_{step_id}_process_id')
588 parent_id = self._form_data.get_int_or_none(
589 f'step_{step_id}_parent_id')
590 steps += [ProcessStep(step_id, process.id_, step_process_id,
592 for step_id in self._form_data.get_all_int('steps'):
593 for step_process_id in self._form_data.get_all_int(
594 f'new_step_to_{step_id}'):
595 steps += [ProcessStep(None, process.id_, step_process_id,
597 new_step_title = None
598 for step_identifier in self._form_data.get_all_str('new_top_step'):
600 step_process_id = int(step_identifier)
601 steps += [ProcessStep(None, process.id_, step_process_id,
604 new_step_title = step_identifier
605 process.set_steps(self.conn, steps)
606 process.set_step_suppressions(self.conn,
608 get_all_int('suppresses'))
610 new_owner_title = None
611 for owner_identifier in self._form_data.get_all_str('step_of'):
613 owners_to_set += [int(owner_identifier)]
615 new_owner_title = owner_identifier
616 process.set_owners(self.conn, owners_to_set)
617 params = f'id={process.id_}'
619 title_b64_encoded = b64encode(new_step_title.encode()).decode()
620 params = f'step_to={process.id_}&title_b64={title_b64_encoded}'
621 elif new_owner_title:
622 title_b64_encoded = b64encode(new_owner_title.encode()).decode()
623 params = f'has_step={process.id_}&title_b64={title_b64_encoded}'
624 process.save(self.conn)
625 return f'/process?{params}'
627 def do_POST_condition_descriptions(self) -> str:
628 """Update history timestamps for Condition.description."""
629 return self._change_versioned_timestamps(Condition, 'description')
631 def do_POST_condition_titles(self) -> str:
632 """Update history timestamps for Condition.title."""
633 return self._change_versioned_timestamps(Condition, 'title')
635 def do_POST_condition(self) -> str:
636 """Update/insert Condition of ?id= and fields defined in postvars."""
637 id_ = self._params.get_int_or_none('id')
638 for _ in self._form_data.get_all_str('delete'):
639 condition = Condition.by_id(self.conn, id_)
640 condition.remove(self.conn)
642 condition = Condition.by_id(self.conn, id_, create=True)
643 condition.is_active = self._form_data.get_all_str('is_active') != []
644 condition.title.set(self._form_data.get_str('title'))
645 condition.description.set(self._form_data.get_str('description'))
646 condition.save(self.conn)
647 return f'/condition?id={condition.id_}'