def linkTasksToGoal(request, goal, profile): # Create the form and calculate the choices form = forms.GoalTaskLinkForm() taskList = Task.all().ancestor(profile).filter('Unit =', goal.Unit) form.TaskList.choices = [(t.key().id(), t.Name) for t in taskList] taskLinks = TaskLink.all().ancestor(goal) selectedTasks = set([link.Task.key().id() for link in taskLinks]) if request.method == 'POST' and form.validate(): for value, label, selected in form.TaskList.iter_choices(): task = Task.get_by_id(value, profile) if value in selectedTasks: if not selected: # Delete the task link oldLink = TaskLink.all().ancestor(goal).filter('Task =', task).get() if oldLink: oldLink.delete() else: if selected: # Create the task link newLink = TaskLink(parent = goal, Task = task) newLink.put() return redirect(url_for('goal', id = goal.key().id())) else: form.TaskList.data = selectedTasks return render_template("views/goal_link.html", goal = goal, form = form)
def task(id = None, action = None): profile = getCurrentProfile() if id is not None: # Make the action name lowercase action = SafeLower(action) thisTask = Task.get_by_id(int(id), profile) if action is None: # TODO: Make this more efficient! statsDict = LoadObjectStats(thisTask, datetime.now()) statsDict["task"] = thisTask statsDict["recentEntries"] = GoalEntry.all().ancestor(profile).filter("CompletedTask =", thisTask).fetch(5) return render_template('views/task_view.html', **statsDict) elif action == "delete": return render_template('views/delete.html', task = thisTask, typeName = "task") elif action == "edit": return render_template("views/task_edit.html", task = thisTask, typeName = "task") elif action is not None: action = action.lower() if action == "create": return showCreateTask(request, profile) else: if request.method == "GET": taskList = Task.all().ancestor(profile) if not taskList: taskList = [] return render_template("views/task_list.html", tasks = taskList) else: return showCreateTask(request, profile)