def save_env_properties(request, test_run): """ helper to save Env properties from TestRun new/edit views """ env_property_id_set = set(request.POST.getlist("env_property_id")) if env_property_id_set: args = [] for property_id in env_property_id_set: select_name = 'select_property_value_%s' % property_id env_values = request.POST.getlist(select_name) if not env_values: continue for value_id in env_values: if value_id: args.append(EnvRunValueMap(run=test_run, value_id=value_id)) EnvRunValueMap.objects.bulk_create(args)
def new(request, template_name='run/new.html'): """Display the create test run page.""" # If from_plan does not exist will redirect to plans for select a plan if not request.POST.get('from_plan'): return HttpResponseRedirect(reverse('plans-all')) plan_id = request.POST.get('from_plan') # case is required by a test run # NOTE: currently this is handled in JavaScript but in the TestRun creation # form cases can be deleted if not request.POST.get('case'): messages.add_message( request, messages.ERROR, _('Creating a TestRun requires at least one TestCase')) return HttpResponseRedirect( reverse('test_plan_url_short', args=[plan_id])) # Ready to write cases to test plan confirm_status = TestCaseStatus.get_CONFIRMED() test_cases = get_selected_testcases(request) # FIXME: optimize this query, only get necessary columns, not all fields # are necessary test_plan = TestPlan.objects.select_related().get(plan_id=plan_id) test_case_runs = TestCaseRun.objects.filter( case_run_id__in=request.POST.getlist('case_run_id')) num_unconfirmed_cases = test_cases.exclude( case_status=confirm_status).count() estimated_time = datetime.timedelta(seconds=0) tcs_values = test_cases.select_related('author', 'case_status', 'category', 'priority') # note: ordered for test_show_create_new_run_page() on PostgreSQL tcs_values = tcs_values.only('case_id', 'summary', 'estimated_time', 'author__email', 'create_date', 'category__name', 'priority__value', 'case_status__name').order_by('case_id') if request.POST.get('POSTING_TO_CREATE'): form = NewRunForm(request.POST) form.populate( product_id=request.POST.get('product', test_plan.product_id)) if form.is_valid(): # Process the data in form.cleaned_data default_tester = form.cleaned_data['default_tester'] test_run = TestRun.objects.create( product_version=form.cleaned_data['product_version'], stop_date=None, summary=form.cleaned_data.get('summary'), notes=form.cleaned_data.get('notes'), plan=test_plan, build=form.cleaned_data['build'], manager=form.cleaned_data['manager'], default_tester=default_tester, estimated_time=form.cleaned_data['estimated_time'], 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'] try: assignee_tester = User.objects.get(username=default_tester) except ObjectDoesNotExist: assignee_tester = None 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=test_plan, case=case) sortkey = tcp.sortkey except ObjectDoesNotExist: sortkey = loop * 10 test_run.add_case_run(case=case, sortkey=sortkey, assignee=assignee_tester) loop += 1 # Add case to the run for test_case_run in test_case_runs: if (keep_status and keep_assign): test_run.add_case_run( case=test_case_run.case, assignee=test_case_run.assignee, case_run_status=test_case_run.case_run_status, sortkey=test_case_run.sortkey or loop * 10) loop += 1 elif keep_status and not keep_assign: test_run.add_case_run( case=test_case_run.case, case_run_status=test_case_run.case_run_status, sortkey=test_case_run.sortkey or loop * 10) loop += 1 elif keep_assign and not keep_status: test_run.add_case_run(case=test_case_run.case, assignee=test_case_run.assignee, sortkey=test_case_run.sortkey or loop * 10) loop += 1 # Write the values into tcms_env_run_value_map table env_property_id_set = set(request.POST.getlist("env_property_id")) if env_property_id_set: args = list() for property_id in env_property_id_set: checkbox_name = 'select_property_id_%s' % property_id select_name = 'select_property_value_%s' % property_id checked = request.POST.getlist(checkbox_name) if checked: env_values = request.POST.getlist(select_name) if not env_values: continue if len(env_values) != len(checked): raise ValueError('Invalid number of env values.') for value_id in env_values: args.append( EnvRunValueMap(run=test_run, value_id=value_id)) EnvRunValueMap.objects.bulk_create(args) return HttpResponseRedirect( reverse('testruns-get', args=[ test_run.run_id, ])) else: estimated_time = reduce(lambda x, y: x + y, (tc.estimated_time for tc in tcs_values)) form = NewRunForm( initial={ 'summary': 'Test run for %s on %s' % (test_plan.name, test_plan.env_group.all() and test_plan.env_group.all()[0] or 'Unknown environment'), 'estimated_time': estimated_time, 'manager': test_plan.author.email, 'default_tester': request.user.email, 'product': test_plan.product_id, 'product_version': test_plan.product_version_id, }) form.populate(product_id=test_plan.product_id) # FIXME: pagination cases within Create New Run page. context_data = { 'from_plan': plan_id, 'test_plan': test_plan, 'test_cases': tcs_values, 'form': form, 'num_unconfirmed_cases': num_unconfirmed_cases, 'run_estimated_time': estimated_time.total_seconds(), } return render(request, template_name, context_data)