home · contact · privacy
Add expression of summations of efforts in Day display.
[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(366)
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         total_effort = 0
148         for todo in todays_todos:
149             total_effort += todo.performed_effort
150         conditions_present = []
151         enablers_for = {}
152         disablers_for = {}
153         for todo in todays_todos:
154             for condition in todo.conditions + todo.blockers:
155                 if condition not in conditions_present:
156                     conditions_present += [condition]
157                     enablers_for[condition.id_] = [p for p in
158                                                    Process.all(self.conn)
159                                                    if condition in p.enables]
160                     disablers_for[condition.id_] = [p for p in
161                                                     Process.all(self.conn)
162                                                     if condition in p.disables]
163         seen_todos: set[int] = set()
164         top_nodes = [t.get_step_tree(seen_todos)
165                      for t in todays_todos if not t.parents]
166         return {'day': Day.by_id(self.conn, date, create=True),
167                 'total_effort': total_effort,
168                 'top_nodes': top_nodes,
169                 'enablers_for': enablers_for,
170                 'disablers_for': disablers_for,
171                 'conditions_present': conditions_present,
172                 'processes': Process.all(self.conn)}
173
174     def do_GET_todo(self) -> dict[str, object]:
175         """Show single Todo of ?id=."""
176         id_ = self.params.get_int('id')
177         todo = Todo.by_id(self.conn, id_)
178         return {'todo': todo,
179                 'process_candidates': Process.all(self.conn),
180                 'todo_candidates': Todo.by_date(self.conn, todo.date),
181                 'condition_candidates': Condition.all(self.conn)}
182
183     def do_GET_todos(self) -> dict[str, object]:
184         """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern"""
185         sort_by = self.params.get_str('sort_by')
186         start = self.params.get_str('start')
187         end = self.params.get_str('end')
188         process_id = self.params.get_int_or_none('process_id')
189         comment_pattern = self.params.get_str('comment_pattern')
190         todos = []
191         ret = Todo.by_date_range_with_limits(self.conn, (start, end))
192         todos_by_date_range, start, end = ret
193         todos = [t for t in todos_by_date_range
194                  if comment_pattern in t.comment
195                  and ((not process_id) or t.process.id_ == process_id)]
196         if sort_by == 'doneness':
197             todos.sort(key=lambda t: t.is_done)
198         elif sort_by == '-doneness':
199             todos.sort(key=lambda t: t.is_done, reverse=True)
200         elif sort_by == 'title':
201             todos.sort(key=lambda t: t.title_then)
202         elif sort_by == '-title':
203             todos.sort(key=lambda t: t.title_then, reverse=True)
204         elif sort_by == 'comment':
205             todos.sort(key=lambda t: t.comment)
206         elif sort_by == '-comment':
207             todos.sort(key=lambda t: t.comment, reverse=True)
208         elif sort_by == '-date':
209             todos.sort(key=lambda t: t.date, reverse=True)
210         else:
211             todos.sort(key=lambda t: t.date)
212         return {'start': start, 'end': end, 'process_id': process_id,
213                 'comment_pattern': comment_pattern, 'todos': todos,
214                 'all_processes': Process.all(self.conn), 'sort_by': sort_by}
215
216     def do_GET_conditions(self) -> dict[str, object]:
217         """Show all Conditions."""
218         pattern = self.params.get_str('pattern')
219         conditions = Condition.matching(self.conn, pattern)
220         sort_by = self.params.get_str('sort_by')
221         if sort_by == 'is_active':
222             conditions.sort(key=lambda c: c.is_active)
223         elif sort_by == '-is_active':
224             conditions.sort(key=lambda c: c.is_active, reverse=True)
225         elif sort_by == '-title':
226             conditions.sort(key=lambda c: c.title.newest, reverse=True)
227         else:
228             conditions.sort(key=lambda c: c.title.newest)
229         return {'conditions': conditions,
230                 'sort_by': sort_by,
231                 'pattern': pattern}
232
233     def do_GET_condition(self) -> dict[str, object]:
234         """Show Condition of ?id=."""
235         id_ = self.params.get_int_or_none('id')
236         c = Condition.by_id(self.conn, id_, create=True)
237         ps = Process.all(self.conn)
238         return {'condition': c, 'is_new': c.id_ is None,
239                 'enabled_processes': [p for p in ps if c in p.conditions],
240                 'disabled_processes': [p for p in ps if c in p.blockers],
241                 'enabling_processes': [p for p in ps if c in p.enables],
242                 'disabling_processes': [p for p in ps if c in p.disables]}
243
244     def do_GET_condition_titles(self) -> dict[str, object]:
245         """Show title history of Condition of ?id=."""
246         id_ = self.params.get_int_or_none('id')
247         condition = Condition.by_id(self.conn, id_)
248         return {'condition': condition}
249
250     def do_GET_condition_descriptions(self) -> dict[str, object]:
251         """Show description historys of Condition of ?id=."""
252         id_ = self.params.get_int_or_none('id')
253         condition = Condition.by_id(self.conn, id_)
254         return {'condition': condition}
255
256     def do_GET_process(self) -> dict[str, object]:
257         """Show Process of ?id=."""
258         id_ = self.params.get_int_or_none('id')
259         process = Process.by_id(self.conn, id_, create=True)
260         title_64 = self.params.get_str('title_b64')
261         if title_64:
262             title = b64decode(title_64.encode()).decode()
263             process.title.set(title)
264         owners = process.used_as_step_by(self.conn)
265         for step_id in self.params.get_all_int('step_to'):
266             owners += [Process.by_id(self.conn, step_id)]
267         preset_top_step = None
268         for process_id in self.params.get_all_int('has_step'):
269             preset_top_step = process_id
270         return {'process': process, 'is_new': process.id_ is None,
271                 'preset_top_step': preset_top_step,
272                 'steps': process.get_steps(self.conn), 'owners': owners,
273                 'n_todos': len(Todo.by_process_id(self.conn, process.id_)),
274                 'process_candidates': Process.all(self.conn),
275                 'condition_candidates': Condition.all(self.conn)}
276
277     def do_GET_process_titles(self) -> dict[str, object]:
278         """Show title history of Process of ?id=."""
279         id_ = self.params.get_int_or_none('id')
280         process = Process.by_id(self.conn, id_)
281         return {'process': process}
282
283     def do_GET_process_descriptions(self) -> dict[str, object]:
284         """Show description historys of Process of ?id=."""
285         id_ = self.params.get_int_or_none('id')
286         process = Process.by_id(self.conn, id_)
287         return {'process': process}
288
289     def do_GET_process_efforts(self) -> dict[str, object]:
290         """Show default effort history of Process of ?id=."""
291         id_ = self.params.get_int_or_none('id')
292         process = Process.by_id(self.conn, id_)
293         return {'process': process}
294
295     def do_GET_processes(self) -> dict[str, object]:
296         """Show all Processes."""
297         pattern = self.params.get_str('pattern')
298         processes = Process.matching(self.conn, pattern)
299         sort_by = self.params.get_str('sort_by')
300         if sort_by == 'steps':
301             processes.sort(key=lambda p: len(p.explicit_steps))
302         elif sort_by == '-steps':
303             processes.sort(key=lambda p: len(p.explicit_steps), reverse=True)
304         elif sort_by == 'owners':
305             processes.sort(key=lambda p: p.n_owners or 0)
306         elif sort_by == '-owners':
307             processes.sort(key=lambda p: p.n_owners or 0, reverse=True)
308         elif sort_by == 'effort':
309             processes.sort(key=lambda p: p.effort.newest)
310         elif sort_by == '-effort':
311             processes.sort(key=lambda p: p.effort.newest, reverse=True)
312         elif sort_by == '-title':
313             processes.sort(key=lambda p: p.title.newest, reverse=True)
314         else:
315             processes.sort(key=lambda p: p.title.newest)
316         return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern}
317
318     def do_POST(self) -> None:
319         """Handle any POST request."""
320         # pylint: disable=attribute-defined-outside-init
321         try:
322             self._init_handling()
323             length = int(self.headers['content-length'])
324             postvars = parse_qs(self.rfile.read(length).decode(),
325                                 keep_blank_values=True, strict_parsing=True)
326             self.form_data = InputsParser(postvars)
327             if hasattr(self, f'do_POST_{self.site}'):
328                 redir_target = getattr(self, f'do_POST_{self.site}')()
329                 self.conn.commit()
330             else:
331                 msg = f'Page not known as POST target: /{self.site}'
332                 raise NotFoundException(msg)
333             self._redirect(redir_target)
334         except HandledException as error:
335             self._send_msg(error, code=error.http_code)
336         finally:
337             self.conn.close()
338
339     def do_POST_day(self) -> str:
340         """Update or insert Day of date and Todos mapped to it."""
341         date = self.params.get_str('date')
342         day = Day.by_id(self.conn, date, create=True)
343         day.comment = self.form_data.get_str('day_comment')
344         day.save(self.conn)
345         for process_id in sorted(self.form_data.get_all_int('new_todo')):
346             Todo.create_with_children(self.conn, process_id, date)
347         done_ids = self.form_data.get_all_int('done')
348         comments = self.form_data.get_all_str('comment')
349         efforts = self.form_data.get_all_str('effort')
350         for i, todo_id in enumerate(self.form_data.get_all_int('todo_id')):
351             todo = Todo.by_id(self.conn, todo_id)
352             todo.is_done = todo_id in done_ids
353             if len(comments) > 0:
354                 todo.comment = comments[i]
355             if len(efforts) > 0:
356                 todo.effort = float(efforts[i]) if efforts[i] else None
357             todo.save(self.conn)
358             for condition in todo.enables:
359                 condition.save(self.conn)
360             for condition in todo.disables:
361                 condition.save(self.conn)
362         return f'/day?date={date}'
363
364     def do_POST_todo(self) -> str:
365         """Update Todo and its children."""
366         id_ = self.params.get_int('id')
367         for _ in self.form_data.get_all_str('delete'):
368             todo = Todo .by_id(self.conn, id_)
369             todo.remove(self.conn)
370             return '/'
371         todo = Todo.by_id(self.conn, id_)
372         adopted_child_ids = self.form_data.get_all_int('adopt')
373         for child in todo.children:
374             if child.id_ not in adopted_child_ids:
375                 assert isinstance(child.id_, int)
376                 child = Todo.by_id(self.conn, child.id_)
377                 todo.remove_child(child)
378         for child_id in adopted_child_ids:
379             if child_id in [c.id_ for c in todo.children]:
380                 continue
381             child = Todo.by_id(self.conn, child_id)
382             todo.add_child(child)
383         for process_id in self.form_data.get_all_int('make'):
384             made = Todo.create_with_children(self.conn, process_id, todo.date)
385             todo.add_child(made)
386         effort = self.form_data.get_str('effort', ignore_strict=True)
387         todo.effort = float(effort) if effort else None
388         todo.set_conditions(self.conn, self.form_data.get_all_int('condition'))
389         todo.set_blockers(self.conn, self.form_data.get_all_int('blocker'))
390         todo.set_enables(self.conn, self.form_data.get_all_int('enables'))
391         todo.set_disables(self.conn, self.form_data.get_all_int('disables'))
392         todo.is_done = len(self.form_data.get_all_str('done')) > 0
393         todo.calendarize = len(self.form_data.get_all_str('calendarize')) > 0
394         todo.comment = self.form_data.get_str('comment', ignore_strict=True)
395         todo.save(self.conn)
396         for condition in todo.enables:
397             condition.save(self.conn)
398         for condition in todo.disables:
399             condition.save(self.conn)
400         return f'/todo?id={todo.id_}'
401
402     def _do_POST_versioned_timestamps(self, cls: Any, attr_name: str) -> str:
403         """Update history timestamps for VersionedAttribute."""
404         id_ = self.params.get_int_or_none('id')
405         item = cls.by_id(self.conn, id_)
406         attr = getattr(item, attr_name)
407         for k, v in self.form_data.get_first_strings_starting('at:').items():
408             old = k[3:]
409             if old[19:] != v:
410                 attr.reset_timestamp(old, f'{v}.0')
411         attr.save(self.conn)
412         cls_name = cls.__name__.lower()
413         return f'/{cls_name}_{attr_name}s?id={item.id_}'
414
415     def do_POST_process_descriptions(self) -> str:
416         """Update history timestamps for Process.description."""
417         return self._do_POST_versioned_timestamps(Process, 'description')
418
419     def do_POST_process_efforts(self) -> str:
420         """Update history timestamps for Process.effort."""
421         return self._do_POST_versioned_timestamps(Process, 'effort')
422
423     def do_POST_process_titles(self) -> str:
424         """Update history timestamps for Process.title."""
425         return self._do_POST_versioned_timestamps(Process, 'title')
426
427     def do_POST_process(self) -> str:
428         """Update or insert Process of ?id= and fields defined in postvars."""
429         # pylint: disable=too-many-branches
430         id_ = self.params.get_int_or_none('id')
431         for _ in self.form_data.get_all_str('delete'):
432             process = Process.by_id(self.conn, id_)
433             process.remove(self.conn)
434             return '/processes'
435         process = Process.by_id(self.conn, id_, create=True)
436         process.title.set(self.form_data.get_str('title'))
437         process.description.set(self.form_data.get_str('description'))
438         process.effort.set(self.form_data.get_float('effort'))
439         process.set_conditions(self.conn,
440                                self.form_data.get_all_int('condition'))
441         process.set_blockers(self.conn, self.form_data.get_all_int('blocker'))
442         process.set_enables(self.conn, self.form_data.get_all_int('enables'))
443         process.set_disables(self.conn, self.form_data.get_all_int('disables'))
444         process.calendarize = self.form_data.get_all_str('calendarize') != []
445         process.save(self.conn)
446         assert isinstance(process.id_, int)
447         steps: list[ProcessStep] = []
448         for step_id in self.form_data.get_all_int('keep_step'):
449             if step_id not in self.form_data.get_all_int('steps'):
450                 raise BadFormatException('trying to keep unknown step')
451         for step_id in self.form_data.get_all_int('steps'):
452             if step_id not in self.form_data.get_all_int('keep_step'):
453                 continue
454             step_process_id = self.form_data.get_int(
455                     f'step_{step_id}_process_id')
456             parent_id = self.form_data.get_int_or_none(
457                     f'step_{step_id}_parent_id')
458             steps += [ProcessStep(step_id, process.id_, step_process_id,
459                                   parent_id)]
460         for step_id in self.form_data.get_all_int('steps'):
461             for step_process_id in self.form_data.get_all_int(
462                     f'new_step_to_{step_id}'):
463                 steps += [ProcessStep(None, process.id_, step_process_id,
464                                       step_id)]
465         new_step_title = None
466         for step_identifier in self.form_data.get_all_str('new_top_step'):
467             try:
468                 step_process_id = int(step_identifier)
469                 steps += [ProcessStep(None, process.id_, step_process_id,
470                                       None)]
471             except ValueError:
472                 new_step_title = step_identifier
473         process.uncache()
474         process.set_steps(self.conn, steps)
475         process.set_step_suppressions(self.conn,
476                                       self.form_data.get_all_int('suppresses'))
477         process.save(self.conn)
478         owners_to_set = []
479         new_owner_title = None
480         for owner_identifier in self.form_data.get_all_str('step_of'):
481             try:
482                 owners_to_set += [int(owner_identifier)]
483             except ValueError:
484                 new_owner_title = owner_identifier
485         process.set_owners(self.conn, owners_to_set)
486         params = f'id={process.id_}'
487         if new_step_title:
488             title_b64_encoded = b64encode(new_step_title.encode()).decode()
489             params = f'step_to={process.id_}&title_b64={title_b64_encoded}'
490         elif new_owner_title:
491             title_b64_encoded = b64encode(new_owner_title.encode()).decode()
492             params = f'has_step={process.id_}&title_b64={title_b64_encoded}'
493         return f'/process?{params}'
494
495     def do_POST_condition_descriptions(self) -> str:
496         """Update history timestamps for Condition.description."""
497         return self._do_POST_versioned_timestamps(Condition, 'description')
498
499     def do_POST_condition_titles(self) -> str:
500         """Update history timestamps for Condition.title."""
501         return self._do_POST_versioned_timestamps(Condition, 'title')
502
503     def do_POST_condition(self) -> str:
504         """Update/insert Condition of ?id= and fields defined in postvars."""
505         id_ = self.params.get_int_or_none('id')
506         for _ in self.form_data.get_all_str('delete'):
507             condition = Condition.by_id(self.conn, id_)
508             condition.remove(self.conn)
509             return '/conditions'
510         condition = Condition.by_id(self.conn, id_, create=True)
511         condition.is_active = self.form_data.get_all_str('is_active') != []
512         condition.title.set(self.form_data.get_str('title'))
513         condition.description.set(self.form_data.get_str('description'))
514         condition.save(self.conn)
515         return f'/condition?id={condition.id_}'
516
517     def _init_handling(self) -> None:
518         # pylint: disable=attribute-defined-outside-init
519         self.conn = DatabaseConnection(self.server.db)
520         parsed_url = urlparse(self.path)
521         self.site = path_split(parsed_url.path)[1]
522         params = parse_qs(parsed_url.query, strict_parsing=True)
523         self.params = InputsParser(params, False)
524
525     def _redirect(self, target: str) -> None:
526         self.send_response(302)
527         self.send_header('Location', target)
528         self.end_headers()
529
530     def _send_html(self, html: str, code: int = 200) -> None:
531         """Send HTML as proper HTTP response."""
532         self.send_response(code)
533         self.end_headers()
534         self.wfile.write(bytes(html, 'utf-8'))
535
536     def _send_msg(self, msg: Exception, code: int = 400) -> None:
537         """Send message in HTML formatting as HTTP response."""
538         html = self.server.jinja.get_template('msg.html').render(msg=msg)
539         self._send_html(html, code)