home · contact · privacy
3d1dd5036f909a553e6678f171fcace7c379cafc
[plomtask] / plomtask / http.py
1 """Web server stuff."""
2 from __future__ import annotations
3 from dataclasses import dataclass
4 from typing import Any, Callable
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 json import dumps as json_dumps
10 from os.path import split as path_split
11 from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
12 from plomtask.dating import date_in_n_days
13 from plomtask.days import Day
14 from plomtask.exceptions import HandledException, BadFormatException, \
15         NotFoundException
16 from plomtask.db import DatabaseConnection, DatabaseFile
17 from plomtask.processes import Process, ProcessStep, ProcessStepsNode
18 from plomtask.conditions import Condition
19 from plomtask.todos import Todo
20
21 TEMPLATES_DIR = 'templates'
22
23
24 class TaskServer(HTTPServer):
25     """Variant of HTTPServer that knows .jinja as Jinja Environment."""
26
27     def __init__(self, db_file: DatabaseFile,
28                  *args: Any, **kwargs: Any) -> None:
29         super().__init__(*args, **kwargs)
30         self.db = db_file
31         self.headers: list[tuple[str, str]] = []
32         self._render_mode = 'html'
33         self._jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
34
35     def set_json_mode(self) -> None:
36         """Make server send JSON instead of HTML responses."""
37         self._render_mode = 'json'
38         self.headers += [('Content-Type', 'application/json')]
39
40     @staticmethod
41     def ctx_to_json(ctx: dict[str, object]) -> str:
42         """Render ctx into JSON string."""
43         def walk_ctx(node: object) -> Any:
44             if hasattr(node, 'as_dict_into_reference'):
45                 if hasattr(node, 'id_') and node.id_ is not None:
46                     return node.as_dict_into_reference(library)
47             if hasattr(node, 'as_dict'):
48                 return node.as_dict
49             if isinstance(node, (list, tuple)):
50                 return [walk_ctx(x) for x in node]
51             if isinstance(node, HandledException):
52                 return str(node)
53             return node
54         library: dict[str, dict[str | int, object]] = {}
55         for k, v in ctx.items():
56             ctx[k] = walk_ctx(v)
57         ctx['_library'] = library
58         return json_dumps(ctx)
59
60     def render(self, ctx: dict[str, object], tmpl_name: str = '') -> str:
61         """Render ctx according to self._render_mode.."""
62         tmpl_name = f'{tmpl_name}.{self._render_mode}'
63         if 'html' == self._render_mode:
64             template = self._jinja.get_template(tmpl_name)
65             return template.render(ctx)
66         return self.__class__.ctx_to_json(ctx)
67
68
69 class InputsParser:
70     """Wrapper for validating and retrieving dict-like HTTP inputs."""
71
72     def __init__(self, dict_: dict[str, list[str]],
73                  strictness: bool = True) -> None:
74         self.inputs = dict_
75         self.strict = strictness
76
77     def get_str(self, key: str, default: str = '',
78                 ignore_strict: bool = False) -> str:
79         """Retrieve single/first string value of key, or default."""
80         if key not in self.inputs.keys() or 0 == len(self.inputs[key]):
81             if self.strict and not ignore_strict:
82                 raise BadFormatException(f'no value found for key {key}')
83             return default
84         return self.inputs[key][0]
85
86     def get_first_strings_starting(self, prefix: str) -> dict[str, str]:
87         """Retrieve dict of (first) strings at key starting with prefix."""
88         ret = {}
89         for key in [k for k in self.inputs.keys() if k.startswith(prefix)]:
90             ret[key] = self.inputs[key][0]
91         return ret
92
93     def get_int(self, key: str) -> int:
94         """Retrieve single/first value of key as int, error if empty."""
95         val = self.get_int_or_none(key)
96         if val is None:
97             raise BadFormatException(f'unexpected empty value for: {key}')
98         return val
99
100     def get_int_or_none(self, key: str) -> int | None:
101         """Retrieve single/first value of key as int, return None if empty."""
102         val = self.get_str(key, ignore_strict=True)
103         if val == '':
104             return None
105         try:
106             return int(val)
107         except ValueError as e:
108             msg = f'cannot int form field value for key {key}: {val}'
109             raise BadFormatException(msg) from e
110
111     def get_float(self, key: str) -> float:
112         """Retrieve float value of key from self.postvars."""
113         val = self.get_str(key)
114         try:
115             return float(val)
116         except ValueError as e:
117             msg = f'cannot float form field value for key {key}: {val}'
118             raise BadFormatException(msg) from e
119
120     def get_all_str(self, key: str) -> list[str]:
121         """Retrieve list of string values at key."""
122         if key not in self.inputs.keys():
123             return []
124         return self.inputs[key]
125
126     def get_all_int(self, key: str) -> list[int]:
127         """Retrieve list of int values at key."""
128         all_str = self.get_all_str(key)
129         try:
130             return [int(s) for s in all_str if len(s) > 0]
131         except ValueError as e:
132             msg = f'cannot int a form field value for key {key} in: {all_str}'
133             raise BadFormatException(msg) from e
134
135
136 class TaskHandler(BaseHTTPRequestHandler):
137     """Handles single HTTP request."""
138     # pylint: disable=too-many-public-methods
139     server: TaskServer
140     conn: DatabaseConnection
141     _site: str
142     _form_data: InputsParser
143     _params: InputsParser
144
145     def _send_page(self,
146                    ctx: dict[str, Any],
147                    tmpl_name: str,
148                    code: int = 200
149                    ) -> None:
150         """Send ctx as proper HTTP response."""
151         body = self.server.render(ctx, tmpl_name)
152         self.send_response(code)
153         for header_tuple in self.server.headers:
154             self.send_header(*header_tuple)
155         self.end_headers()
156         self.wfile.write(bytes(body, 'utf-8'))
157
158     @staticmethod
159     def _request_wrapper(http_method: str, not_found_msg: str
160                          ) -> Callable[..., Callable[[TaskHandler], None]]:
161         def decorator(f: Callable[..., str | None]
162                       ) -> Callable[[TaskHandler], None]:
163             def wrapper(self: TaskHandler) -> None:
164                 # pylint: disable=protected-access
165                 # (because pylint here fails to detect the use of wrapper as a
166                 # method to self with respective access privileges)
167                 try:
168                     self.conn = DatabaseConnection(self.server.db)
169                     parsed_url = urlparse(self.path)
170                     self._site = path_split(parsed_url.path)[1]
171                     params = parse_qs(parsed_url.query, strict_parsing=True)
172                     self._params = InputsParser(params, False)
173                     handler_name = f'do_{http_method}_{self._site}'
174                     if hasattr(self, handler_name):
175                         handler = getattr(self, handler_name)
176                         redir_target = f(self, handler)
177                         if redir_target:
178                             self.send_response(302)
179                             self.send_header('Location', redir_target)
180                             self.end_headers()
181                     else:
182                         msg = f'{not_found_msg}: {self._site}'
183                         raise NotFoundException(msg)
184                 except HandledException as error:
185                     for cls in (Day, Todo, Condition, Process, ProcessStep):
186                         assert hasattr(cls, 'empty_cache')
187                         cls.empty_cache()
188                     ctx = {'msg': error}
189                     self._send_page(ctx, 'msg', error.http_code)
190                 finally:
191                     self.conn.close()
192             return wrapper
193         return decorator
194
195     @_request_wrapper('GET', 'Unknown page')
196     def do_GET(self, handler: Callable[[], str | dict[str, object]]
197                ) -> str | None:
198         """Render page with result of handler, or redirect if result is str."""
199         tmpl_name = f'{self._site}'
200         ctx_or_redir_target = handler()
201         if isinstance(ctx_or_redir_target, str):
202             return ctx_or_redir_target
203         self._send_page(ctx_or_redir_target, tmpl_name)
204         return None
205
206     @_request_wrapper('POST', 'Unknown POST target')
207     def do_POST(self, handler: Callable[[], str]) -> str:
208         """Handle POST with handler, prepare redirection to result."""
209         length = int(self.headers['content-length'])
210         postvars = parse_qs(self.rfile.read(length).decode(),
211                             keep_blank_values=True, strict_parsing=True)
212         self._form_data = InputsParser(postvars)
213         redir_target = handler()
214         self.conn.commit()
215         return redir_target
216
217     # GET handlers
218
219     def do_GET_(self) -> str:
220         """Return redirect target on GET /."""
221         return '/day'
222
223     def _do_GET_calendar(self) -> dict[str, object]:
224         """Show Days from ?start= to ?end=.
225
226         Both .do_GET_calendar and .do_GET_calendar_txt refer to this to do the
227         same, the only difference being the HTML template they are rendered to,
228         which .do_GET selects from their method name.
229         """
230         start = self._params.get_str('start')
231         end = self._params.get_str('end')
232         if not end:
233             end = date_in_n_days(366)
234         ret = Day.by_date_range_with_limits(self.conn, (start, end), 'id')
235         days, start, end = ret
236         days = Day.with_filled_gaps(days, start, end)
237         today = date_in_n_days(0)
238         return {'start': start, 'end': end, 'days': days, 'today': today}
239
240     def do_GET_calendar(self) -> dict[str, object]:
241         """Show Days from ?start= to ?end= – normal view."""
242         return self._do_GET_calendar()
243
244     def do_GET_calendar_txt(self) -> dict[str, object]:
245         """Show Days from ?start= to ?end= – minimalist view."""
246         return self._do_GET_calendar()
247
248     def do_GET_day(self) -> dict[str, object]:
249         """Show single Day of ?date=."""
250         date = self._params.get_str('date', date_in_n_days(0))
251         day = Day.by_id_or_create(self.conn, date)
252         make_type = self._params.get_str('make_type')
253         conditions_present = []
254         enablers_for = {}
255         disablers_for = {}
256         for todo in day.todos:
257             for condition in todo.conditions + todo.blockers:
258                 if condition not in conditions_present:
259                     conditions_present += [condition]
260                     enablers_for[condition.id_] = [p for p in
261                                                    Process.all(self.conn)
262                                                    if condition in p.enables]
263                     disablers_for[condition.id_] = [p for p in
264                                                     Process.all(self.conn)
265                                                     if condition in p.disables]
266         seen_todos: set[int] = set()
267         top_nodes = [t.get_step_tree(seen_todos)
268                      for t in day.todos if not t.parents]
269         return {'day': day,
270                 'top_nodes': top_nodes,
271                 'make_type': make_type,
272                 'enablers_for': enablers_for,
273                 'disablers_for': disablers_for,
274                 'conditions_present': conditions_present,
275                 'processes': Process.all(self.conn)}
276
277     def do_GET_todo(self) -> dict[str, object]:
278         """Show single Todo of ?id=."""
279
280         @dataclass
281         class TodoStepsNode:
282             """Collect what's useful for Todo steps tree display."""
283             id_: int
284             todo: Todo | None
285             process: Process | None
286             children: list[TodoStepsNode]  # pylint: disable=undefined-variable
287             fillable: bool = False
288
289         def walk_process_steps(id_: int,
290                                process_step_nodes: list[ProcessStepsNode],
291                                steps_nodes: list[TodoStepsNode]) -> None:
292             for process_step_node in process_step_nodes:
293                 id_ += 1
294                 node = TodoStepsNode(id_, None, process_step_node.process, [])
295                 steps_nodes += [node]
296                 walk_process_steps(id_, list(process_step_node.steps.values()),
297                                    node.children)
298
299         def walk_todo_steps(id_: int, todos: list[Todo],
300                             steps_nodes: list[TodoStepsNode]) -> None:
301             for todo in todos:
302                 matched = False
303                 for match in [item for item in steps_nodes
304                               if item.process
305                               and item.process == todo.process]:
306                     match.todo = todo
307                     matched = True
308                     for child in match.children:
309                         child.fillable = True
310                     walk_todo_steps(id_, todo.children, match.children)
311                 if not matched:
312                     id_ += 1
313                     node = TodoStepsNode(id_, todo, None, [])
314                     steps_nodes += [node]
315                     walk_todo_steps(id_, todo.children, node.children)
316
317         def collect_adoptables_keys(steps_nodes: list[TodoStepsNode]
318                                     ) -> set[int]:
319             ids = set()
320             for node in steps_nodes:
321                 if not node.todo:
322                     assert isinstance(node.process, Process)
323                     assert isinstance(node.process.id_, int)
324                     ids.add(node.process.id_)
325                 ids = ids | collect_adoptables_keys(node.children)
326             return ids
327
328         id_ = self._params.get_int('id')
329         todo = Todo.by_id(self.conn, id_)
330         todo_steps = [step.todo for step in todo.get_step_tree(set()).children]
331         process_tree = todo.process.get_steps(self.conn, None)
332         steps_todo_to_process: list[TodoStepsNode] = []
333         walk_process_steps(0, list(process_tree.values()),
334                            steps_todo_to_process)
335         for steps_node in steps_todo_to_process:
336             steps_node.fillable = True
337         walk_todo_steps(len(steps_todo_to_process), todo_steps,
338                         steps_todo_to_process)
339         adoptables: dict[int, list[Todo]] = {}
340         any_adoptables = [Todo.by_id(self.conn, t.id_)
341                           for t in Todo.by_date(self.conn, todo.date)
342                           if t.id_ is not None
343                           and t != todo]
344         for id_ in collect_adoptables_keys(steps_todo_to_process):
345             adoptables[id_] = [t for t in any_adoptables
346                                if t.process.id_ == id_]
347         return {'todo': todo, 'steps_todo_to_process': steps_todo_to_process,
348                 'adoption_candidates_for': adoptables,
349                 'process_candidates': Process.all(self.conn),
350                 'todo_candidates': any_adoptables,
351                 'condition_candidates': Condition.all(self.conn)}
352
353     def do_GET_todos(self) -> dict[str, object]:
354         """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern"""
355         sort_by = self._params.get_str('sort_by')
356         start = self._params.get_str('start')
357         end = self._params.get_str('end')
358         process_id = self._params.get_int_or_none('process_id')
359         comment_pattern = self._params.get_str('comment_pattern')
360         todos = []
361         ret = Todo.by_date_range_with_limits(self.conn, (start, end))
362         todos_by_date_range, start, end = ret
363         todos = [t for t in todos_by_date_range
364                  if comment_pattern in t.comment
365                  and ((not process_id) or t.process.id_ == process_id)]
366         if sort_by == 'doneness':
367             todos.sort(key=lambda t: t.is_done)
368         elif sort_by == '-doneness':
369             todos.sort(key=lambda t: t.is_done, reverse=True)
370         elif sort_by == 'title':
371             todos.sort(key=lambda t: t.title_then)
372         elif sort_by == '-title':
373             todos.sort(key=lambda t: t.title_then, reverse=True)
374         elif sort_by == 'comment':
375             todos.sort(key=lambda t: t.comment)
376         elif sort_by == '-comment':
377             todos.sort(key=lambda t: t.comment, reverse=True)
378         elif sort_by == '-date':
379             todos.sort(key=lambda t: t.date, reverse=True)
380         else:
381             todos.sort(key=lambda t: t.date)
382             sort_by = 'title'
383         return {'start': start, 'end': end, 'process_id': process_id,
384                 'comment_pattern': comment_pattern, 'todos': todos,
385                 'all_processes': Process.all(self.conn), 'sort_by': sort_by}
386
387     def do_GET_conditions(self) -> dict[str, object]:
388         """Show all Conditions."""
389         pattern = self._params.get_str('pattern')
390         conditions = Condition.matching(self.conn, pattern)
391         sort_by = self._params.get_str('sort_by')
392         if sort_by == 'is_active':
393             conditions.sort(key=lambda c: c.is_active)
394         elif sort_by == '-is_active':
395             conditions.sort(key=lambda c: c.is_active, reverse=True)
396         elif sort_by == '-title':
397             conditions.sort(key=lambda c: c.title.newest, reverse=True)
398         else:
399             conditions.sort(key=lambda c: c.title.newest)
400             sort_by = 'title'
401         return {'conditions': conditions,
402                 'sort_by': sort_by,
403                 'pattern': pattern}
404
405     def do_GET_condition(self) -> dict[str, object]:
406         """Show Condition of ?id=."""
407         id_ = self._params.get_int_or_none('id')
408         c = Condition.by_id_or_create(self.conn, id_)
409         ps = Process.all(self.conn)
410         return {'condition': c, 'is_new': c.id_ is None,
411                 'enabled_processes': [p for p in ps if c in p.conditions],
412                 'disabled_processes': [p for p in ps if c in p.blockers],
413                 'enabling_processes': [p for p in ps if c in p.enables],
414                 'disabling_processes': [p for p in ps if c in p.disables]}
415
416     def do_GET_condition_titles(self) -> dict[str, object]:
417         """Show title history of Condition of ?id=."""
418         id_ = self._params.get_int('id')
419         condition = Condition.by_id(self.conn, id_)
420         return {'condition': condition}
421
422     def do_GET_condition_descriptions(self) -> dict[str, object]:
423         """Show description historys of Condition of ?id=."""
424         id_ = self._params.get_int('id')
425         condition = Condition.by_id(self.conn, id_)
426         return {'condition': condition}
427
428     def do_GET_process(self) -> dict[str, object]:
429         """Show Process of ?id=."""
430         id_ = self._params.get_int_or_none('id')
431         process = Process.by_id_or_create(self.conn, id_)
432         title_64 = self._params.get_str('title_b64')
433         if title_64:
434             title = b64decode(title_64.encode()).decode()
435             process.title.set(title)
436         owners = process.used_as_step_by(self.conn)
437         for step_id in self._params.get_all_int('step_to'):
438             owners += [Process.by_id(self.conn, step_id)]
439         preset_top_step = None
440         for process_id in self._params.get_all_int('has_step'):
441             preset_top_step = process_id
442         return {'process': process, 'is_new': process.id_ is None,
443                 'preset_top_step': preset_top_step,
444                 'steps': process.get_steps(self.conn), 'owners': owners,
445                 'n_todos': len(Todo.by_process_id(self.conn, process.id_)),
446                 'process_candidates': Process.all(self.conn),
447                 'condition_candidates': Condition.all(self.conn)}
448
449     def do_GET_process_titles(self) -> dict[str, object]:
450         """Show title history of Process of ?id=."""
451         id_ = self._params.get_int('id')
452         process = Process.by_id(self.conn, id_)
453         return {'process': process}
454
455     def do_GET_process_descriptions(self) -> dict[str, object]:
456         """Show description historys of Process of ?id=."""
457         id_ = self._params.get_int('id')
458         process = Process.by_id(self.conn, id_)
459         return {'process': process}
460
461     def do_GET_process_efforts(self) -> dict[str, object]:
462         """Show default effort history of Process of ?id=."""
463         id_ = self._params.get_int('id')
464         process = Process.by_id(self.conn, id_)
465         return {'process': process}
466
467     def do_GET_processes(self) -> dict[str, object]:
468         """Show all Processes."""
469         pattern = self._params.get_str('pattern')
470         processes = Process.matching(self.conn, pattern)
471         sort_by = self._params.get_str('sort_by')
472         if sort_by == 'steps':
473             processes.sort(key=lambda p: len(p.explicit_steps))
474         elif sort_by == '-steps':
475             processes.sort(key=lambda p: len(p.explicit_steps), reverse=True)
476         elif sort_by == 'owners':
477             processes.sort(key=lambda p: p.n_owners or 0)
478         elif sort_by == '-owners':
479             processes.sort(key=lambda p: p.n_owners or 0, reverse=True)
480         elif sort_by == 'effort':
481             processes.sort(key=lambda p: p.effort.newest)
482         elif sort_by == '-effort':
483             processes.sort(key=lambda p: p.effort.newest, reverse=True)
484         elif sort_by == '-title':
485             processes.sort(key=lambda p: p.title.newest, reverse=True)
486         else:
487             processes.sort(key=lambda p: p.title.newest)
488             sort_by = 'title'
489         return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern}
490
491     # POST handlers
492
493     def _change_versioned_timestamps(self, cls: Any, attr_name: str) -> str:
494         """Update history timestamps for VersionedAttribute."""
495         id_ = self._params.get_int_or_none('id')
496         item = cls.by_id(self.conn, id_)
497         attr = getattr(item, attr_name)
498         for k, v in self._form_data.get_first_strings_starting('at:').items():
499             old = k[3:]
500             if old[19:] != v:
501                 attr.reset_timestamp(old, f'{v}.0')
502         attr.save(self.conn)
503         cls_name = cls.__name__.lower()
504         return f'/{cls_name}_{attr_name}s?id={item.id_}'
505
506     def do_POST_day(self) -> str:
507         """Update or insert Day of date and Todos mapped to it."""
508         date = self._params.get_str('date')
509         day = Day.by_id_or_create(self.conn, date)
510         day.comment = self._form_data.get_str('day_comment')
511         day.save(self.conn)
512         make_type = self._form_data.get_str('make_type')
513         for process_id in sorted(self._form_data.get_all_int('new_todo')):
514             if 'empty' == make_type:
515                 process = Process.by_id(self.conn, process_id)
516                 todo = Todo(None, process, False, date)
517                 todo.save(self.conn)
518             else:
519                 Todo.create_with_children(self.conn, process_id, date)
520         done_ids = self._form_data.get_all_int('done')
521         comments = self._form_data.get_all_str('comment')
522         efforts = self._form_data.get_all_str('effort')
523         for i, todo_id in enumerate(self._form_data.get_all_int('todo_id')):
524             todo = Todo.by_id(self.conn, todo_id)
525             todo.is_done = todo_id in done_ids
526             if len(comments) > 0:
527                 todo.comment = comments[i]
528             if len(efforts) > 0:
529                 todo.effort = float(efforts[i]) if efforts[i] else None
530             todo.save(self.conn)
531         return f'/day?date={date}&make_type={make_type}'
532
533     def do_POST_todo(self) -> str:
534         """Update Todo and its children."""
535         # pylint: disable=too-many-locals
536         # pylint: disable=too-many-branches
537         id_ = self._params.get_int('id')
538         for _ in self._form_data.get_all_str('delete'):
539             todo = Todo .by_id(self.conn, id_)
540             todo.remove(self.conn)
541             return '/'
542         todo = Todo.by_id(self.conn, id_)
543         adopted_child_ids = self._form_data.get_all_int('adopt')
544         processes_to_make_full = self._form_data.get_all_int('make_full')
545         processes_to_make_empty = self._form_data.get_all_int('make_empty')
546         fill_fors = self._form_data.get_first_strings_starting('fill_for_')
547         for v in fill_fors.values():
548             if v.startswith('make_empty_'):
549                 processes_to_make_empty += [int(v[11:])]
550             elif v.startswith('make_full_'):
551                 processes_to_make_full += [int(v[10:])]
552             elif v != 'ignore':
553                 adopted_child_ids += [int(v)]
554         to_remove = []
555         for child in todo.children:
556             assert isinstance(child.id_, int)
557             if child.id_ not in adopted_child_ids:
558                 to_remove += [child.id_]
559         for id_ in to_remove:
560             child = Todo.by_id(self.conn, id_)
561             todo.remove_child(child)
562         for child_id in adopted_child_ids:
563             if child_id in [c.id_ for c in todo.children]:
564                 continue
565             child = Todo.by_id(self.conn, child_id)
566             todo.add_child(child)
567         for process_id in processes_to_make_empty:
568             process = Process.by_id(self.conn, process_id)
569             made = Todo(None, process, False, todo.date)
570             made.save(self.conn)
571             todo.add_child(made)
572         for process_id in processes_to_make_full:
573             made = Todo.create_with_children(self.conn, process_id, todo.date)
574             todo.add_child(made)
575         effort = self._form_data.get_str('effort', ignore_strict=True)
576         todo.effort = float(effort) if effort else None
577         todo.set_conditions(self.conn,
578                             self._form_data.get_all_int('condition'))
579         todo.set_blockers(self.conn, self._form_data.get_all_int('blocker'))
580         todo.set_enables(self.conn, self._form_data.get_all_int('enables'))
581         todo.set_disables(self.conn, self._form_data.get_all_int('disables'))
582         todo.is_done = len(self._form_data.get_all_str('done')) > 0
583         todo.calendarize = len(self._form_data.get_all_str('calendarize')) > 0
584         todo.comment = self._form_data.get_str('comment', ignore_strict=True)
585         todo.save(self.conn)
586         return f'/todo?id={todo.id_}'
587
588     def do_POST_process_descriptions(self) -> str:
589         """Update history timestamps for Process.description."""
590         return self._change_versioned_timestamps(Process, 'description')
591
592     def do_POST_process_efforts(self) -> str:
593         """Update history timestamps for Process.effort."""
594         return self._change_versioned_timestamps(Process, 'effort')
595
596     def do_POST_process_titles(self) -> str:
597         """Update history timestamps for Process.title."""
598         return self._change_versioned_timestamps(Process, 'title')
599
600     def do_POST_process(self) -> str:
601         """Update or insert Process of ?id= and fields defined in postvars."""
602         # pylint: disable=too-many-branches
603         id_ = self._params.get_int_or_none('id')
604         for _ in self._form_data.get_all_str('delete'):
605             if id_ is None:
606                 raise NotFoundException('trying to delete non-saved Process')
607             process = Process.by_id(self.conn, id_)
608             process.remove(self.conn)
609             return '/processes'
610         process = Process.by_id_or_create(self.conn, id_)
611         process.title.set(self._form_data.get_str('title'))
612         process.description.set(self._form_data.get_str('description'))
613         process.effort.set(self._form_data.get_float('effort'))
614         process.set_conditions(self.conn,
615                                self._form_data.get_all_int('condition'))
616         process.set_blockers(self.conn, self._form_data.get_all_int('blocker'))
617         process.set_enables(self.conn, self._form_data.get_all_int('enables'))
618         process.set_disables(self.conn,
619                              self._form_data.get_all_int('disables'))
620         process.calendarize = self._form_data.get_all_str('calendarize') != []
621         process.save(self.conn)
622         assert isinstance(process.id_, int)
623         steps: list[ProcessStep] = []
624         for step_id in self._form_data.get_all_int('keep_step'):
625             if step_id not in self._form_data.get_all_int('steps'):
626                 raise BadFormatException('trying to keep unknown step')
627         for step_id in self._form_data.get_all_int('steps'):
628             if step_id not in self._form_data.get_all_int('keep_step'):
629                 continue
630             step_process_id = self._form_data.get_int(
631                     f'step_{step_id}_process_id')
632             parent_id = self._form_data.get_int_or_none(
633                     f'step_{step_id}_parent_id')
634             steps += [ProcessStep(step_id, process.id_, step_process_id,
635                                   parent_id)]
636         for step_id in self._form_data.get_all_int('steps'):
637             for step_process_id in self._form_data.get_all_int(
638                     f'new_step_to_{step_id}'):
639                 steps += [ProcessStep(None, process.id_, step_process_id,
640                                       step_id)]
641         new_step_title = None
642         for step_identifier in self._form_data.get_all_str('new_top_step'):
643             try:
644                 step_process_id = int(step_identifier)
645                 steps += [ProcessStep(None, process.id_, step_process_id,
646                                       None)]
647             except ValueError:
648                 new_step_title = step_identifier
649         process.set_steps(self.conn, steps)
650         process.set_step_suppressions(self.conn,
651                                       self._form_data.
652                                       get_all_int('suppresses'))
653         owners_to_set = []
654         new_owner_title = None
655         for owner_identifier in self._form_data.get_all_str('step_of'):
656             try:
657                 owners_to_set += [int(owner_identifier)]
658             except ValueError:
659                 new_owner_title = owner_identifier
660         process.set_owners(self.conn, owners_to_set)
661         params = f'id={process.id_}'
662         if new_step_title:
663             title_b64_encoded = b64encode(new_step_title.encode()).decode()
664             params = f'step_to={process.id_}&title_b64={title_b64_encoded}'
665         elif new_owner_title:
666             title_b64_encoded = b64encode(new_owner_title.encode()).decode()
667             params = f'has_step={process.id_}&title_b64={title_b64_encoded}'
668         process.save(self.conn)
669         return f'/process?{params}'
670
671     def do_POST_condition_descriptions(self) -> str:
672         """Update history timestamps for Condition.description."""
673         return self._change_versioned_timestamps(Condition, 'description')
674
675     def do_POST_condition_titles(self) -> str:
676         """Update history timestamps for Condition.title."""
677         return self._change_versioned_timestamps(Condition, 'title')
678
679     def do_POST_condition(self) -> str:
680         """Update/insert Condition of ?id= and fields defined in postvars."""
681         id_ = self._params.get_int_or_none('id')
682         for _ in self._form_data.get_all_str('delete'):
683             if id_ is None:
684                 raise NotFoundException('trying to delete non-saved Condition')
685             condition = Condition.by_id_or_create(self.conn, id_)
686             condition.remove(self.conn)
687             return '/conditions'
688         condition = Condition.by_id_or_create(self.conn, id_)
689         condition.is_active = self._form_data.get_str('is_active') == 'True'
690         condition.title.set(self._form_data.get_str('title'))
691         condition.description.set(self._form_data.get_str('description'))
692         condition.save(self.conn)
693         return f'/condition?id={condition.id_}'