keep_blank_values=True, strict_parsing=True)
self.form_data = InputsParser(postvars)
if self.site in ('day', 'process', 'todo', 'condition'):
- getattr(self, f'do_POST_{self.site}')()
+ redir_target = getattr(self, f'do_POST_{self.site}')()
self.conn.commit()
else:
msg = f'Page not known as POST target: /{self.site}'
raise NotFoundException(msg)
- self._redirect('/')
+ self._redirect(redir_target)
except HandledException as error:
self._send_msg(error, code=error.http_code)
finally:
self.conn.close()
- def do_POST_day(self) -> None:
+ def do_POST_day(self) -> str:
"""Update or insert Day of date and Todos mapped to it."""
date = self.params.get_str('date')
day = Day.by_id(self.conn, date, create=True)
todo.adopt_from(existing_todos)
todo.make_missing_children(self.conn)
todo.save(self.conn)
+ return f'/day?date={date}'
- def do_POST_todo(self) -> None:
+ def do_POST_todo(self) -> str:
"""Update Todo and its children."""
id_ = self.params.get_int('id')
todo = Todo.by_id(self.conn, id_)
condition.save(self.conn)
for condition in todo.disables:
condition.save(self.conn)
+ return f'/todo?id={todo.id_}'
- def do_POST_process(self) -> None:
+ def do_POST_process(self) -> str:
"""Update or insert Process of ?id= and fields defined in postvars."""
id_ = self.params.get_int_or_none('id')
for _ in self.form_data.get_all_str('delete'):
process = Process.by_id(self.conn, id_)
process.remove(self.conn)
- return
+ return '/processes'
process = Process.by_id(self.conn, id_, create=True)
process.title.set(self.form_data.get_str('title'))
process.description.set(self.form_data.get_str('description'))
steps += [(None, step_process_id, None)]
process.set_steps(self.conn, steps)
process.save(self.conn)
+ return f'/process?id={process.id_}'
- def do_POST_condition(self) -> None:
+ def do_POST_condition(self) -> str:
"""Update/insert Condition of ?id= and fields defined in postvars."""
id_ = self.params.get_int_or_none('id')
condition = Condition.by_id(self.conn, id_, create=True)
condition.title.set(self.form_data.get_str('title'))
condition.description.set(self.form_data.get_str('description'))
condition.save(self.conn)
+ return f'/condition?id={condition.id_}'
def _init_handling(self) -> None:
# pylint: disable=attribute-defined-outside-init
def test_do_POST_condition(self) -> None:
"""Test POST /condition and its effect on the database."""
form_data = {'title': 'foo', 'description': 'foo'}
- self.check_post(form_data, '/condition', 302, '/')
+ self.check_post(form_data, '/condition', 302, '/condition?id=1')
self.assertEqual(1, len(Condition.all(self.db_conn)))
def test_do_GET(self) -> None:
form_data = {'comment': ''}
self.check_post(form_data, '/day', 400)
self.check_post(form_data, '/day?date=foo', 400)
- self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
+ self.check_post(form_data, '/day?date=2024-01-01', 302)
self.check_post({'foo': ''}, '/day?date=2024-01-01', 400)
"""Test POST /process and its effect on the database."""
self.assertEqual(0, len(Process.all(self.db_conn)))
form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.1}
- self.check_post(form_data, '/process?id=', 302, '/')
+ self.check_post(form_data, '/process?id=', 302, '/process?id=1')
self.assertEqual(1, len(Process.all(self.db_conn)))
self.check_post(form_data, '/process?id=FOO', 400)
form_data['effort'] = 'foo'
self.assertEqual(1, len(Process.all(self.db_conn)))
form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0,
'condition': []}
- self.check_post(form_data, '/process?id=', 302, '/')
+ self.check_post(form_data, '/process?id=', 302, '/process?id=2')
form_data['condition'] = [1]
self.check_post(form_data, '/process?id=', 404)
form_data_cond = {'title': 'foo', 'description': 'foo'}
- self.check_post(form_data_cond, '/condition', 302, '/')
- self.check_post(form_data, '/process?id=', 302, '/')
+ self.check_post(form_data_cond, '/condition', 302, '/condition?id=1')
+ self.check_post(form_data, '/process?id=', 302, '/process?id=3')
form_data['disables'] = [1]
- self.check_post(form_data, '/process?id=', 302, '/')
+ self.check_post(form_data, '/process?id=', 302, '/process?id=4')
form_data['enables'] = [1]
- self.check_post(form_data, '/process?id=', 302, '/')
+ self.check_post(form_data, '/process?id=', 302, '/process?id=5')
+ form_data['delete'] = ''
+ self.check_post(form_data, '/process?id=', 404)
+ self.check_post(form_data, '/process?id=6', 404)
+ self.check_post(form_data, '/process?id=5', 302, '/processes')
def test_do_GET(self) -> None:
"""Test /process and /processes response codes."""
def test_do_POST_day(self) -> None:
"""Test Todo posting of POST /day."""
form_data = {'title': '', 'description': '', 'effort': 1}
- self.check_post(form_data, '/process?id=', 302, '/')
- self.check_post(form_data, '/process?id=', 302, '/')
+ self.check_post(form_data, '/process?id=', 302, '/process?id=1')
+ self.check_post(form_data, '/process?id=', 302, '/process?id=2')
proc = Process.by_id(self.db_conn, 1)
proc2 = Process.by_id(self.db_conn, 2)
form_data = {'comment': ''}
- self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
+ self.check_post(form_data, '/day?date=2024-01-01', 302)
self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
form_data['new_todo'] = str(proc.id_)
- self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
+ self.check_post(form_data, '/day?date=2024-01-01', 302)
todos = Todo.by_date(self.db_conn, '2024-01-01')
self.assertEqual(1, len(todos))
todo1 = todos[0]
self.assertEqual(todo1.process.id_, proc.id_)
self.assertEqual(todo1.is_done, False)
form_data['new_todo'] = str(proc2.id_)
- self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
+ self.check_post(form_data, '/day?date=2024-01-01', 302)
todos = Todo.by_date(self.db_conn, '2024-01-01')
todo1 = todos[1]
self.assertEqual(todo1.id_, 2)
"""Test POST /todo."""
def post_and_reload(form_data: dict[str, object],
status: int = 302) -> Todo:
- self.check_post(form_data, '/todo?id=1', status, '/')
+ self.check_post(form_data, '/todo?id=1', status)
return Todo.by_date(self.db_conn, '2024-01-01')[0]
# test minimum
form_data = {'title': '', 'description': '', 'effort': 1}
- self.check_post(form_data, '/process', 302)
+ self.check_post(form_data, '/process', 302, '/process?id=1')
form_data = {'comment': '', 'new_todo': 1}
self.check_post(form_data, '/day?date=2024-01-01', 302)
# test posting to bad URLs
def test_do_POST_day_todo_adoption(self) -> None:
"""Test Todos posted to Day view may adopt existing Todos."""
form_data = {'title': '', 'description': '', 'effort': 1}
- self.check_post(form_data, '/process', 302, '/')
+ self.check_post(form_data, '/process', 302, '/process?id=1')
form_data['new_top_step'] = 1
- self.check_post(form_data, '/process', 302, '/')
+ self.check_post(form_data, '/process', 302, '/process?id=2')
form_data = {'comment': '', 'new_todo': 1}
self.check_post(form_data, '/day?date=2024-01-01', 302)
form_data = {'comment': '', 'new_todo': 2}
def test_do_GET_todo(self) -> None:
"""Test GET /todo response codes."""
form_data = {'title': '', 'description': '', 'effort': 1}
- self.check_post(form_data, '/process?id=', 302, '/')
+ self.check_post(form_data, '/process?id=', 302, '/process?id=1')
form_data = {'comment': '', 'new_todo': 1}
- self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
+ self.check_post(form_data, '/day?date=2024-01-01', 302)
self.check_get('/todo', 400)
self.check_get('/todo?id=', 400)
self.check_get('/todo?id=foo', 400)
self.assertEqual(self.conn.getresponse().status, expected_code)
def check_post(self, data: Mapping[str, object], target: str,
- expected_code: int, redirect_location: str = '/') -> None:
+ expected_code: int, redirect_location: str = '') -> None:
"""Check that POST of data to target yields expected_code."""
encoded_form_data = urlencode(data, doseq=True).encode('utf-8')
headers = {'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': str(len(encoded_form_data))}
self.conn.request('POST', target,
body=encoded_form_data, headers=headers)
+ if redirect_location == '':
+ redirect_location = target
if 302 == expected_code:
self.check_redirect(redirect_location)
else: