def add_tasks(request, project_name_url):
    context = RequestContext(request)
    project_name = decode_url(project_name_url)
    context_dict = {}
    context_dict['project_name'] = project_name
    context_dict['project_name_url'] = project_name_url
    context_dict['action_url'] = "/requirements_tracker/project/"+project_name_url+"/add_task/"
    # If the request is a HTTP POST, try to pull out the relevant information.
    
    user = request.user
    project = Project.objects.get(title=project_name)
    if (not does_user_have_permisions_on_project(user, project)):
        return HttpResponseRedirect('/requirements_tracker/', context_dict, context)  
    
    if request.method == 'POST':
        form = TaskForm(request.POST)
        context_dict['form'] = form
        if form.is_valid():
            page = form.save(commit=False)
            try:
                proj = Project.objects.get(title = project_name)
                page.project = proj
                page.save()
                return HttpResponseRedirect('/requirements_tracker/project/'+project_name_url+'/')
            except Project.DoesNotExist, Project.MultipleObjectsReturned:
                #Wrong url means non existant or wrong project name so just redirect to index page
                return HttpResponseRedirect('/requirements_tracker/', context_dict, context)
        else:
            form.errors
def edit_tasks(request, project_name_url, task_id):
    context = RequestContext(request)
    context_dict = {}
    context_dict['project_name_url'] = project_name_url
    context_dict['action_url'] = '/requirements_tracker/project/'+project_name_url+'/edit_task/'+task_id+'/'
    try:
        task = Task.objects.get(id=task_id)
    except:
        return HttpResponseRedirect('/requirements_tracker/', {}, context)
    project = task.project
    user = request.user
    if (not does_user_have_permisions_on_project(user, project)):
        return HttpResponseRedirect('/requirements_tracker/', context_dict, context)
    
    
    if request.method == 'POST':
        form = TaskForm(request.POST, instance=task)
        if form.is_valid():
            form.save(commit=True)
            return HttpResponseRedirect('/requirements_tracker/project/'+project_name_url+'/',
                                        context_dict, context)
        else:
            print form.errors
    else:
        form = TaskForm(instance = task)
    
    context_dict['form'] = form
    context_dict['task'] = task
    return render_to_response('requirements_tracker/tasks.html', context_dict, context)
        form = TaskForm(request.POST)
        context_dict['form'] = form
        if form.is_valid():
            page = form.save(commit=False)
            try:
                proj = Project.objects.get(title = project_name)
                page.project = proj
                page.save()
                return HttpResponseRedirect('/requirements_tracker/project/'+project_name_url+'/')
            except Project.DoesNotExist, Project.MultipleObjectsReturned:
                #Wrong url means non existant or wrong project name so just redirect to index page
                return HttpResponseRedirect('/requirements_tracker/', context_dict, context)
        else:
            form.errors
    else:
        form = TaskForm()
        context_dict['form'] = form
    return render_to_response('requirements_tracker/tasks.html', context_dict, context)



@login_required
def edit_tasks(request, project_name_url, task_id):
    context = RequestContext(request)
    context_dict = {}
    context_dict['project_name_url'] = project_name_url
    context_dict['action_url'] = '/requirements_tracker/project/'+project_name_url+'/edit_task/'+task_id+'/'
    try:
        task = Task.objects.get(id=task_id)
    except:
        return HttpResponseRedirect('/requirements_tracker/', {}, context)