def query_export_custom(request): """ Create and serve the CSV file. """ try: content_type = Query.get_content_type(request.GET.get("entity")) except InvalidContentTypeError as e: messages.error(request, e) raise Http404() if content_type.model_class() not in QueryCondition.RELATION_MAP: messages.error( request, "Wrong table name in entity param. Please refer to query docs.") raise Http404() try: conditions = Query.parse_conditions(content_type, request.GET.get("conditions")) except InvalidConditionsError as e: messages.error(request, e) raise Http404() filename = "query_%s_export" % (content_type) try: results = Query.get_queryset(content_type, conditions).visible_by_user(request.user) except (FieldDoesNotExist, FieldError) as e: messages.error(request, e) raise Http404() return _export_query(results, content_type, filename)
def query_export_custom(request): """ Create and serve the CSV file. """ try: content_type = Query.get_content_type(request.GET.get("entity")) except ContentType.DoesNotExist: raise InvalidContentTypeError( "Wrong table name in entity param. Please refer to query docs.") if content_type.model_class() not in QueryCondition.RELATION_MAP: raise InvalidContentTypeError( "Wrong table name in entity param. Please refer to query docs.") conditions = Query.parse_conditions( content_type, request.GET.get("conditions")) filename = "query_%s_export" % (content_type) try: results = Query.get_queryset(content_type, conditions).visible_by_user( request.user) except FieldError: raise InvalidConditionsError("Conditions URL incorrect: Field does " "not exist. Please refer to query docs.") return _export_query(results, content_type, filename)
def query_export_custom(request): """ Create and serve the CSV file. """ try: content_type = Query.get_content_type(request.GET.get("entity")) except InvalidContentTypeError as e: messages.error(request, e) raise Http404() if content_type.model_class() not in QueryCondition.RELATION_MAP: messages.error( request, "Wrong table name in entity param. Please refer to query docs.") raise Http404() try: conditions = Query.parse_conditions( content_type, request.GET.get("conditions")) except InvalidConditionsError as e: messages.error(request, e) raise Http404() filename = "query_%s_export" % (content_type) try: results = Query.get_queryset(content_type, conditions).visible_by_user( request.user) except (FieldDoesNotExist, FieldError) as e: messages.error(request, e) raise Http404() return _export_query(results, content_type, filename)
def query_export_custom(request): """ Create and serve the CSV file. """ try: content_type = Query.get_content_type(request.GET.get("entity")) except ContentType.DoesNotExist: raise InvalidContentTypeError( "Wrong table name in entity param. Please refer to query docs.") if content_type.model_class() not in QueryCondition.RELATION_MAP: raise InvalidContentTypeError( "Wrong table name in entity param. Please refer to query docs.") conditions = Query.parse_conditions(content_type, request.GET.get("conditions")) filename = "query_%s_export" % (content_type) try: results = Query.get_queryset(content_type, conditions).visible_by_user(request.user) except FieldError: raise InvalidConditionsError("Conditions URL incorrect: Field does " "not exist. Please refer to query docs.") return _export_query(results, content_type, filename)
def query_add(request): query = Query() query.owner = request.user if request.GET.get("entity"): query.content_type = Query.get_content_type(request.GET.get("entity")) return query_form(request, BreadCrumbTrail.leading_to(query_add), instance=query)
def query_custom(request): try: content_type = Query.get_content_type(request.GET.get("entity")) except InvalidContentTypeError as e: messages.error(request, e) raise Http404() if content_type.model_class() not in QueryCondition.RELATION_MAP: messages.error( request, "Wrong table name in entity param. Please refer to query docs.") raise Http404() try: conditions = Query.parse_conditions(content_type, request.GET.get("conditions")) except InvalidConditionsError as e: messages.error(request, e) raise Http404() view = QueryCustomResultView( content_type=content_type, conditions=conditions, request=request, model=content_type.model_class(), table_class=QUERY_CONTENT_TYPE_TABLE[content_type.model_class()] ) try: table = QUERY_CONTENT_TYPE_TABLE[content_type.model_class()]( None, request.user, view.get_table_data() ) except (FieldDoesNotExist, FieldError) as e: messages.error(request, e) raise Http404() config = RequestConfig(request, paginate={"per_page": table.length}) config.configure(table) template = loader.get_template('lava_results_app/query_custom.html') return HttpResponse(template.render( { 'query_table': table, 'conditions': conditions, 'terms_data': table.prepare_terms_data(view), 'search_data': table.prepare_search_data(view), 'discrete_data': table.prepare_discrete_data(view), 'bread_crumb_trail': BreadCrumbTrail.leading_to(query_custom), 'context_help': ['lava-queries-charts'], }, request=request) )
def query_custom(request): try: content_type = Query.get_content_type(request.GET.get("entity")) except InvalidContentTypeError as e: messages.error(request, e) raise Http404() if content_type.model_class() not in QueryCondition.RELATION_MAP: messages.error( request, "Wrong table name in entity param. Please refer to query docs.") raise Http404() try: conditions = Query.parse_conditions(content_type, request.GET.get("conditions")) except InvalidConditionsError as e: messages.error(request, e) raise Http404() view = QueryCustomResultView( content_type=content_type, conditions=conditions, request=request, model=content_type.model_class(), table_class=QUERY_CONTENT_TYPE_TABLE[content_type.model_class()] ) try: table = QUERY_CONTENT_TYPE_TABLE[content_type.model_class()]( None, request.user, view.get_table_data() ) except (FieldDoesNotExist, FieldError) as e: messages.error(request, e) raise Http404() config = RequestConfig(request, paginate={"per_page": table.length}) config.configure(table) template = loader.get_template('lava_results_app/query_custom.html') return HttpResponse(template.render( { 'query_table': table, 'conditions': conditions, 'terms_data': table.prepare_terms_data(view), 'search_data': table.prepare_search_data(view), 'discrete_data': table.prepare_discrete_data(view), 'bread_crumb_trail': BreadCrumbTrail.leading_to(query_custom), 'context_help': ['lava-queries-charts'], }, request=request) )
def query_add(request): query = Query() query.owner = request.user if request.GET.get("entity"): query.content_type = Query.get_content_type(request.GET.get("entity")) return query_form( request, BreadCrumbTrail.leading_to(query_add), instance=query)
def chart_custom(request): content_type = Query.get_content_type(request.GET.get("entity")) chart_type = request.GET.get("type") chart_type_choices = ChartQuery._meta.get_field("chart_type").choices if not chart_type: chart_type = ChartQuery._meta.get_field("chart_type").default else: found = False for choice in chart_type_choices: if chart_type in choice: found = True if not found: raise InvalidChartTypeError( "Wrong chart type param. Please refer to chart docs." ) if content_type.model_class() not in QueryCondition.RELATION_MAP: raise InvalidContentTypeError( "Wrong table name in entity param. Please refer to chart docs." ) if content_type.model_class() == TestCase and chart_type == "pass/fail": raise TestCasePassFailChartError( "Chart of TestCase entity cannot be of pass/fail chart type." ) conditions = Query.parse_conditions(content_type, request.GET.get("conditions")) chart = Chart(name="Custom") chart_query = ChartQuery(id=0) chart_query.chart = chart chart_query.chart_type = chart_type chart_data = {} chart_data[0] = chart_query.get_data(request.user, content_type, conditions) template = loader.get_template("lava_results_app/chart_display.html") return HttpResponse( template.render( { "chart": chart, "chart_data": simplejson.dumps(chart_data), "bread_crumb_trail": BreadCrumbTrail.leading_to(chart_custom), "can_admin": False, }, request=request, ) )
def chart_custom(request): content_type = Query.get_content_type(request.GET.get("entity")) chart_type = request.GET.get("type") chart_type_choices = ChartQuery._meta.get_field_by_name( 'chart_type')[0].choices if not chart_type: chart_type = ChartQuery._meta.get_field_by_name( 'chart_type')[0].default else: found = False for choice in chart_type_choices: if chart_type in choice: found = True if not found: raise InvalidChartTypeError( "Wrong chart type param. Please refer to chart docs.") if content_type.model_class() not in QueryCondition.RELATION_MAP: raise InvalidContentTypeError( "Wrong table name in entity param. Please refer to chart docs.") if content_type.model_class() == TestCase and chart_type == "pass/fail": raise TestCasePassFailChartError( "Chart of TestCase entity cannot be of pass/fail chart type.") conditions = Query.parse_conditions( content_type, request.GET.get("conditions")) chart = Chart(name="Custom") chart_query = ChartQuery(id=0) chart_query.chart = chart chart_query.chart_type = chart_type chart_data = {} chart_data[0] = chart_query.get_data(request.user, content_type, conditions) template = loader.get_template('lava_results_app/chart_display.html') return HttpResponse(template.render( { 'chart': chart, 'chart_data': simplejson.dumps(chart_data), 'bread_crumb_trail': BreadCrumbTrail.leading_to( chart_custom), 'can_admin': False, }, request=request) )
def query_custom(request): content_type = Query.get_content_type(request.GET.get("entity")) if content_type.model_class() not in QueryCondition.RELATION_MAP: raise InvalidContentTypeError( "Wrong table name in entity param. Please refer to query docs.") view = QueryCustomResultView( content_type=content_type, conditions=Query.parse_conditions(content_type, request.GET.get("conditions")), request=request, model=content_type.model_class(), table_class=QUERY_CONTENT_TYPE_TABLE[content_type.model_class()] ) try: table = QUERY_CONTENT_TYPE_TABLE[content_type.model_class()]( view.get_table_data() ) except FieldError: raise InvalidConditionsError("Conditions URL incorrect: Field does " "not exist. Please refer to query docs.") config = RequestConfig(request, paginate={"per_page": table.length}) config.configure(table) return render_to_response( 'lava_results_app/query_custom.html', { 'query_table': table, 'terms_data': table.prepare_terms_data(view), 'search_data': table.prepare_search_data(view), 'discrete_data': table.prepare_discrete_data(view), 'bread_crumb_trail': BreadCrumbTrail.leading_to(query_custom), 'context_help': BreadCrumbTrail.leading_to(query_list), }, RequestContext(request) )
def make_custom_query(self, entity, conditions, limit=200): """ Name ---- `make_custom_query` (`entity`, `conditions`, `limit`) Description ----------- Construct and run a custom query and return the results. Arguments --------- `entity`: string The entity you want to query `conditions`: string The conditions of the query `limit`: integer Add a limit to the number of results returned. Defaults to 200. Return value ------------ A list of dictionaries containing the query results. The user should be authenticated with a username and token. Example ------- # Get all test jobs submitted by the user 'kernel-ci', and which ended # as 'Incomplete': server.results.make_custom_query("testjob", "testjob__submitter__exact__kernel-ci," "testjob__health__exact__Incomplete") [{ jobXX }, { jobXY }, ...] # Get all test cases in a test suite named 'custom-tests', that failed, # and for whom the job ended after '2017-04-26 00:00:00'. server.results.make_custom_query("testcase", "testsuite__name__exact__1_custom-tests," "testcase__result__exact__Test failed," "testjob__end_time__gt__2017-04-26 00:00:00") [{ testcaseXX }, { testcaseXY }, ...] """ self._authenticate() try: content_type = Query.get_content_type(entity) except InvalidContentTypeError: raise xmlrpc.client.Fault( 400, "Wrong table name in entity parameter. " "Please refer to query docs.") if content_type.model_class() not in QueryCondition.RELATION_MAP: raise xmlrpc.client.Fault( 400, "Wrong table name in entity parameter. " "Please refer to query docs.") conditions = Query.parse_conditions(content_type, conditions) try: results = Query.get_queryset(content_type, conditions).visible_by_user(self.user) except FieldDoesNotExist: raise xmlrpc.client.Fault( 400, "Conditions URL incorrect: Field does not exist. " "Please refer to query docs.") return list(results[:limit])
def create_notification(job, data): # Create notification object. notification = Notification() if "verbosity" in data: notification.verbosity = Notification.VERBOSITY_MAP[data["verbosity"]] if "type" in data["criteria"]: notification.type = Notification.TYPE_MAP[data["criteria"]["type"]] if "compare" in data: if "blacklist" in data["compare"]: notification.blacklist = data["compare"]["blacklist"] if "query" in data["compare"]: query_data = data["compare"]["query"] if "username" in query_data: # DoesNotExist scenario already verified in validate username = query_data["username"] notification.query_owner = User.objects.get(username=username) notification.query_name = query_data["name"] else: # Custom query. notification.entity = Query.get_content_type( query_data["entity"]) if "conditions" in query_data: # Save conditions as a string. conditions = [ "%s%s%s" % (key, Query.CONDITION_DIVIDER, value) for (key, value) in query_data["conditions"].items() ] notification.conditions = Query.CONDITIONS_SEPARATOR.join( conditions) notification.test_job = job notification.template = Notification.DEFAULT_TEMPLATE notification.save() if "recipients" in data: for recipient in data["recipients"]: notification_recipient = NotificationRecipient( notification=notification) notification_recipient.method = NotificationRecipient.METHOD_MAP[ recipient["to"]["method"]] if "user" in recipient["to"]: user = User.objects.get(username=recipient["to"]["user"]) notification_recipient.user = user if "email" in recipient["to"]: notification_recipient.email = recipient["to"]["email"] if "handle" in recipient["to"]: notification_recipient.irc_handle = recipient["to"]["handle"] if "server" in recipient["to"]: notification_recipient.irc_server = recipient["to"]["server"] # Ignore unique constraint violation. with contextlib.suppress(IntegrityError): notification_recipient.save() else: # You can do "callbacks only" without having recipients, in that # case, no notification will be sent. if "callbacks" not in data and "callback" not in data: # But if there's no callback and no recipients then we add a # submitter as a default recipient. # Ignore unique constraint violation. with contextlib.suppress(IntegrityError): notification_recipient = NotificationRecipient.objects.create( user=job.submitter, notification=notification) # Add callbacks. if "callbacks" in data: for callback in data["callbacks"]: create_callback(job, callback, notification) if "callback" in data: create_callback(job, data["callback"], notification)
def make_custom_query(self, entity, conditions, limit=200): """ Name ---- `make_custom_query` (`entity`, `conditions`, `limit`) Description ----------- Construct and run a custom query and return the results. Arguments --------- `entity`: string The entity you want to query `conditions`: string The conditions of the query `limit`: integer Add a limit to the number of results returned. Defaults to 200. Return value ------------ A list of dictionaries containing the query results. The user should be authenticated with a username and token. Example ------- # Get all test jobs submitted by the user 'kernel-ci', and which ended # as 'Incomplete': server.results.make_custom_query("testjob", "testjob__submitter__exact__kernel-ci," "testjob__health__exact__Incomplete") [{ jobXX }, { jobXY }, ...] # Get all test cases in a test suite named 'custom-tests', that failed, # and for whom the job ended after '2017-04-26 00:00:00'. server.results.make_custom_query("testcase", "testsuite__name__exact__1_custom-tests," "testcase__result__exact__Test failed," "testjob__end_time__gt__2017-04-26 00:00:00") [{ testcaseXX }, { testcaseXY }, ...] """ self._authenticate() try: content_type = Query.get_content_type(entity) except InvalidContentTypeError: raise xmlrpclib.Fault( 400, "Wrong table name in entity parameter. " "Please refer to query docs.") if content_type.model_class() not in QueryCondition.RELATION_MAP: raise xmlrpclib.Fault( 400, "Wrong table name in entity parameter. " "Please refer to query docs.") conditions = Query.parse_conditions(content_type, conditions) try: results = Query.get_queryset(content_type, conditions).visible_by_user(self.user) except FieldDoesNotExist: raise xmlrpclib.Fault(400, "Conditions URL incorrect: Field does not exist. " "Please refer to query docs.") return list(results[:limit])