+ # posts
+
+ def post_todo(self, id_, postvars, todo_parenthood):
+ if postvars.has('delete') and (not id_ in self.todos.keys()):
+ raise PlomException('can only do this on Todo that already exists')
+ old_todo = self.todos[id_] if id_ in self.todos.keys() else None
+ latest_date = self.selected_date
+ efforts = {}
+ for i, date in enumerate(postvars.get_all('effort_date', [])):
+ if '' == date:
+ continue
+ latest_date = date
+ efforts[date] = postvars.get_at_index('effort', i, float_if_possible=True)
+ if postvars.has('delete'):
+ raise PlomException('can only do this on Task that already exists')
+ 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')
+ self.delete_todo(id_)
+ return False
+ elif postvars.has('update'):
+ if postvars.has('delete_effort'):
+ for date in postvars.get_all('delete_effort'):
+ self.delete_effort(old_todo, date)
+ del efforts[date]
+ deps = [self.todos[adopt_id] for adopt_id in postvars.get_all('adopt_dep', [])
+ if adopt_id in self.todos.keys()]
+ birth_dep_ids = postvars.get_all('birth_dep', [])
+ for bad_id in [bad_id for bad_id in birth_dep_ids if not bad_id in self.tasks.keys()]:
+ raise PlomException('submitted illegal dep ID')
+ tasks_to_birth = [self.tasks[dep_id] for dep_id in birth_dep_ids]
+ for task in tasks_to_birth:
+ deps += [self.add_todo(task=task,
+ efforts={latest_date: None},
+ parenthood=todo_parenthood)]
+ depender_ids = postvars.get_all('depender', [])
+ self.update_todo(id_=id_,
+ efforts=efforts,
+ done=postvars.has('done'),
+ comment=postvars.get('comment', ''),
+ tags=postvars.get_all('tag', []),
+ importance=postvars.get('importance', float_if_possible=True),
+ deps=deps,
+ depender_ids=depender_ids)
+ return True
+
+ def post_task(self, id_, postvars):
+ if (postvars.has('delete') or postvars.has('fork')) and (not id_ in self.tasks.keys()):
+ raise PlomException('can only do this on Task that already exists')
+ if postvars.has('delete'):
+ if [t for t in self.todos.values() if 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('dep', [])\
+ or postvars.get('comment', ''):
+ raise PlomException('will not remove Task of preserve-worthy values')
+ self.delete_task(id_)
+ return None
+ elif postvars.has('update'):
+ dep_ids = postvars.get_all('dep', [])
+ for bad_id in [bad_id for bad_id in dep_ids if not bad_id in self.tasks.keys()]:
+ raise PlomException('submitted illegal dep ID')
+ depender_ids = postvars.get_all('depender', [])
+ for bad_id in [bad_id_ for bad_id in depender_ids if not bad_id in self.tasks.keys()]:
+ raise PlomException('submitted illegal dep ID')
+ task = self.update_task(
+ id_=id_,
+ title=postvars.get('title', ''),
+ default_effort=postvars.get('default_effort', float_if_possible=True),
+ tags=postvars.get_all('tag', []),
+ comment=postvars.get('comment', ''),
+ fences_adoptions=postvars.get('fences_adoptions', False),
+ dep_ids=dep_ids,
+ depender_ids=depender_ids)
+ if postvars.has('add_as_todo'):
+ self.add_todo(task=task, efforts={postvars.get('new_todo_date'): None})
+ elif postvars.has('fork'):
+ t = self.fork_task(id_)
+ return t.id_
+ return id_
+
+ def post_day_todos(self, postvars, todo_parenthood):
+ if not postvars.has('update'):
+ return
+ self.selected_date = postvars.get('date')
+ self.selected_day.comment = postvars.get('day_comment', '')
+ task_id = postvars.get('choose_task', None)
+ if task_id:
+ if task_id not in self.tasks.keys():
+ raise PlomException('illegal task ID entered')
+ self.add_todo(task=self.tasks[task_id], efforts={self.selected_date: None},
+ parenthood=todo_parenthood)
+ for todo_id in postvars.get_all('choose_todo', []):
+ self.todos[todo_id].efforts[self.selected_date] = None
+ for i, todo_id in enumerate(postvars.get_all('todo_id', [])):
+ old_todo = self.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:
+ self.delete_effort(old_todo, self.selected_date)
+ else:
+ self.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
+ self.update_todo_for_day(
+ todo_id,
+ self.selected_date,
+ day_effort,
+ done,
+ comment,
+ importance)
+
+ # helpers
+
+ def init_calendar_items(self, start_date_str, end_date_str):
+ self.tag_filter_and = ['calendar']
+ self.tag_filter_not = ['deleted']
+
+ todays_date_obj = datetime.strptime(today_date(), DATE_FORMAT)
+ yesterdays_date_obj = todays_date_obj - timedelta(1)
+ def get_day_limit_obj(index, day_limit_string):
+ date_obj = datetime.strptime(sorted(self.days.keys())[index], DATE_FORMAT)
+ if day_limit_string and len(day_limit_string) > 0:
+ if day_limit_string in {'today', 'yesterday'}:
+ date_obj = todays_date_obj if day_limit_string == 'today' else yesterdays_date_obj
+ else:
+ date_obj = datetime.strptime(day_limit_string, DATE_FORMAT)
+ return date_obj
+ start_date_obj = get_day_limit_obj(0, start_date_str)
+ end_date_obj = get_day_limit_obj(-1, end_date_str)
+
+ days_to_show = {}
+ for n in range(int((end_date_obj - start_date_obj).days) + 1):
+ date_obj = start_date_obj + timedelta(n)
+ date_str = date_obj.strftime(DATE_FORMAT)
+ if date_str not in self.days.keys():
+ days_to_show[date_str] = self.add_day(date_str)
+ else:
+ days_to_show[date_str] = self.days[date_str]
+ days_to_show[date_str].month_title = date_obj.strftime('%B') if date_obj.day == 1 else None
+ days_to_show[date_str].weekday = datetime.strptime(date_str, DATE_FORMAT).strftime('%A')[:2]
+ return days_to_show
+