1 """Web server stuff."""
2 from __future__ import annotations
3 from dataclasses import dataclass
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'
25 """Collect what's useful for Todo steps tree display."""
28 process: Process | None
29 children: list[TodoStepsNode]
30 fillable: bool = False
33 class TaskServer(HTTPServer):
34 """Variant of HTTPServer that knows .jinja as Jinja Environment."""
36 def __init__(self, db_file: DatabaseFile,
37 *args: Any, **kwargs: Any) -> None:
38 super().__init__(*args, **kwargs)
40 self.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
44 """Wrapper for validating and retrieving dict-like HTTP inputs."""
46 def __init__(self, dict_: dict[str, list[str]],
47 strictness: bool = True) -> None:
49 self.strict = strictness
51 def get_str(self, key: str, default: str = '',
52 ignore_strict: bool = False) -> str:
53 """Retrieve single/first string value of key, or default."""
54 if key not in self.inputs.keys() or 0 == len(self.inputs[key]):
55 if self.strict and not ignore_strict:
56 raise BadFormatException(f'no value found for key {key}')
58 return self.inputs[key][0]
60 def get_first_strings_starting(self, prefix: str) -> dict[str, str]:
61 """Retrieve dict of (first) strings at key starting with prefix."""
63 for key in [k for k in self.inputs.keys() if k.startswith(prefix)]:
64 ret[key] = self.inputs[key][0]
67 def get_int(self, key: str) -> int:
68 """Retrieve single/first value of key as int, error if empty."""
69 val = self.get_int_or_none(key)
71 raise BadFormatException(f'unexpected empty value for: {key}')
74 def get_int_or_none(self, key: str) -> int | None:
75 """Retrieve single/first value of key as int, return None if empty."""
76 val = self.get_str(key, ignore_strict=True)
81 except ValueError as e:
82 msg = f'cannot int form field value for key {key}: {val}'
83 raise BadFormatException(msg) from e
85 def get_float(self, key: str) -> float:
86 """Retrieve float value of key from self.postvars."""
87 val = self.get_str(key)
90 except ValueError as e:
91 msg = f'cannot float form field value for key {key}: {val}'
92 raise BadFormatException(msg) from e
94 def get_all_str(self, key: str) -> list[str]:
95 """Retrieve list of string values at key."""
96 if key not in self.inputs.keys():
98 return self.inputs[key]
100 def get_all_int(self, key: str) -> list[int]:
101 """Retrieve list of int values at key."""
102 all_str = self.get_all_str(key)
104 return [int(s) for s in all_str if len(s) > 0]
105 except ValueError as e:
106 msg = f'cannot int a form field value for key {key} in: {all_str}'
107 raise BadFormatException(msg) from e
110 class TaskHandler(BaseHTTPRequestHandler):
111 """Handles single HTTP request."""
112 # pylint: disable=too-many-public-methods
114 conn: DatabaseConnection
116 _form_data: InputsParser
117 _params: InputsParser
119 def do_GET(self) -> None:
120 """Handle any GET request."""
122 self._init_handling()
123 if hasattr(self, f'do_GET_{self._site}'):
124 template = f'{self._site}.html'
125 ctx = getattr(self, f'do_GET_{self._site}')()
126 html = self.server.jinja.get_template(template).render(**ctx)
127 self._send_html(html)
128 elif '' == self._site:
129 self._redirect('/day')
131 raise NotFoundException(f'Unknown page: /{self._site}')
132 except HandledException as error:
133 self._send_msg(error, code=error.http_code)
137 def _do_GET_calendar(self) -> dict[str, object]:
138 """Show Days from ?start= to ?end=."""
139 start = self._params.get_str('start')
140 end = self._params.get_str('end')
142 end = date_in_n_days(366)
143 ret = Day.by_date_range_with_limits(self.conn, (start, end), 'id')
144 days, start, end = ret
145 days = Day.with_filled_gaps(days, start, end)
147 day.collect_calendarized_todos(self.conn)
148 today = date_in_n_days(0)
149 return {'start': start, 'end': end, 'days': days, 'today': today}
151 def do_GET_calendar(self) -> dict[str, object]:
152 """Show Days from ?start= to ?end= – normal view."""
153 return self._do_GET_calendar()
155 def do_GET_calendar_txt(self) -> dict[str, object]:
156 """Show Days from ?start= to ?end= – minimalist view."""
157 return self._do_GET_calendar()
159 def do_GET_day(self) -> dict[str, object]:
160 """Show single Day of ?date=."""
161 date = self._params.get_str('date', date_in_n_days(0))
162 make_type = self._params.get_str('make_type')
163 todays_todos = Todo.by_date(self.conn, date)
165 for todo in todays_todos:
166 total_effort += todo.performed_effort
167 conditions_present = []
170 for todo in todays_todos:
171 for condition in todo.conditions + todo.blockers:
172 if condition not in conditions_present:
173 conditions_present += [condition]
174 enablers_for[condition.id_] = [p for p in
175 Process.all(self.conn)
176 if condition in p.enables]
177 disablers_for[condition.id_] = [p for p in
178 Process.all(self.conn)
179 if condition in p.disables]
180 seen_todos: set[int] = set()
181 top_nodes = [t.get_step_tree(seen_todos)
182 for t in todays_todos if not t.parents]
183 return {'day': Day.by_id(self.conn, date, create=True),
184 'total_effort': total_effort,
185 'top_nodes': top_nodes,
186 'make_type': make_type,
187 'enablers_for': enablers_for,
188 'disablers_for': disablers_for,
189 'conditions_present': conditions_present,
190 'processes': Process.all(self.conn)}
192 def do_GET_todo(self) -> dict[str, object]:
193 """Show single Todo of ?id=."""
195 def walk_process_steps(id_: int,
196 process_step_nodes: list[ProcessStepsNode],
197 steps_nodes: list[TodoStepsNode]) -> None:
198 for process_step_node in process_step_nodes:
200 node = TodoStepsNode(id_, None, process_step_node.process, [])
201 steps_nodes += [node]
202 walk_process_steps(id_, list(process_step_node.steps.values()),
205 def walk_todo_steps(id_: int, todos: list[Todo],
206 steps_nodes: list[TodoStepsNode]) -> None:
209 for match in [item for item in steps_nodes
211 and item.process == todo.process]:
214 for child in match.children:
215 child.fillable = True
216 walk_todo_steps(id_, todo.children, match.children)
219 node = TodoStepsNode(id_, todo, None, [])
220 steps_nodes += [node]
221 walk_todo_steps(id_, todo.children, node.children)
223 def collect_adoptables_keys(steps_nodes: list[TodoStepsNode]
226 for node in steps_nodes:
228 assert isinstance(node.process, Process)
229 assert isinstance(node.process.id_, int)
230 ids.add(node.process.id_)
231 ids = ids | collect_adoptables_keys(node.children)
234 id_ = self._params.get_int('id')
235 todo = Todo.by_id(self.conn, id_)
236 todo_steps = [step.todo for step in todo.get_step_tree(set()).children]
237 process_tree = todo.process.get_steps(self.conn, None)
238 steps_todo_to_process: list[TodoStepsNode] = []
239 walk_process_steps(0, list(process_tree.values()),
240 steps_todo_to_process)
241 for steps_node in steps_todo_to_process:
242 steps_node.fillable = True
243 walk_todo_steps(len(steps_todo_to_process), todo_steps,
244 steps_todo_to_process)
245 adoptables: dict[int, list[Todo]] = {}
246 any_adoptables = [Todo.by_id(self.conn, t.id_)
247 for t in Todo.by_date(self.conn, todo.date)
249 for id_ in collect_adoptables_keys(steps_todo_to_process):
250 adoptables[id_] = [t for t in any_adoptables
251 if t.process.id_ == id_]
252 return {'todo': todo, 'steps_todo_to_process': steps_todo_to_process,
253 'adoption_candidates_for': adoptables,
254 'process_candidates': Process.all(self.conn),
255 'todo_candidates': any_adoptables,
256 'condition_candidates': Condition.all(self.conn)}
258 def do_GET_todos(self) -> dict[str, object]:
259 """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern"""
260 sort_by = self._params.get_str('sort_by')
261 start = self._params.get_str('start')
262 end = self._params.get_str('end')
263 process_id = self._params.get_int_or_none('process_id')
264 comment_pattern = self._params.get_str('comment_pattern')
266 ret = Todo.by_date_range_with_limits(self.conn, (start, end))
267 todos_by_date_range, start, end = ret
268 todos = [t for t in todos_by_date_range
269 if comment_pattern in t.comment
270 and ((not process_id) or t.process.id_ == process_id)]
271 if sort_by == 'doneness':
272 todos.sort(key=lambda t: t.is_done)
273 elif sort_by == '-doneness':
274 todos.sort(key=lambda t: t.is_done, reverse=True)
275 elif sort_by == 'title':
276 todos.sort(key=lambda t: t.title_then)
277 elif sort_by == '-title':
278 todos.sort(key=lambda t: t.title_then, reverse=True)
279 elif sort_by == 'comment':
280 todos.sort(key=lambda t: t.comment)
281 elif sort_by == '-comment':
282 todos.sort(key=lambda t: t.comment, reverse=True)
283 elif sort_by == '-date':
284 todos.sort(key=lambda t: t.date, reverse=True)
286 todos.sort(key=lambda t: t.date)
287 return {'start': start, 'end': end, 'process_id': process_id,
288 'comment_pattern': comment_pattern, 'todos': todos,
289 'all_processes': Process.all(self.conn), 'sort_by': sort_by}
291 def do_GET_conditions(self) -> dict[str, object]:
292 """Show all Conditions."""
293 pattern = self._params.get_str('pattern')
294 conditions = Condition.matching(self.conn, pattern)
295 sort_by = self._params.get_str('sort_by')
296 if sort_by == 'is_active':
297 conditions.sort(key=lambda c: c.is_active)
298 elif sort_by == '-is_active':
299 conditions.sort(key=lambda c: c.is_active, reverse=True)
300 elif sort_by == '-title':
301 conditions.sort(key=lambda c: c.title.newest, reverse=True)
303 conditions.sort(key=lambda c: c.title.newest)
304 return {'conditions': conditions,
308 def do_GET_condition(self) -> dict[str, object]:
309 """Show Condition of ?id=."""
310 id_ = self._params.get_int_or_none('id')
311 c = Condition.by_id(self.conn, id_, create=True)
312 ps = Process.all(self.conn)
313 return {'condition': c, 'is_new': c.id_ is None,
314 'enabled_processes': [p for p in ps if c in p.conditions],
315 'disabled_processes': [p for p in ps if c in p.blockers],
316 'enabling_processes': [p for p in ps if c in p.enables],
317 'disabling_processes': [p for p in ps if c in p.disables]}
319 def do_GET_condition_titles(self) -> dict[str, object]:
320 """Show title history of Condition of ?id=."""
321 id_ = self._params.get_int_or_none('id')
322 condition = Condition.by_id(self.conn, id_)
323 return {'condition': condition}
325 def do_GET_condition_descriptions(self) -> dict[str, object]:
326 """Show description historys of Condition of ?id=."""
327 id_ = self._params.get_int_or_none('id')
328 condition = Condition.by_id(self.conn, id_)
329 return {'condition': condition}
331 def do_GET_process(self) -> dict[str, object]:
332 """Show Process of ?id=."""
333 id_ = self._params.get_int_or_none('id')
334 process = Process.by_id(self.conn, id_, create=True)
335 title_64 = self._params.get_str('title_b64')
337 title = b64decode(title_64.encode()).decode()
338 process.title.set(title)
339 owners = process.used_as_step_by(self.conn)
340 for step_id in self._params.get_all_int('step_to'):
341 owners += [Process.by_id(self.conn, step_id)]
342 preset_top_step = None
343 for process_id in self._params.get_all_int('has_step'):
344 preset_top_step = process_id
345 return {'process': process, 'is_new': process.id_ is None,
346 'preset_top_step': preset_top_step,
347 'steps': process.get_steps(self.conn), 'owners': owners,
348 'n_todos': len(Todo.by_process_id(self.conn, process.id_)),
349 'process_candidates': Process.all(self.conn),
350 'condition_candidates': Condition.all(self.conn)}
352 def do_GET_process_titles(self) -> dict[str, object]:
353 """Show title history of Process of ?id=."""
354 id_ = self._params.get_int_or_none('id')
355 process = Process.by_id(self.conn, id_)
356 return {'process': process}
358 def do_GET_process_descriptions(self) -> dict[str, object]:
359 """Show description historys of Process of ?id=."""
360 id_ = self._params.get_int_or_none('id')
361 process = Process.by_id(self.conn, id_)
362 return {'process': process}
364 def do_GET_process_efforts(self) -> dict[str, object]:
365 """Show default effort history of Process of ?id=."""
366 id_ = self._params.get_int_or_none('id')
367 process = Process.by_id(self.conn, id_)
368 return {'process': process}
370 def do_GET_processes(self) -> dict[str, object]:
371 """Show all Processes."""
372 pattern = self._params.get_str('pattern')
373 processes = Process.matching(self.conn, pattern)
374 sort_by = self._params.get_str('sort_by')
375 if sort_by == 'steps':
376 processes.sort(key=lambda p: len(p.explicit_steps))
377 elif sort_by == '-steps':
378 processes.sort(key=lambda p: len(p.explicit_steps), reverse=True)
379 elif sort_by == 'owners':
380 processes.sort(key=lambda p: p.n_owners or 0)
381 elif sort_by == '-owners':
382 processes.sort(key=lambda p: p.n_owners or 0, reverse=True)
383 elif sort_by == 'effort':
384 processes.sort(key=lambda p: p.effort.newest)
385 elif sort_by == '-effort':
386 processes.sort(key=lambda p: p.effort.newest, reverse=True)
387 elif sort_by == '-title':
388 processes.sort(key=lambda p: p.title.newest, reverse=True)
390 processes.sort(key=lambda p: p.title.newest)
391 return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern}
393 def do_POST(self) -> None:
394 """Handle any POST request."""
396 self._init_handling()
397 length = int(self.headers['content-length'])
398 postvars = parse_qs(self.rfile.read(length).decode(),
399 keep_blank_values=True, strict_parsing=True)
400 self._form_data = InputsParser(postvars)
401 if hasattr(self, f'do_POST_{self._site}'):
402 redir_target = getattr(self, f'do_POST_{self._site}')()
405 msg = f'Page not known as POST target: /{self._site}'
406 raise NotFoundException(msg)
407 self._redirect(redir_target)
408 except HandledException as error:
409 self._send_msg(error, code=error.http_code)
413 def do_POST_day(self) -> str:
414 """Update or insert Day of date and Todos mapped to it."""
415 date = self._params.get_str('date')
416 day = Day.by_id(self.conn, date, create=True)
417 day.comment = self._form_data.get_str('day_comment')
419 make_type = self._form_data.get_str('make_type')
420 for process_id in sorted(self._form_data.get_all_int('new_todo')):
421 if 'empty' == make_type:
422 process = Process.by_id(self.conn, process_id)
423 todo = Todo(None, process, False, date)
426 Todo.create_with_children(self.conn, process_id, date)
427 done_ids = self._form_data.get_all_int('done')
428 comments = self._form_data.get_all_str('comment')
429 efforts = self._form_data.get_all_str('effort')
430 for i, todo_id in enumerate(self._form_data.get_all_int('todo_id')):
431 todo = Todo.by_id(self.conn, todo_id)
432 todo.is_done = todo_id in done_ids
433 if len(comments) > 0:
434 todo.comment = comments[i]
436 todo.effort = float(efforts[i]) if efforts[i] else None
438 for condition in todo.enables:
439 condition.save(self.conn)
440 for condition in todo.disables:
441 condition.save(self.conn)
442 return f'/day?date={date}&make_type={make_type}'
444 def do_POST_todo(self) -> str:
445 """Update Todo and its children."""
446 # pylint: disable=too-many-locals
447 # pylint: disable=too-many-branches
448 id_ = self._params.get_int('id')
449 for _ in self._form_data.get_all_str('delete'):
450 todo = Todo .by_id(self.conn, id_)
451 todo.remove(self.conn)
453 todo = Todo.by_id(self.conn, id_)
454 adopted_child_ids = self._form_data.get_all_int('adopt')
455 processes_to_make_full = self._form_data.get_all_int('make_full')
456 processes_to_make_empty = self._form_data.get_all_int('make_empty')
457 fill_fors = self._form_data.get_first_strings_starting('fill_for_')
458 for v in fill_fors.values():
459 if v.startswith('make_empty_'):
460 processes_to_make_empty += [int(v[11:])]
461 elif v.startswith('make_full_'):
462 processes_to_make_full += [int(v[10:])]
464 adopted_child_ids += [int(v)]
466 for child in todo.children:
467 assert isinstance(child.id_, int)
468 if child.id_ not in adopted_child_ids:
469 to_remove += [child.id_]
470 for id_ in to_remove:
471 child = Todo.by_id(self.conn, id_)
472 todo.remove_child(child)
473 for child_id in adopted_child_ids:
474 if child_id in [c.id_ for c in todo.children]:
476 child = Todo.by_id(self.conn, child_id)
477 todo.add_child(child)
478 for process_id in processes_to_make_empty:
479 process = Process.by_id(self.conn, process_id)
480 made = Todo(None, process, False, todo.date)
483 for process_id in processes_to_make_full:
484 made = Todo.create_with_children(self.conn, process_id, todo.date)
486 effort = self._form_data.get_str('effort', ignore_strict=True)
487 todo.effort = float(effort) if effort else None
488 todo.set_conditions(self.conn,
489 self._form_data.get_all_int('condition'))
490 todo.set_blockers(self.conn, self._form_data.get_all_int('blocker'))
491 todo.set_enables(self.conn, self._form_data.get_all_int('enables'))
492 todo.set_disables(self.conn, self._form_data.get_all_int('disables'))
493 todo.is_done = len(self._form_data.get_all_str('done')) > 0
494 todo.calendarize = len(self._form_data.get_all_str('calendarize')) > 0
495 todo.comment = self._form_data.get_str('comment', ignore_strict=True)
497 for condition in todo.enables:
498 condition.save(self.conn)
499 for condition in todo.disables:
500 condition.save(self.conn)
501 return f'/todo?id={todo.id_}'
503 def _do_POST_versioned_timestamps(self, cls: Any, attr_name: str) -> str:
504 """Update history timestamps for VersionedAttribute."""
505 id_ = self._params.get_int_or_none('id')
506 item = cls.by_id(self.conn, id_)
507 attr = getattr(item, attr_name)
508 for k, v in self._form_data.get_first_strings_starting('at:').items():
511 attr.reset_timestamp(old, f'{v}.0')
513 cls_name = cls.__name__.lower()
514 return f'/{cls_name}_{attr_name}s?id={item.id_}'
516 def do_POST_process_descriptions(self) -> str:
517 """Update history timestamps for Process.description."""
518 return self._do_POST_versioned_timestamps(Process, 'description')
520 def do_POST_process_efforts(self) -> str:
521 """Update history timestamps for Process.effort."""
522 return self._do_POST_versioned_timestamps(Process, 'effort')
524 def do_POST_process_titles(self) -> str:
525 """Update history timestamps for Process.title."""
526 return self._do_POST_versioned_timestamps(Process, 'title')
528 def do_POST_process(self) -> str:
529 """Update or insert Process of ?id= and fields defined in postvars."""
530 # pylint: disable=too-many-branches
531 id_ = self._params.get_int_or_none('id')
532 for _ in self._form_data.get_all_str('delete'):
533 process = Process.by_id(self.conn, id_)
534 process.remove(self.conn)
536 process = Process.by_id(self.conn, id_, create=True)
537 process.title.set(self._form_data.get_str('title'))
538 process.description.set(self._form_data.get_str('description'))
539 process.effort.set(self._form_data.get_float('effort'))
540 process.set_conditions(self.conn,
541 self._form_data.get_all_int('condition'))
542 process.set_blockers(self.conn, self._form_data.get_all_int('blocker'))
543 process.set_enables(self.conn, self._form_data.get_all_int('enables'))
544 process.set_disables(self.conn,
545 self._form_data.get_all_int('disables'))
546 process.calendarize = self._form_data.get_all_str('calendarize') != []
547 process.save(self.conn)
548 assert isinstance(process.id_, int)
549 steps: list[ProcessStep] = []
550 for step_id in self._form_data.get_all_int('keep_step'):
551 if step_id not in self._form_data.get_all_int('steps'):
552 raise BadFormatException('trying to keep unknown step')
553 for step_id in self._form_data.get_all_int('steps'):
554 if step_id not in self._form_data.get_all_int('keep_step'):
556 step_process_id = self._form_data.get_int(
557 f'step_{step_id}_process_id')
558 parent_id = self._form_data.get_int_or_none(
559 f'step_{step_id}_parent_id')
560 steps += [ProcessStep(step_id, process.id_, step_process_id,
562 for step_id in self._form_data.get_all_int('steps'):
563 for step_process_id in self._form_data.get_all_int(
564 f'new_step_to_{step_id}'):
565 steps += [ProcessStep(None, process.id_, step_process_id,
567 new_step_title = None
568 for step_identifier in self._form_data.get_all_str('new_top_step'):
570 step_process_id = int(step_identifier)
571 steps += [ProcessStep(None, process.id_, step_process_id,
574 new_step_title = step_identifier
576 process.set_steps(self.conn, steps)
577 process.set_step_suppressions(self.conn,
579 get_all_int('suppresses'))
580 process.save(self.conn)
582 new_owner_title = None
583 for owner_identifier in self._form_data.get_all_str('step_of'):
585 owners_to_set += [int(owner_identifier)]
587 new_owner_title = owner_identifier
588 process.set_owners(self.conn, owners_to_set)
589 params = f'id={process.id_}'
591 title_b64_encoded = b64encode(new_step_title.encode()).decode()
592 params = f'step_to={process.id_}&title_b64={title_b64_encoded}'
593 elif new_owner_title:
594 title_b64_encoded = b64encode(new_owner_title.encode()).decode()
595 params = f'has_step={process.id_}&title_b64={title_b64_encoded}'
596 return f'/process?{params}'
598 def do_POST_condition_descriptions(self) -> str:
599 """Update history timestamps for Condition.description."""
600 return self._do_POST_versioned_timestamps(Condition, 'description')
602 def do_POST_condition_titles(self) -> str:
603 """Update history timestamps for Condition.title."""
604 return self._do_POST_versioned_timestamps(Condition, 'title')
606 def do_POST_condition(self) -> str:
607 """Update/insert Condition of ?id= and fields defined in postvars."""
608 id_ = self._params.get_int_or_none('id')
609 for _ in self._form_data.get_all_str('delete'):
610 condition = Condition.by_id(self.conn, id_)
611 condition.remove(self.conn)
613 condition = Condition.by_id(self.conn, id_, create=True)
614 condition.is_active = self._form_data.get_all_str('is_active') != []
615 condition.title.set(self._form_data.get_str('title'))
616 condition.description.set(self._form_data.get_str('description'))
617 condition.save(self.conn)
618 return f'/condition?id={condition.id_}'
620 def _init_handling(self) -> None:
621 """Our own __init__, as we're not supposed to use the original."""
622 self.conn = DatabaseConnection(self.server.db)
623 parsed_url = urlparse(self.path)
624 self._site = path_split(parsed_url.path)[1]
625 params = parse_qs(parsed_url.query, strict_parsing=True)
626 self._params = InputsParser(params, False)
628 def _redirect(self, target: str) -> None:
629 """Redirect to target."""
630 self.send_response(302)
631 self.send_header('Location', target)
634 def _send_html(self, html: str, code: int = 200) -> None:
635 """Send HTML as proper HTTP response."""
636 self.send_response(code)
638 self.wfile.write(bytes(html, 'utf-8'))
640 def _send_msg(self, msg: Exception, code: int = 400) -> None:
641 """Send message in HTML formatting as HTTP response."""
642 html = self.server.jinja.get_template('msg.html').render(msg=msg)
643 self._send_html(html, code)