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