def edit_test_plan_view(request, test_plan_id):
    """ Edits an existing test plan's details """
    if request.method == 'GET':
        plan = TestPlan.objects.get(id=test_plan_id)
        form = TestPlanForm(instance=plan)
        author = plan.creator.username
        currentEdit = []

        return render_to_response(
            'test_plan_form.html', {
            'form': form,
            'type': 'View/Edit Test Plan',
            'plan': plan,
            'author': author,
            'currentEdit': currentEdit,
            }, context_instance=RequestContext(request))
    else:
        plan = TestPlan.objects.get(id=test_plan_id)
        form = TestPlanForm(request.POST, instance=plan)
        testplan = form.save()
        message = "Edited the plan successfully"
        messages.add_message(request, messages.SUCCESS, message)
        return render_to_response(
            'test_plan_add_testcases.html',
            {"testplan": testplan
               }, context_instance=RequestContext(request))
def edit_test_plan_view(request, test_plan_id):
    """ Edits an existing test plan's details """
    if request.method == 'GET':
        plan = TestPlan.objects.get(id=test_plan_id)
        form = TestPlanForm(instance=plan)
        author = plan.creator.username
        currentEdit = []

        return render_to_response(
            'test_plan_form.html', {
            'form': form,
            'type': 'View/Edit Test Plan',
            'plan': plan,
            'author': author,
            'currentEdit': currentEdit,
            }, context_instance=RequestContext(request))
    else:
        plan = TestPlan.objects.get(id=test_plan_id)
        form = TestPlanForm(request.POST, instance=plan)
        testplan = form.save()
        message = "Edited the plan successfully"
        messages.add_message(request, messages.SUCCESS, message)
        return render_to_response(
            'test_plan_add_testcases.html',
            {"testplan": testplan
               }, context_instance=RequestContext(request))
def clone_test_plan_view(request, test_plan_id):

    if request.method == 'GET':
        plan = TestPlan.objects.get(id=test_plan_id)
        previous_name = plan.name
        previous_id = plan.id
        plan.name = "CLONE OF " + plan.name
        plan.id = None
        author = request.user.username
        plan.creator = request.user
        form = TestPlanForm(instance=plan)

        return render_to_response(
            'test_plan_form.html', {
            'form': form,
            'type': 'Clone Test Plan',
            'plan': plan,
            'name': plan.name,
            'previous_id': previous_id,
            'previous_name': previous_name,
            'author': author,
            'clone': True
            }, context_instance=RequestContext(request))
    else:

        form = TestPlanForm(request.POST)
        prevous_testplan = TestPlan.objects.get(id=request.POST['previous_plan_id'])
        try:
            testplan = form.save()
            message = "Edited the plan successfully"
        except:
            message = "it failed"
            return render_to_response('test_plan_form.html', {'form': form, 'type': 'Clone Test Plan', 'plan': prevous_testplan, 'name': prevous_testplan.name}, context_instance=RequestContext(request))

        tests = prevous_testplan.testcases.all()

        for test in tests:
            testplan_testcase_link = TestplanTestcaseLink(testplan=testplan, testcase=test)
            testplan_testcase_link.save()

        message = message + " and added " + str(tests.count()) + " tests"
        messages.add_message(request, messages.SUCCESS, message)
        return render_to_response(
            'test_plan_add_testcases.html',
            {"testplan": testplan
             }, context_instance=RequestContext(request))
def create_test_plan_view(request):
    """Creates a new test plan"""
    author = request.user.username
    if request.method == 'POST':  # If the form has been submitted...
        form = TestPlanForm(request.POST)
        if form.is_valid():
            testplan = form.save()
            message = "Created the plan successfully"
            messages.add_message(request, messages.SUCCESS, message)
            return HttpResponseRedirect(reverse('plan_add_tests', args=(testplan.id,)))
    else:
        form = TestPlanForm(initial={'creator': request.user})

    return render_to_response(
        'test_plan_form.html',
        {'form': form,
         'type': 'Create Test Plan',
         'author': author,
         }, context_instance=RequestContext(request))
def clone_test_plan_view(request, test_plan_id):

    if request.method == 'GET':
        plan = TestPlan.objects.get(id=test_plan_id)
        previous_name = plan.name
        previous_id = plan.id
        plan.name = "CLONE OF " + plan.name
        plan.id = None
        author = request.user.username
        plan.creator = request.user
        form = TestPlanForm(instance=plan)

        return render_to_response(
            'test_plan_form.html', {
            'form': form,
            'type': 'Clone Test Plan',
            'plan': plan,
            'name': plan.name,
            'previous_id': previous_id,
            'previous_name': previous_name,
            'author': author,
            'clone': True
            }, context_instance=RequestContext(request))
    else:

        form = TestPlanForm(request.POST)
        prevous_testplan = TestPlan.objects.get(id=request.POST['previous_plan_id'])

        testplan = form.save()
        message = "Edited the plan successfully"

        tests = prevous_testplan.testcases.all()

        for test in tests:
            testplan_testcase_link = TestplanTestcaseLink(testplan=testplan, testcase=test)
            testplan_testcase_link.save()

        message = message + " and added " + str(tests.count()) + " tests"
        messages.add_message(request, messages.SUCCESS, message)
        return render_to_response(
            'test_plan_add_testcases.html',
            {"testplan": testplan
             }, context_instance=RequestContext(request))
def create_test_plan_view(request):
    """Creates a new test plan"""
    author = request.user.username
    if request.method == 'POST':  # If the form has been submitted...
        form = TestPlanForm(request.POST)
        if form.is_valid():
            testplan = form.save()
            message = "Created the plan successfully"
            messages.add_message(request, messages.SUCCESS, message)
            return HttpResponseRedirect(reverse('plan_add_tests', args=(testplan.id,)))
    else:
        form = TestPlanForm(initial={'creator': request.user})

    return render_to_response(
        'test_plan_form.html',
        {'form': form,
         'type': 'Create Test Plan',
         'author': author,
         }, context_instance=RequestContext(request))