home · contact · privacy
For Processes and Conditions, in edit page title warn if creation NEW.
[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_int(self, key: str) -> int:
49         """Retrieve single/first value of key as int, error if empty."""
50         val = self.get_int_or_none(key)
51         if val is None:
52             raise BadFormatException(f'unexpected empty value for: {key}')
53         return val
54
55     def get_int_or_none(self, key: str) -> int | None:
56         """Retrieve single/first value of key as int, return None if empty."""
57         val = self.get_str(key, ignore_strict=True)
58         if val == '':
59             return None
60         try:
61             return int(val)
62         except ValueError as e:
63             msg = f'cannot int form field value for key {key}: {val}'
64             raise BadFormatException(msg) from e
65
66     def get_float(self, key: str) -> float:
67         """Retrieve float value of key from self.postvars."""
68         val = self.get_str(key)
69         try:
70             return float(val)
71         except ValueError as e:
72             msg = f'cannot float form field value for key {key}: {val}'
73             raise BadFormatException(msg) from e
74
75     def get_all_str(self, key: str) -> list[str]:
76         """Retrieve list of string values at key."""
77         if key not in self.inputs.keys():
78             return []
79         return self.inputs[key]
80
81     def get_all_int(self, key: str) -> list[int]:
82         """Retrieve list of int values at key."""
83         all_str = self.get_all_str(key)
84         try:
85             return [int(s) for s in all_str if len(s) > 0]
86         except ValueError as e:
87             msg = f'cannot int a form field value for key {key} in: {all_str}'
88             raise BadFormatException(msg) from e
89
90
91 class TaskHandler(BaseHTTPRequestHandler):
92     """Handles single HTTP request."""
93     server: TaskServer
94
95     def do_GET(self) -> None:
96         """Handle any GET request."""
97         try:
98             self._init_handling()
99             if hasattr(self, f'do_GET_{self.site}'):
100                 template = f'{self.site}.html'
101                 ctx = getattr(self, f'do_GET_{self.site}')()
102                 html = self.server.jinja.get_template(template).render(**ctx)
103                 self._send_html(html)
104             elif '' == self.site:
105                 self._redirect('/day')
106             else:
107                 raise NotFoundException(f'Unknown page: /{self.site}')
108         except HandledException as error:
109             self._send_msg(error, code=error.http_code)
110         finally:
111             self.conn.close()
112
113     def do_GET_calendar(self) -> dict[str, object]:
114         """Show Days from ?start= to ?end=."""
115         start = self.params.get_str('start')
116         end = self.params.get_str('end')
117         if not end:
118             end = date_in_n_days(60)
119         ret = Day.by_date_range_with_limits(self.conn, (start, end), 'id')
120         days, start, end = ret
121         days = Day.with_filled_gaps(days, start, end)
122         for day in days:
123             day.collect_calendarized_todos(self.conn)
124         today = date_in_n_days(0)
125         return {'start': start, 'end': end, 'days': days, 'today': today}
126
127     def do_GET_day(self) -> dict[str, object]:
128         """Show single Day of ?date=."""
129         date = self.params.get_str('date', date_in_n_days(0))
130         todays_todos = Todo.by_date(self.conn, date)
131         conditions_present = []
132         enablers_for = {}
133         disablers_for = {}
134         for todo in todays_todos:
135             for condition in todo.conditions + todo.blockers:
136                 if condition not in conditions_present:
137                     conditions_present += [condition]
138                     enablers_for[condition.id_] = [p for p in
139                                                    Process.all(self.conn)
140                                                    if condition in p.enables]
141                     disablers_for[condition.id_] = [p for p in
142                                                     Process.all(self.conn)
143                                                     if condition in p.disables]
144         seen_todos: set[int] = set()
145         top_nodes = [t.get_step_tree(seen_todos)
146                      for t in todays_todos if not t.parents]
147         return {'day': Day.by_id(self.conn, date, create=True),
148                 'top_nodes': top_nodes,
149                 'enablers_for': enablers_for,
150                 'disablers_for': disablers_for,
151                 'conditions_present': conditions_present,
152                 'processes': Process.all(self.conn)}
153
154     def do_GET_todo(self) -> dict[str, object]:
155         """Show single Todo of ?id=."""
156         id_ = self.params.get_int('id')
157         todo = Todo.by_id(self.conn, id_)
158         return {'todo': todo,
159                 'todo_candidates': Todo.by_date(self.conn, todo.date),
160                 'condition_candidates': Condition.all(self.conn)}
161
162     def do_GET_todos(self) -> dict[str, object]:
163         """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern"""
164         sort_by = self.params.get_str('sort_by')
165         start = self.params.get_str('start')
166         end = self.params.get_str('end')
167         process_id = self.params.get_int_or_none('process_id')
168         comment_pattern = self.params.get_str('comment_pattern')
169         todos = []
170         ret = Todo.by_date_range_with_limits(self.conn, (start, end))
171         todos_by_date_range, start, end = ret
172         todos = [t for t in todos_by_date_range
173                  if comment_pattern in t.comment
174                  and ((not process_id) or t.process.id_ == process_id)]
175         if sort_by == 'doneness':
176             todos.sort(key=lambda t: t.is_done)
177         elif sort_by == '-doneness':
178             todos.sort(key=lambda t: t.is_done, reverse=True)
179         elif sort_by == 'title':
180             todos.sort(key=lambda t: t.title_then)
181         elif sort_by == '-title':
182             todos.sort(key=lambda t: t.title_then, reverse=True)
183         elif sort_by == 'comment':
184             todos.sort(key=lambda t: t.comment)
185         elif sort_by == '-comment':
186             todos.sort(key=lambda t: t.comment, reverse=True)
187         elif sort_by == '-date':
188             todos.sort(key=lambda t: t.date, reverse=True)
189         else:
190             todos.sort(key=lambda t: t.date)
191         return {'start': start, 'end': end, 'process_id': process_id,
192                 'comment_pattern': comment_pattern, 'todos': todos,
193                 'all_processes': Process.all(self.conn), 'sort_by': sort_by}
194
195     def do_GET_conditions(self) -> dict[str, object]:
196         """Show all Conditions."""
197         pattern = self.params.get_str('pattern')
198         conditions = Condition.matching(self.conn, pattern)
199         sort_by = self.params.get_str('sort_by')
200         if sort_by == 'is_active':
201             conditions.sort(key=lambda c: c.is_active)
202         elif sort_by == '-is_active':
203             conditions.sort(key=lambda c: c.is_active, reverse=True)
204         elif sort_by == '-title':
205             conditions.sort(key=lambda c: c.title.newest, reverse=True)
206         else:
207             conditions.sort(key=lambda c: c.title.newest)
208         return {'conditions': conditions,
209                 'sort_by': sort_by,
210                 'pattern': pattern}
211
212     def do_GET_condition(self) -> dict[str, object]:
213         """Show Condition of ?id=."""
214         id_ = self.params.get_int_or_none('id')
215         c = Condition.by_id(self.conn, id_, create=True)
216         ps = Process.all(self.conn)
217         return {'condition': c, 'is_new': c.id_ is None,
218                 'enabled_processes': [p for p in ps if c in p.conditions],
219                 'disabled_processes': [p for p in ps if c in p.blockers],
220                 'enabling_processes': [p for p in ps if c in p.enables],
221                 'disabling_processes': [p for p in ps if c in p.disables]}
222
223     def do_GET_condition_titles(self) -> dict[str, object]:
224         """Show title history of Condition of ?id=."""
225         id_ = self.params.get_int_or_none('id')
226         condition = Condition.by_id(self.conn, id_)
227         return {'condition': condition}
228
229     def do_GET_condition_descriptions(self) -> dict[str, object]:
230         """Show description historys of Condition of ?id=."""
231         id_ = self.params.get_int_or_none('id')
232         condition = Condition.by_id(self.conn, id_)
233         return {'condition': condition}
234
235     def do_GET_process(self) -> dict[str, object]:
236         """Show Process of ?id=."""
237         id_ = self.params.get_int_or_none('id')
238         process = Process.by_id(self.conn, id_, create=True)
239         title_64 = self.params.get_str('title_b64')
240         if title_64:
241             title = b64decode(title_64.encode()).decode()
242             process.title.set(title)
243         return {'process': process, 'is_new': process.id_ is None,
244                 'steps': process.get_steps(self.conn),
245                 'owners': process.used_as_step_by(self.conn),
246                 'n_todos': len(Todo.by_process_id(self.conn, process.id_)),
247                 'step_candidates': Process.all(self.conn),
248                 'condition_candidates': Condition.all(self.conn)}
249
250     def do_GET_process_titles(self) -> dict[str, object]:
251         """Show title history of Process of ?id=."""
252         id_ = self.params.get_int_or_none('id')
253         process = Process.by_id(self.conn, id_)
254         return {'process': process}
255
256     def do_GET_process_descriptions(self) -> dict[str, object]:
257         """Show description historys of Process of ?id=."""
258         id_ = self.params.get_int_or_none('id')
259         process = Process.by_id(self.conn, id_)
260         return {'process': process}
261
262     def do_GET_process_efforts(self) -> dict[str, object]:
263         """Show default effort history of Process of ?id=."""
264         id_ = self.params.get_int_or_none('id')
265         process = Process.by_id(self.conn, id_)
266         return {'process': process}
267
268     def do_GET_processes(self) -> dict[str, object]:
269         """Show all Processes."""
270         pattern = self.params.get_str('pattern')
271         processes = Process.matching(self.conn, pattern)
272         sort_by = self.params.get_str('sort_by')
273         if sort_by == 'steps':
274             processes.sort(key=lambda p: len(p.explicit_steps))
275         elif sort_by == '-steps':
276             processes.sort(key=lambda p: len(p.explicit_steps), reverse=True)
277         elif sort_by == 'effort':
278             processes.sort(key=lambda p: p.effort.newest)
279         elif sort_by == '-effort':
280             processes.sort(key=lambda p: p.effort.newest, reverse=True)
281         elif sort_by == '-title':
282             processes.sort(key=lambda p: p.title.newest, reverse=True)
283         else:
284             processes.sort(key=lambda p: p.title.newest)
285         return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern}
286
287     def do_POST(self) -> None:
288         """Handle any POST request."""
289         # pylint: disable=attribute-defined-outside-init
290         try:
291             self._init_handling()
292             length = int(self.headers['content-length'])
293             postvars = parse_qs(self.rfile.read(length).decode(),
294                                 keep_blank_values=True, strict_parsing=True)
295             self.form_data = InputsParser(postvars)
296             if hasattr(self, f'do_POST_{self.site}'):
297                 redir_target = getattr(self, f'do_POST_{self.site}')()
298                 self.conn.commit()
299             else:
300                 msg = f'Page not known as POST target: /{self.site}'
301                 raise NotFoundException(msg)
302             self._redirect(redir_target)
303         except HandledException as error:
304             self._send_msg(error, code=error.http_code)
305         finally:
306             self.conn.close()
307
308     def do_POST_day(self) -> str:
309         """Update or insert Day of date and Todos mapped to it."""
310         date = self.params.get_str('date')
311         day = Day.by_id(self.conn, date, create=True)
312         day.comment = self.form_data.get_str('day_comment')
313         day.save(self.conn)
314         for process_id in sorted(self.form_data.get_all_int('new_todo')):
315             Todo.create_with_children(self.conn, process_id, date)
316         done_ids = self.form_data.get_all_int('done')
317         comments = self.form_data.get_all_str('comment')
318         efforts = self.form_data.get_all_str('effort')
319         for i, todo_id in enumerate(self.form_data.get_all_int('todo_id')):
320             todo = Todo.by_id(self.conn, todo_id)
321             todo.is_done = todo_id in done_ids
322             if len(comments) > 0:
323                 todo.comment = comments[i]
324             if len(efforts) > 0:
325                 todo.effort = float(efforts[i]) if efforts[i] else None
326             todo.save(self.conn)
327             for condition in todo.enables:
328                 condition.save(self.conn)
329             for condition in todo.disables:
330                 condition.save(self.conn)
331         return f'/day?date={date}'
332
333     def do_POST_todo(self) -> str:
334         """Update Todo and its children."""
335         id_ = self.params.get_int('id')
336         for _ in self.form_data.get_all_str('delete'):
337             todo = Todo .by_id(self.conn, id_)
338             todo.remove(self.conn)
339             return '/'
340         todo = Todo.by_id(self.conn, id_)
341         adopted_child_ids = self.form_data.get_all_int('adopt')
342         for child in todo.children:
343             if child.id_ not in adopted_child_ids:
344                 assert isinstance(child.id_, int)
345                 child = Todo.by_id(self.conn, child.id_)
346                 todo.remove_child(child)
347         for child_id in adopted_child_ids:
348             if child_id in [c.id_ for c in todo.children]:
349                 continue
350             child = Todo.by_id(self.conn, child_id)
351             todo.add_child(child)
352         effort = self.form_data.get_str('effort', ignore_strict=True)
353         todo.effort = float(effort) if effort else None
354         todo.set_conditions(self.conn, self.form_data.get_all_int('condition'))
355         todo.set_blockers(self.conn, self.form_data.get_all_int('blocker'))
356         todo.set_enables(self.conn, self.form_data.get_all_int('enables'))
357         todo.set_disables(self.conn, self.form_data.get_all_int('disables'))
358         todo.is_done = len(self.form_data.get_all_str('done')) > 0
359         todo.calendarize = len(self.form_data.get_all_str('calendarize')) > 0
360         todo.comment = self.form_data.get_str('comment', ignore_strict=True)
361         todo.save(self.conn)
362         for condition in todo.enables:
363             condition.save(self.conn)
364         for condition in todo.disables:
365             condition.save(self.conn)
366         return f'/todo?id={todo.id_}'
367
368     def do_POST_process(self) -> str:
369         """Update or insert Process of ?id= and fields defined in postvars."""
370         id_ = self.params.get_int_or_none('id')
371         for _ in self.form_data.get_all_str('delete'):
372             process = Process.by_id(self.conn, id_)
373             process.remove(self.conn)
374             return '/processes'
375         process = Process.by_id(self.conn, id_, create=True)
376         process.title.set(self.form_data.get_str('title'))
377         process.description.set(self.form_data.get_str('description'))
378         process.effort.set(self.form_data.get_float('effort'))
379         process.set_conditions(self.conn,
380                                self.form_data.get_all_int('condition'))
381         process.set_blockers(self.conn, self.form_data.get_all_int('blocker'))
382         process.set_enables(self.conn, self.form_data.get_all_int('enables'))
383         process.set_disables(self.conn, self.form_data.get_all_int('disables'))
384         process.calendarize = self.form_data.get_all_str('calendarize') != []
385         process.save(self.conn)
386         assert isinstance(process.id_, int)
387         steps: list[ProcessStep] = []
388         for step_id in self.form_data.get_all_int('keep_step'):
389             if step_id not in self.form_data.get_all_int('steps'):
390                 raise BadFormatException('trying to keep unknown step')
391         for step_id in self.form_data.get_all_int('steps'):
392             if step_id not in self.form_data.get_all_int('keep_step'):
393                 continue
394             step_process_id = self.form_data.get_int(
395                     f'step_{step_id}_process_id')
396             parent_id = self.form_data.get_int_or_none(
397                     f'step_{step_id}_parent_id')
398             steps += [ProcessStep(step_id, process.id_, step_process_id,
399                                   parent_id)]
400         for step_id in self.form_data.get_all_int('steps'):
401             for step_process_id in self.form_data.get_all_int(
402                     f'new_step_to_{step_id}'):
403                 steps += [ProcessStep(None, process.id_, step_process_id,
404                                       step_id)]
405         new_process_title = None
406         for step_identifier in self.form_data.get_all_str('new_top_step'):
407             try:
408                 step_process_id = int(step_identifier)
409                 steps += [ProcessStep(None, process.id_, step_process_id,
410                                       None)]
411             except ValueError:
412                 new_process_title = step_identifier
413         process.uncache()
414         process.set_steps(self.conn, steps)
415         process.set_step_suppressions(self.conn,
416                                       self.form_data.get_all_int('suppresses'))
417         process.save(self.conn)
418         if new_process_title:
419             title_b64_encoded = b64encode(new_process_title.encode()).decode()
420             return f'/process?title_b64={title_b64_encoded}'
421         return f'/process?id={process.id_}'
422
423     def do_POST_condition(self) -> str:
424         """Update/insert Condition of ?id= and fields defined in postvars."""
425         id_ = self.params.get_int_or_none('id')
426         for _ in self.form_data.get_all_str('delete'):
427             condition = Condition.by_id(self.conn, id_)
428             condition.remove(self.conn)
429             return '/conditions'
430         condition = Condition.by_id(self.conn, id_, create=True)
431         condition.is_active = self.form_data.get_all_str('is_active') != []
432         condition.title.set(self.form_data.get_str('title'))
433         condition.description.set(self.form_data.get_str('description'))
434         condition.save(self.conn)
435         return f'/condition?id={condition.id_}'
436
437     def _init_handling(self) -> None:
438         # pylint: disable=attribute-defined-outside-init
439         self.conn = DatabaseConnection(self.server.db)
440         parsed_url = urlparse(self.path)
441         self.site = path_split(parsed_url.path)[1]
442         params = parse_qs(parsed_url.query, strict_parsing=True)
443         self.params = InputsParser(params, False)
444
445     def _redirect(self, target: str) -> None:
446         self.send_response(302)
447         self.send_header('Location', target)
448         self.end_headers()
449
450     def _send_html(self, html: str, code: int = 200) -> None:
451         """Send HTML as proper HTTP response."""
452         self.send_response(code)
453         self.end_headers()
454         self.wfile.write(bytes(html, 'utf-8'))
455
456     def _send_msg(self, msg: Exception, code: int = 400) -> None:
457         """Send message in HTML formatting as HTTP response."""
458         html = self.server.jinja.get_template('msg.html').render(msg=msg)
459         self._send_html(html, code)