def ajax_search(request, template_name='run/common/json_runs.txt'): """Response request to search test runs from Search Runs""" search_form = SearchRunForm(request.GET) if request.GET.get('product'): search_form.populate(product_id=request.GET['product']) else: search_form.populate() if search_form.is_valid(): trs = TestRun.list(search_form.cleaned_data) trs = trs.select_related( 'manager', 'default_tester', 'plan', 'build').only('run_id', 'summary', 'manager__username', 'default_tester__id', 'default_tester__username', 'plan__name', 'env_value', 'build__product__name', 'stop_date', 'product_version__value') # Further optimize by adding caserun attributes: column_names = [ '', 'run_id', 'summary', 'manager__username', 'default_tester__username', 'plan', 'build__product__name', 'product_version', 'total_num_caseruns', 'stop_date', 'completed', ] data_table_result = DataTableResult(request.GET, trs, column_names) response_data = data_table_result.get_response_data() searched_runs = response_data['querySet'] # Get associated statistics data run_ids = [run.pk for run in searched_runs] qs = TestCaseRun.objects.filter( run__in=run_ids).values('run').annotate(cases_count=Count('case')) cases_subtotal = magic_convert(qs, key_name='run', value_name='cases_count') for run in searched_runs: run_id = run.pk cases_count = cases_subtotal.get(run_id, 0) run.nitrate_stats = { 'cases': cases_count, } else: response_data = { 'sEcho': int(request.GET.get('sEcho', 0)), 'iTotalRecords': 0, 'iTotalDisplayRecords': 0, 'runs': TestRun.objects.none(), } json_data = render_to_string(template_name, response_data, request=request) return HttpResponse(json_data, content_type='application/json')
def ajax_response(request, queryset, column_names, template_name): """ JSON template for the ajax request for searching """ data_table_result = DataTableResult(request.GET, queryset, column_names) data = data_table_result.get_response_data() data['querySet'] = calculate_stats_for_testplans(data['querySet']) # todo: prepare the JSON with the response, consider using : # from django.template.defaultfilters import escapejs json_result = render_to_string(template_name, data, request=request) return HttpResponse(json_result, content_type='application/json')
def search_runs(request): """Search test runs""" search_form = SearchRunForm(request.GET) product_id = request.GET.get('product') search_form.populate(product_id=int(product_id) if product_id else None) runs = TestRun.objects.none() if search_form.is_valid(): runs = (TestRun.list(search_form.cleaned_data).select_related( 'manager', 'default_tester', 'build', 'plan', 'build__product').only( 'run_id', 'summary', 'manager__username', 'default_tester__id', 'default_tester__username', 'plan__name', 'build__product__name', 'stop_date', 'product_version__value').extra( select={'cases_count': RawSQL.total_num_caseruns})) column_names = [ '', 'run_id', 'summary', 'manager__username', 'default_tester__username', 'build__product__name', 'product_version__value', 'env_groups', 'cases_count', 'stop_date', 'completed', ] dt = DataTableResult(request.GET, runs, column_names, default_order_key='-pk') response_data = dt.get_response_data() calculate_associated_data(response_data['querySet']) if 'sEcho' in request.GET: resp_data = (get_template('run/common/json_runs.txt').render( response_data, request)) return JsonResponse(json.loads(resp_data)) else: return render(request, 'run/all.html', context={ 'module': MODULE_NAME, 'sub_module': 'runs', 'object_list': response_data['querySet'], 'search_form': search_form, 'total_count': runs.count() })
def ajax_response(request, queryset, column_names, template_name): """json template for the ajax request for searching""" dt = DataTableResult(request.GET, queryset, column_names) data = dt.get_response_data() data['querySet'] = calculate_stats_for_testplans(data['querySet']) # prepare the JSON with the response, consider using : # from django.template.defaultfilters import escapejs t = get_template(template_name) resp_data = t.render(data, request) return JsonResponse(json.loads(resp_data))
def load_runs_of_one_plan(request, plan_id, template_name='plan/common/json_plan_runs.txt'): """A dedicated view to return a set of runs of a plan This view is used in a plan detail page, for the contained testrun tab. It replaces the original solution, with a paginated resultset in return, serves as a performance healing. Also, in order for user to locate the data, it accepts field lookup parameters collected from the filter panel in the UI. """ column_names = [ '', 'run_id', 'summary', 'manager__username', 'default_tester__username', 'start_date', 'build__name', 'stop_date', 'total_num_caseruns', 'failure_caseruns_percent', 'successful_caseruns_percent', ] test_plan = TestPlan.objects.get(plan_id=plan_id) form = PlanFilterRunForm(request.GET) if form.is_valid(): queryset = test_plan.run.filter(**form.cleaned_data) queryset = queryset.select_related('build', 'manager', 'default_tester').order_by('-pk') data_table_result = DataTableResult(request.GET, queryset, column_names) response_data = data_table_result.get_response_data() searched_runs = response_data['querySet'] # Get associated statistics data run_filters = dict(('run__{0}'.format(key), value) for key, value in form.cleaned_data.items()) query_set = TestCaseRun.objects.filter( case_run_status=TestCaseRunStatus.id_failed(), **run_filters).values('run', 'case_run_status').annotate( count=Count('pk')).order_by('run', 'case_run_status') failure_subtotal = magic_convert(query_set, key_name='run', value_name='count') query_set = TestCaseRun.objects.filter( case_run_status=TestCaseRunStatus.id_passed(), **run_filters).values('run', 'case_run_status').annotate( count=Count('pk')).order_by('run', 'case_run_status') success_subtotal = magic_convert(query_set, key_name='run', value_name='count') query_set = TestCaseRun.objects.filter( **run_filters).values('run').annotate( count=Count('case')).order_by('run') cases_subtotal = magic_convert(query_set, key_name='run', value_name='count') for run in searched_runs: run_id = run.pk cases_count = cases_subtotal.get(run_id, 0) if cases_count: failure_percent = failure_subtotal.get( run_id, 0) * 1.0 / cases_count * 100 success_percent = success_subtotal.get( run_id, 0) * 1.0 / cases_count * 100 else: failure_percent = success_percent = 0 run.nitrate_stats = { 'cases': cases_count, 'failure_percent': failure_percent, 'success_percent': success_percent, } else: response_data = { 'sEcho': int(request.GET.get('sEcho', 0)), 'iTotalRecords': 0, 'iTotalDisplayRecords': 0, 'querySet': TestRun.objects.none(), } json_data = render_to_string(template_name, response_data, request=request) return HttpResponse(json_data, content_type='application/json')
def advance_search(request, tmpl='search/advanced_search.html'): """View of /advance-search/""" errors = None data = request.GET target = data.get('target') plan_form = PlanForm(data) case_form = CaseForm(data) run_form = RunForm(data) # Update MultipleModelChoiceField on each form dynamically plan_form.populate(data) case_form.populate(data) run_form.populate(data) all_forms = (plan_form, case_form, run_form) errors = [f.errors for f in all_forms if not f.is_valid()] if errors or not data: products = Product.objects.order_by('pk').only('pk', 'name') plan_types = TestPlanType.objects.order_by('name').only('pk', 'name') priorities = Priority.objects.filter(is_active=True).order_by('value') errors = fmt_errors(errors) return render(request, tmpl, context=locals()) start_time = time.time() results = search_objects(request, plan_form.cleaned_data, run_form.cleaned_data, case_form.cleaned_data, target) results = order_targets(results, data) queries = fmt_queries(*[f.cleaned_data for f in all_forms]) queries['Target'] = target search_infos = { 'plan': SearchInfo(column_names=[ '', 'plan_id', 'name', 'author__username', 'owner__username', 'product', 'product_version', 'type', 'cases_count', 'runs_count', '' ], template_file='plan/common/json_plans.txt'), 'case': SearchInfo(column_names=[ '', '', 'case_id', 'summary', 'author__username', 'default_tester__username', '', 'case_status__name', 'category__name', 'priority__value', 'create_date' ], template_file='case/common/json_cases.txt'), 'run': SearchInfo(column_names=[ '', 'run_id', 'summary', 'manager__username', 'default_tester__username', 'build__product__name', 'product_version__value', 'env_groups', 'cases_count', 'stop_date', 'completed' ], template_file='run/common/json_runs.txt') } search_info = search_infos[target] dt = DataTableResult(request.GET, results, search_info.column_names, default_order_key='-pk') response_data = dt.get_response_data() if target == 'run': from tcms.testruns.views import calculate_associated_data calculate_associated_data(response_data['querySet']) if 'sEcho' in request.GET: resp_data = (get_template(search_info.template_file).render( response_data, request)) return JsonResponse(json.loads(resp_data)) else: end_time = time.time() time_cost = round(end_time - start_time, 3) return render( request, 'search/results.html', context={ 'search_target': target, 'time_cost': time_cost, 'queries': queries, # FIXME: choose another name rather than this_page 'object_list': response_data['querySet'], 'total_count': response_data['iTotalRecords'], })
def get(self, request, *args, **kwargs): _, plans = self.filter_plans() dt = DataTableResult(request.GET, plans, self.column_names) data = dt.get_response_data() resp_data = get_template(self.template_name).render(data, request) return JsonResponse(json.loads(resp_data))