def ajax_search(request, template_name='plan/common/json_plans.txt'): """Display all testplans""" # Define the default sub module SUB_MODULE_NAME = 'plans' # If it's not a search the page will be blank tps = TestPlan.objects.none() query_result = False # if it's a search request the page will be fill if request.REQUEST.items(): search_form = SearchPlanForm(request.REQUEST) if request.REQUEST.get('product'): search_form.populate(product_id=request.REQUEST['product']) else: search_form.populate() if search_form.is_valid(): # Detemine the query is the user's plans and change the sub module value if request.REQUEST.get('author__email__startswith') and len(search_form.changed_data) == 1: if request.user.is_authenticated(): if request.REQUEST['author__email__startswith'] == request.user.username \ or request.REQUEST['author__email__startswith'] == request.user.email: user_email = request.REQUEST['author__email__startswith'] tps = TestPlan.objects.filter(Q(author__email__startswith=user_email) | Q(owner__email__startswith=user_email)).distinct() else: tps = TestPlan.list(search_form.cleaned_data) tps = tps.select_related('author', 'type', 'product') query_result = True # We want to get the number of cases and runs, without doing # lots of per-test queries. # # Ideally we would get the case/run counts using m2m field tricks # in the ORM # Unfortunately, Django's select_related only works on ForeignKey # relationships, not on ManyToManyField attributes # See http://code.djangoproject.com/ticket/6432 # SQLAlchemy can handle this kind of thing in several ways. # Unfortunately we're using Django # The cleanest way I can find to get it into one query is to # use QuerySet.extra() # See http://docs.djangoproject.com/en/dev/ref/models/querysets tps = tps.extra(select={ 'num_cases': RawSQL.num_cases, 'num_runs': RawSQL.num_runs, 'num_children': RawSQL.num_plans, }) else: # Set search active plans only by default # I wish to use 'default' argument, as the same as in ModelForm # But it does not seem to work search_form = SearchPlanForm(initial={'is_active': True}) #columnIndexNameMap is required for correct sorting behavior, 5 should be product, but we use run.build.product columnIndexNameMap = { 0: '', 1: 'plan_id', 2: 'name', 3: 'author__username', 4: 'owner__username', 5: 'product', 6: 'default_product_version', 7: 'type', 8: 'num_cases', 9: 'num_runs', 10: ''} return ajax_response(request, tps, columnIndexNameMap, jsonTemplatePath='plan/common/json_plans.txt')
def all(request, template_name='plan/all.html'): """Display all testplans""" # Define the default sub module SUB_MODULE_NAME = 'plans' # If it's not a search the page will be blank tps = TestPlan.objects.none() query_result = False order_by = request.REQUEST.get('order_by', 'create_date') asc = bool(request.REQUEST.get('asc', None)) # if it's a search request the page will be fill if request.REQUEST.items(): search_form = SearchPlanForm(request.REQUEST) if request.REQUEST.get('product'): search_form.populate(product_id=request.REQUEST['product']) else: search_form.populate() if search_form.is_valid(): # Detemine the query is the user's plans and change the sub module value if request.REQUEST.get('author'): if request.user.is_authenticated(): if request.REQUEST['author'] == request.user.username \ or request.REQUEST['author'] == request.user.email: SUB_MODULE_NAME = "my_plans" query_result = True # build a QuerySet: tps = TestPlan.list(search_form.cleaned_data) tps = tps.select_related('author', 'type', 'product') # We want to get the number of cases and runs, without doing # lots of per-test queries. # # Ideally we would get the case/run counts using m2m field tricks # in the ORM # Unfortunately, Django's select_related only works on ForeignKey # relationships, not on ManyToManyField attributes # See http://code.djangoproject.com/ticket/6432 # SQLAlchemy can handle this kind of thing in several ways. # Unfortunately we're using Django # The cleanest way I can find to get it into one query is to # use QuerySet.extra() # See http://docs.djangoproject.com/en/dev/ref/models/querysets tps = tps.extra(select={ 'num_cases': RawSQL.num_cases, 'num_runs': RawSQL.num_runs, 'num_children': RawSQL.num_plans, }) tps = order_plan_queryset(tps, order_by, asc) else: # Set search active plans only by default # I wish to use 'default' argument, as the same as in ModelForm # But it does not seem to work search_form = SearchPlanForm(initial={'is_active': True}) if request.REQUEST.get('action') == 'clone_case': template_name = 'case/clone_select_plan.html' tps = tps.order_by('name') if request.REQUEST.get('t') == 'ajax': return HttpResponse(serializers.serialize( request.REQUEST.get('f', 'json'), tps, extras=('num_cases','num_runs', 'num_children', 'get_url_path') )) if request.REQUEST.get('t') == 'html': if request.REQUEST.get('f') == 'preview': template_name = 'plan/preview.html' query_url = remove_from_request_path(request, 'order_by') if asc: query_url = remove_from_request_path(query_url, 'asc') else: query_url = '%s&asc=True' % query_url page_type = request.REQUEST.get('page_type', 'pagination') query_url_page_type = remove_from_request_path(request, 'page_type') if query_url_page_type: query_url_page_type = remove_from_request_path(query_url_page_type, 'page') return direct_to_template(request, template_name, { 'module': MODULE_NAME, 'sub_module': SUB_MODULE_NAME, 'test_plans' : tps, 'query_result' : query_result, 'search_plan_form' : search_form, 'query_url': query_url, 'query_url_page_type': query_url_page_type, 'page_type': page_type })