Esempio n. 1
0
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)