- if parsed_url.path == app_config['prefix'] + '/calendar':
- params_to_encode += [('start', postvars['start'][0] if len(postvars['start'][0]) > 0 else '-')]
- params_to_encode += [('end', postvars['end'][0] if len(postvars['end'][0]) > 0 else '-')]
-
- elif parsed_url.path == app_config['prefix'] + '/todo':
- task_uuid = postvars['task_uuid'][0]
- date = postvars['date'][0]
- params_to_encode += [('task', task_uuid), ('date', date)]
- db.update_todo(task_uuid, date, postvars['day_effort'][0], 'done' in postvars.keys(), postvars['comment'][0], postvars['joined_day_tags'][0], collect_checked('day_tag_', postvars), postvars['importance'][0])
-
- elif parsed_url.path == app_config['prefix'] + '/task':
- id_ = postvars['id'][0]
- params_to_encode += [('id', id_)]
- if 'title' in postvars.keys():
- db.update_task(id_, postvars['title'][0], postvars['default_effort'][0], postvars['joined_tags'][0], collect_checked('tag_', postvars), collect_checked('link_', postvars))
-
- elif parsed_url.path == app_config['prefix'] + '/day':
- if 'expect_unchosen_done' in postvars.keys():
- params_to_encode += [('hide_unchosen', int('hide_unchosen' in postvars.keys()))] + [('hide_done', int('hide_done' in postvars.keys()))]
- if 'selected_date' in postvars.keys():
- db.selected_date = postvars['selected_date'][0]
- if 't_uuid' in postvars.keys():
- for i, uuid in enumerate(postvars['t_uuid']):
- t = db.tasks[uuid]
- if uuid in db.selected_day.todos.keys() and ((not 'choose' in postvars) or uuid not in postvars['choose']) and not db.selected_day.todos[uuid].is_empty():
- del db.selected_day.todos[uuid]
- if 'choose' in postvars.keys():
- for i, uuid in enumerate(postvars['t_uuid']):
- uuids = postvars['choose'] + postvars['done'] if 'done' in postvars.keys() else []
- if uuid in uuids or postvars['day_effort'][i] != '' or postvars['importance'][i] != '1.0':
- done = 'done' in postvars and uuid in postvars['done']
- db.update_todo_mini(uuid, db.selected_date, postvars['day_effort'][i], done, postvars['importance'][i])
- if 'day_comment' in postvars.keys():
- db.selected_day.comment = postvars['day_comment'][0]
- params_to_encode += [('selected_date', db.selected_date)]
-
- encoded_params = urlencode(params_to_encode)
- homepage = f'{parsed_url.path}?{encoded_params}'
+ if 'todo' == site:
+ todo_id = postvars.get('todo_id')
+ redir_params += [('id', todo_id)]
+ old_todo = db.todos[todo_id] if todo_id in db.todos.keys() else None
+ efforts = {}
+ latest_date = db.selected_date
+ for i, date in enumerate(postvars.get_all('effort_date', [])):
+ if '' == date:
+ continue
+ try:
+ datetime.strptime(date, DATE_FORMAT)
+ except ValueError:
+ raise PlomException(f'bad date string')
+ latest_date = date
+ efforts[date] = None
+ if not (old_todo and old_todo.children):
+ efforts[date] = postvars.get_at_index('effort', i, on_empty=None,
+ float_if_possible=True)
+ if postvars.has('delete'):
+ has_day_effort = len([e for e in efforts.values() if e is not None]) > 0
+ if postvars.has('done')\
+ or postvars.get('comment')\
+ or postvars.get_all('tag', [])\
+ or has_day_effort:
+ raise PlomException('will not remove todo of preserve-worthy values')
+ db.delete_todo(todo_id)
+ postvars.set('return_to', 'calendar')
+ elif postvars.has('update'):
+ if postvars.has('delete_effort'):
+ for date in postvars.get_all('delete_effort'):
+ db.delete_effort(old_todo, date)
+ del efforts[date]
+ children = [db.todos[id_] for id_ in postvars.get_all('adopt_child', [])
+ if id_ in db.todos.keys()]
+ for child in children:
+ if not todo_id in [t.id_ for t in child.parents]:
+ child.parents += [db.todos[todo_id]]
+ tasks_to_birth = [db.tasks[id_] for id_ in postvars.get_all('birth_child', [])]
+ for task in tasks_to_birth:
+ children += [db.add_todo(task=task, efforts={latest_date: None})]
+ db.update_todo(id_=todo_id,
+ efforts=efforts,
+ done=postvars.has('done'),
+ comment=postvars.get('comment', ''),
+ tags=postvars.get_all('tag', []),
+ importance=float(postvars.get('importance')),
+ children=children)
+
+ elif 'task' == site:
+ task_id = postvars.get('task_id')
+ if postvars.has('delete') and task_id in db.tasks.keys():
+ if [t for t in db.todos.values() if task_id == t.task.id_]:
+ raise PlomException('will not remove Task describing existing Todos')
+ if postvars.get('title', '')\
+ or postvars.get_all('tag', [])\
+ or postvars.get_all('subtask', [])\
+ or postvars.get('comment', ''):
+ raise PlomException('will not remove Task of preserve-worthy values')
+ db.delete_task(task_id)
+ elif postvars.has('update'):
+ task = db.update_task(
+ id_=task_id,
+ title=postvars.get('title', ''),
+ default_effort=postvars.get('default_effort', float_if_possible=True),
+ tags=postvars.get_all('tag', []),
+ subtask_ids=postvars.get_all('subtask', []),
+ comment=postvars.get('comment', ''))
+ if postvars.has('add_as_todo'):
+ db.add_todo(task=task, efforts={postvars.get('selected_date'): None})
+ redir_params += [('id', task_id)]
+
+ elif 'pick_tasks' == site:
+ redir_params += [('hide_chosen_tasks', int(postvars.has('hide_chosen_tasks')))]
+ if postvars.has('update'):
+ db.selected_date = postvars.get('date')
+ todos_to_shrink = []
+ todos_to_delete = []
+ for todo in db.selected_day.linked_todos_as_list:
+ if todo.visible and not todo.id_ in postvars.get_all('chosen_todo', []):
+ if len(todo.comment) > 0\
+ or len(todo.day_tags) > 0\
+ or not todo.is_effort_removable(db.selected_date):
+ raise PlomException('will not remove effort of preserve-worthy values')
+ if len(todo.efforts) > 1:
+ todos_to_shrink += [todo]
+ else:
+ todos_to_delete += [todo]
+ for todo in todos_to_shrink:
+ db.delete_effort(todo, db.selected_date)
+ for todo in todos_to_delete:
+ db.delete_todo(todo.id_)
+ for id_ in postvars.get_all('choose_task', []):
+ db.add_todo(task=db.tasks[id_], efforts={db.selected_date: None})
+ for id_ in postvars.get_all('choose_todo', []):
+ db.todos[id_].efforts[db.selected_date] = None
+
+ elif 'do_todos' == site:
+ redir_params += [('hide_done', int(postvars.has('hide_done')))]
+ if postvars.has('update'):
+ db.selected_date = postvars.get('date')
+ redir_params += [('date', db.selected_date)]
+ db.selected_day.comment = postvars.get('day_comment', '')
+ for i, todo_id in enumerate(postvars.get_all('todo_id', [])):
+ old_todo = db.todos[todo_id]
+ done = todo_id in postvars.get_all('done', [])
+ day_effort_input = postvars.get_at_index('effort', i, '')
+ day_effort = float(day_effort_input) if len(day_effort_input) > 0 else None
+ comment = postvars.get_at_index('effort_comment', i, '')
+ if (day_effort is not None) and (not done) and day_effort < 0 and 0 == len(comment):
+ if len(old_todo.efforts) > 1:
+ db.delete_effort(old_todo, db.selected_date)
+ else:
+ db.delete_todo(todo_id)
+ continue
+ importance = float(postvars.get_at_index('importance', i))
+ if old_todo\
+ and old_todo.done == done\
+ and old_todo.day_effort == day_effort\
+ and comment == old_todo.comment\
+ and old_todo.importance == importance:
+ continue
+ db.update_todo_for_day(
+ todo_id,
+ db.selected_date,
+ day_effort,
+ done,
+ comment,
+ importance)
+
+ homepage = postvars.get('return_to')
+ if not homepage:
+ encoded_params = urlencode(redir_params)
+ homepage = f'{site}?{encoded_params}'