def new(request, template_name='run/new.html'): """Display the create test run page.""" SUB_MODULE_NAME = "new_run" # If from_plan does not exist will redirect to plans for select a plan if not request.REQUEST.get('from_plan'): return HttpResponseRedirect(reverse('tcms.apps.testplans.views.all')) plan_id = request.REQUEST.get('from_plan') # Case is required by a test run if not request.REQUEST.get('case'): return HttpResponse(Prompt.render( request=request, info_type=Prompt.Info, info='At least one case is required by a run.', next=reverse('tcms.apps.testplans.views.get', args=[plan_id, ]), )) # Ready to write cases to test plan tcs = TestCase.objects.filter(case_id__in=request.REQUEST.getlist('case')) tp = TestPlan.objects.select_related().get(plan_id=plan_id) tcrs = TestCaseRun.objects.filter(case_run_id__in=request.REQUEST.getlist('case_run_id')) num_unconfirmed_cases = 0 for tc in tcs: # Hardcode here, the case_status_id is CONFIRMED if not tc.case_status.is_confirmed(): num_unconfirmed_cases += 1 if request.REQUEST.get('POSTING_TO_CREATE'): form = NewRunForm(request.POST) if request.REQUEST.get('product'): form.populate(product_id=request.REQUEST['product']) else: form.populate(product_id=tp.product_id) if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data default_tester = form.cleaned_data['default_tester'] tr = TestRun.objects.create( product_version=form.cleaned_data['product_version'], plan_text_version=tp.latest_text() and tp.latest_text().plan_text_version or 0, stop_date=None, summary=form.cleaned_data.get('summary'), notes=form.cleaned_data.get('notes'), plan=tp, build=form.cleaned_data['build'], manager=form.cleaned_data['manager'], default_tester=default_tester, estimated_time=form.cleaned_data['estimated_time'], errata_id=form.cleaned_data['errata_id'], auto_update_run_status = form.cleaned_data['auto_update_run_status'] ) keep_status = form.cleaned_data['keep_status'] keep_assign = form.cleaned_data['keep_assignee'] loop = 1 # not reserve assignee and status, assignee will default set to default_tester if not keep_assign and not keep_status: for case in form.cleaned_data['case']: try: tcp = TestCasePlan.objects.get(plan=tp, case=case) sortkey = tcp.sortkey except ObjectDoesNotExist, error: sortkey = loop * 10 try: assignee_tester = User.objects.get(username=default_tester) except ObjectDoesNotExist, error: assignee_tester = None tr.add_case_run(case=case, sortkey=sortkey, assignee=assignee_tester) loop += 1 # Add case to the run for tcr in tcrs: if (keep_status and keep_assign): tr.add_case_run(case=tcr.case, assignee=tcr.assignee, case_run_status=tcr.case_run_status, sortkey=tcr.sortkey or loop * 10) loop += 1 elif keep_status and not keep_assign: tr.add_case_run(case=tcr.case, case_run_status=tcr.case_run_status, sortkey=tcr.sortkey or loop * 10) loop += 1 elif keep_assign and not keep_status: tr.add_case_run(case=tcr.case, assignee=tcr.assignee, sortkey=tcr.sortkey or loop * 10) loop += 1 # Write the values into tcms_env_run_value_map table for key, value in request.REQUEST.items(): if key.startswith('select_property_id_'): try: property_id = key.split('_')[3] property_id = int(property_id) except IndexError, error: raise except ValueError, error: raise if request.REQUEST.get('select_property_value_%s' % property_id): try: value_id = int(request.REQUEST.get( 'select_property_value_%s' % property_id) ) except ValueError, error: raise TCMSEnvRunValueMap.objects.create( run=tr, value_id=request.REQUEST.get( 'select_property_value_%s' % property_id ), )
'select_property_value_%s' % property_id ), ) return HttpResponseRedirect( reverse('tcms.apps.testruns.views.get', args=[tr.run_id, ]) ) else: estimated_time = reduce(lambda x, y: x + y, [tc.estimated_time for tc in tcs]) form = NewRunForm(initial={ 'summary': 'Test run for %s on %s' % ( tp.name, tp.env_group.all() and tp.env_group.all()[0] or 'Unknown environment' ), 'estimated_time': estimated_time, 'manager': tp.author.email, 'default_tester': request.user.email, 'product': tp.product_id, 'product_version': tp.get_version_id(), }) form.populate(product_id=tp.product_id) return direct_to_template(request, template_name, { 'module': MODULE_NAME, 'sub_module': SUB_MODULE_NAME, 'from_plan': plan_id, 'test_plan': tp, 'test_cases': tcs, 'form': form, 'num_unconfirmed_cases': num_unconfirmed_cases,