示例#1
0
def testcase_valid(request, testcase_id=0, **kwargs):
    if testcase_id:
        testcase = TestCase.objects.get(pk=str(testcase_id))
        testcase_form = TestCaseForm(request.POST, instance=testcase)
        testcasesteps_form = TestCaseStepFormSet(request.POST, instance=testcase)
    else:
        testcase_form = TestCaseForm(request.POST)
        testcasesteps_form = TestCaseStepFormSet(request.POST)

    if testcase_form.is_valid() and testcasesteps_form.is_valid():
        testcase = testcase_form.save()
        testcasesteps_form.instance = testcase
        testcasesteps_form.save()

        log = history.History(request.user, testcase)
        log.add_form(testcase_form, is_new=(testcase_id == 0))
        log.add_formset(testcasesteps_form)
        log.save()
        return success(
            message="TestCase saved", data={"parent_id": getattr(testcase.parent, "id", 0), "current_id": testcase.id}
        )
    else:
        return failed(
            message="Validation errors: %s" % testcase_form.error_message(),
            data=testcase_form.errors_list() + testcasesteps_form._errors_list(),
        )