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