home · contact · privacy
Default calendar date range end to 60 days later.
[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
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         return {'start': start, 'end': end, 'days': days}
124
125     def do_GET_day(self) -> dict[str, object]:
126         """Show single Day of ?date=."""
127         date = self.params.get_str('date', date_in_n_days(0))
128         todays_todos = Todo.by_date(self.conn, date)
129         conditions_present = []
130         enablers_for = {}
131         disablers_for = {}
132         for todo in todays_todos:
133             for condition in todo.conditions + todo.blockers:
134                 if condition not in conditions_present:
135                     conditions_present += [condition]
136                     enablers_for[condition.id_] = [p for p in
137                                                    Process.all(self.conn)
138                                                    if condition in p.enables]
139                     disablers_for[condition.id_] = [p for p in
140                                                     Process.all(self.conn)
141                                                     if condition in p.disables]
142         seen_todos: set[int] = set()
143         top_nodes = [t.get_step_tree(seen_todos)
144                      for t in todays_todos if not t.parents]
145         return {'day': Day.by_id(self.conn, date, create=True),
146                 'top_nodes': top_nodes,
147                 'enablers_for': enablers_for,
148                 'disablers_for': disablers_for,
149                 'conditions_present': conditions_present,
150                 'processes': Process.all(self.conn)}
151
152     def do_GET_todo(self) -> dict[str, object]:
153         """Show single Todo of ?id=."""
154         id_ = self.params.get_int('id')
155         todo = Todo.by_id(self.conn, id_)
156         return {'todo': todo,
157                 'todo_candidates': Todo.by_date(self.conn, todo.date),
158                 'condition_candidates': Condition.all(self.conn)}
159
160     def do_GET_todos(self) -> dict[str, object]:
161         """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern"""
162         sort_by = self.params.get_str('sort_by')
163         start = self.params.get_str('start')
164         end = self.params.get_str('end')
165         process_id = self.params.get_int_or_none('process_id')
166         comment_pattern = self.params.get_str('comment_pattern')
167         todos = []
168         ret = Todo.by_date_range_with_limits(self.conn, (start, end))
169         todos_by_date_range, start, end = ret
170         todos = [t for t in todos_by_date_range
171                  if comment_pattern in t.comment
172                  and ((not process_id) or t.process.id_ == process_id)]
173         if sort_by == 'doneness':
174             todos.sort(key=lambda t: t.is_done)
175         elif sort_by == '-doneness':
176             todos.sort(key=lambda t: t.is_done, reverse=True)
177         elif sort_by == 'process':
178             todos.sort(key=lambda t: t.title_then)
179         elif sort_by == '-process':
180             todos.sort(key=lambda t: t.title_then, reverse=True)
181         elif sort_by == 'comment':
182             todos.sort(key=lambda t: t.comment)
183         elif sort_by == '-comment':
184             todos.sort(key=lambda t: t.comment, reverse=True)
185         elif sort_by == '-date':
186             todos.sort(key=lambda t: t.date, reverse=True)
187         else:
188             todos.sort(key=lambda t: t.date)
189         return {'start': start, 'end': end, 'process_id': process_id,
190                 'comment_pattern': comment_pattern, 'todos': todos,
191                 'all_processes': Process.all(self.conn), 'sort_by': sort_by}
192
193     def do_GET_conditions(self) -> dict[str, object]:
194         """Show all Conditions."""
195         pattern = self.params.get_str('pattern')
196         conditions = Condition.matching(self.conn, pattern)
197         sort_by = self.params.get_str('sort_by')
198         if sort_by == 'is_active':
199             conditions.sort(key=lambda c: c.is_active)
200         elif sort_by == '-is_active':
201             conditions.sort(key=lambda c: c.is_active, reverse=True)
202         elif sort_by == '-title':
203             conditions.sort(key=lambda c: c.title.newest, reverse=True)
204         else:
205             conditions.sort(key=lambda c: c.title.newest)
206         return {'conditions': conditions,
207                 'sort_by': sort_by,
208                 'pattern': pattern}
209
210     def do_GET_condition(self) -> dict[str, object]:
211         """Show Condition of ?id=."""
212         id_ = self.params.get_int_or_none('id')
213         c = Condition.by_id(self.conn, id_, create=True)
214         ps = Process.all(self.conn)
215         return {'condition': c,
216                 'enabled_processes': [p for p in ps if c in p.conditions],
217                 'disabled_processes': [p for p in ps if c in p.blockers],
218                 'enabling_processes': [p for p in ps if c in p.enables],
219                 'disabling_processes': [p for p in ps if c in p.disables]}
220
221     def do_GET_condition_titles(self) -> dict[str, object]:
222         """Show title history of Condition of ?id=."""
223         id_ = self.params.get_int_or_none('id')
224         condition = Condition.by_id(self.conn, id_)
225         return {'condition': condition}
226
227     def do_GET_condition_descriptions(self) -> dict[str, object]:
228         """Show description historys of Condition of ?id=."""
229         id_ = self.params.get_int_or_none('id')
230         condition = Condition.by_id(self.conn, id_)
231         return {'condition': condition}
232
233     def do_GET_process(self) -> dict[str, object]:
234         """Show Process of ?id=."""
235         id_ = self.params.get_int_or_none('id')
236         process = Process.by_id(self.conn, id_, create=True)
237         return {'process': process,
238                 'steps': process.get_steps(self.conn),
239                 'owners': process.used_as_step_by(self.conn),
240                 'n_todos': len(Todo.by_process_id(self.conn, process.id_)),
241                 'step_candidates': Process.all(self.conn),
242                 'condition_candidates': Condition.all(self.conn)}
243
244     def do_GET_process_titles(self) -> dict[str, object]:
245         """Show title history of Process of ?id=."""
246         id_ = self.params.get_int_or_none('id')
247         process = Process.by_id(self.conn, id_)
248         return {'process': process}
249
250     def do_GET_process_descriptions(self) -> dict[str, object]:
251         """Show description historys 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_efforts(self) -> dict[str, object]:
257         """Show default effort history 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_processes(self) -> dict[str, object]:
263         """Show all Processes."""
264         pattern = self.params.get_str('pattern')
265         processes = Process.matching(self.conn, pattern)
266         sort_by = self.params.get_str('sort_by')
267         if sort_by == 'steps':
268             processes.sort(key=lambda p: len(p.explicit_steps))
269         elif sort_by == '-steps':
270             processes.sort(key=lambda p: len(p.explicit_steps), reverse=True)
271         elif sort_by == '-title':
272             processes.sort(key=lambda p: p.title.newest, reverse=True)
273         else:
274             processes.sort(key=lambda p: p.title.newest)
275         return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern}
276
277     def do_POST(self) -> None:
278         """Handle any POST request."""
279         # pylint: disable=attribute-defined-outside-init
280         try:
281             self._init_handling()
282             length = int(self.headers['content-length'])
283             postvars = parse_qs(self.rfile.read(length).decode(),
284                                 keep_blank_values=True, strict_parsing=True)
285             self.form_data = InputsParser(postvars)
286             if hasattr(self, f'do_POST_{self.site}'):
287                 redir_target = getattr(self, f'do_POST_{self.site}')()
288                 self.conn.commit()
289             else:
290                 msg = f'Page not known as POST target: /{self.site}'
291                 raise NotFoundException(msg)
292             self._redirect(redir_target)
293         except HandledException as error:
294             self._send_msg(error, code=error.http_code)
295         finally:
296             self.conn.close()
297
298     def do_POST_day(self) -> str:
299         """Update or insert Day of date and Todos mapped to it."""
300         date = self.params.get_str('date')
301         day = Day.by_id(self.conn, date, create=True)
302         day.comment = self.form_data.get_str('day_comment')
303         day.save(self.conn)
304         Todo.create_with_children(self.conn, date,
305                                   self.form_data.get_all_int('new_todo'))
306         done_ids = self.form_data.get_all_int('done')
307         comments = self.form_data.get_all_str('comment')
308         efforts = self.form_data.get_all_str('effort')
309         for i, todo_id in enumerate(self.form_data.get_all_int('todo_id')):
310             todo = Todo.by_id(self.conn, todo_id)
311             todo.is_done = todo_id in done_ids
312             if len(comments) > 0:
313                 todo.comment = comments[i]
314             if len(efforts) > 0:
315                 todo.effort = float(efforts[i]) if efforts[i] else None
316             todo.save(self.conn)
317             for condition in todo.enables:
318                 condition.save(self.conn)
319             for condition in todo.disables:
320                 condition.save(self.conn)
321         return f'/day?date={date}'
322
323     def do_POST_todo(self) -> str:
324         """Update Todo and its children."""
325         id_ = self.params.get_int('id')
326         for _ in self.form_data.get_all_str('delete'):
327             todo = Todo .by_id(self.conn, id_)
328             todo.remove(self.conn)
329             return '/'
330         todo = Todo.by_id(self.conn, id_)
331         adopted_child_ids = self.form_data.get_all_int('adopt')
332         for child in todo.children:
333             if child.id_ not in adopted_child_ids:
334                 assert isinstance(child.id_, int)
335                 child = Todo.by_id(self.conn, child.id_)
336                 todo.remove_child(child)
337         for child_id in adopted_child_ids:
338             if child_id in [c.id_ for c in todo.children]:
339                 continue
340             child = Todo.by_id(self.conn, child_id)
341             todo.add_child(child)
342         effort = self.form_data.get_str('effort', ignore_strict=True)
343         todo.effort = float(effort) if effort else None
344         todo.set_conditions(self.conn, self.form_data.get_all_int('condition'))
345         todo.set_blockers(self.conn, self.form_data.get_all_int('blocker'))
346         todo.set_enables(self.conn, self.form_data.get_all_int('enables'))
347         todo.set_disables(self.conn, self.form_data.get_all_int('disables'))
348         todo.is_done = len(self.form_data.get_all_str('done')) > 0
349         todo.calendarize = len(self.form_data.get_all_str('calendarize')) > 0
350         todo.comment = self.form_data.get_str('comment', ignore_strict=True)
351         todo.save(self.conn)
352         for condition in todo.enables:
353             condition.save(self.conn)
354         for condition in todo.disables:
355             condition.save(self.conn)
356         return f'/todo?id={todo.id_}'
357
358     def do_POST_process(self) -> str:
359         """Update or insert Process of ?id= and fields defined in postvars."""
360         id_ = self.params.get_int_or_none('id')
361         for _ in self.form_data.get_all_str('delete'):
362             process = Process.by_id(self.conn, id_)
363             process.remove(self.conn)
364             return '/processes'
365         process = Process.by_id(self.conn, id_, create=True)
366         process.title.set(self.form_data.get_str('title'))
367         process.description.set(self.form_data.get_str('description'))
368         process.effort.set(self.form_data.get_float('effort'))
369         process.set_conditions(self.conn,
370                                self.form_data.get_all_int('condition'))
371         process.set_blockers(self.conn, self.form_data.get_all_int('blocker'))
372         process.set_enables(self.conn, self.form_data.get_all_int('enables'))
373         process.set_disables(self.conn, self.form_data.get_all_int('disables'))
374         process.calendarize = self.form_data.get_all_str('calendarize') != []
375         process.save(self.conn)
376         steps: list[tuple[int | None, int, int | None]] = []
377         for step_id in self.form_data.get_all_int('keep_step'):
378             if step_id not in self.form_data.get_all_int('steps'):
379                 raise BadFormatException('trying to keep unknown step')
380         for step_id in self.form_data.get_all_int('steps'):
381             for step_process_id in self.form_data.get_all_int(
382                     f'new_step_to_{step_id}'):
383                 steps += [(None, step_process_id, step_id)]
384             if step_id not in self.form_data.get_all_int('keep_step'):
385                 continue
386             step_process_id = self.form_data.get_int(
387                     f'step_{step_id}_process_id')
388             parent_id = self.form_data.get_int_or_none(
389                     f'step_{step_id}_parent_id')
390             steps += [(step_id, step_process_id, parent_id)]
391         for step_process_id in self.form_data.get_all_int('new_top_step'):
392             steps += [(None, step_process_id, None)]
393         process.set_steps(self.conn, steps)
394         process.save(self.conn)
395         return f'/process?id={process.id_}'
396
397     def do_POST_condition(self) -> str:
398         """Update/insert Condition of ?id= and fields defined in postvars."""
399         id_ = self.params.get_int_or_none('id')
400         for _ in self.form_data.get_all_str('delete'):
401             condition = Condition.by_id(self.conn, id_)
402             condition.remove(self.conn)
403             return '/conditions'
404         condition = Condition.by_id(self.conn, id_, create=True)
405         condition.is_active = self.form_data.get_all_str('is_active') != []
406         condition.title.set(self.form_data.get_str('title'))
407         condition.description.set(self.form_data.get_str('description'))
408         condition.save(self.conn)
409         return f'/condition?id={condition.id_}'
410
411     def _init_handling(self) -> None:
412         # pylint: disable=attribute-defined-outside-init
413         self.conn = DatabaseConnection(self.server.db)
414         parsed_url = urlparse(self.path)
415         self.site = path_split(parsed_url.path)[1]
416         params = parse_qs(parsed_url.query, strict_parsing=True)
417         self.params = InputsParser(params, False)
418
419     def _redirect(self, target: str) -> None:
420         self.send_response(302)
421         self.send_header('Location', target)
422         self.end_headers()
423
424     def _send_html(self, html: str, code: int = 200) -> None:
425         """Send HTML as proper HTTP response."""
426         self.send_response(code)
427         self.end_headers()
428         self.wfile.write(bytes(html, 'utf-8'))
429
430     def _send_msg(self, msg: Exception, code: int = 400) -> None:
431         """Send message in HTML formatting as HTTP response."""
432         html = self.server.jinja.get_template('msg.html').render(msg=msg)
433         self._send_html(html, code)