def suite_csv_stream(request, job, pk): """ Django is designed for short-lived requests. Streaming responses will tie a worker process for the entire duration of the response. This may result in poor performance. Generally speaking, you should perform expensive tasks outside of the request-response cycle, rather than resorting to a streamed response. https://docs.djangoproject.com/en/1.8/ref/request-response/#django.http.StreamingHttpResponse https://docs.djangoproject.com/en/1.8/howto/outputting-csv/ """ job = get_object_or_404(TestJob, pk=job) test_suite = get_object_or_404(TestSuite, name=pk, job=job) check_request_auth(request, job) querydict = request.GET offset = querydict.get('offset', default=None) limit = querydict.get('limit', default=None) pseudo_buffer = StreamEcho() writer = csv.writer(pseudo_buffer) testcases = get_testcases_with_limit(test_suite, limit, offset) response = StreamingHttpResponse( (writer.writerow(export_testcase(row)) for row in testcases), content_type="text/csv") filename = "lava_stream_%s.csv" % test_suite.name response['Content-Disposition'] = 'attachment; filename="%s"' % filename return response
def get_testsuite_results_csv(self, job_id, suite_name, limit=None, offset=None): """ Name ---- `get_testsuite_results_csv` (`job_id`, `suite_name`, `limit=None`, `offset=None`) Description ----------- Get the suite results of given job id and suite name in CSV format. Arguments --------- `job_id`: string Job id for which the results are required. `suite_name`: string Name of the suite for which the results are required. `limit`: int Limit the number of test cases fetched. `offset`: int Start fetching test cases from a specific point. Return value ------------ This function returns an XML-RPC structures of suite results in CSV format, provided the user is authenticated with an username and token. """ self._authenticate() if not job_id: raise xmlrpclib.Fault(400, "Bad request: TestJob id was not " "specified.") try: job = TestJob.get_by_job_number(job_id) if not job.can_view(self.user): raise xmlrpclib.Fault( 401, "Permission denied for user to job %s" % job_id) output = io.BytesIO() writer = csv.DictWriter( output, quoting=csv.QUOTE_ALL, extrasaction='ignore', fieldnames=testcase_export_fields()) writer.writeheader() test_suite = job.testsuite_set.get(name=suite_name) for row in get_testcases_with_limit(test_suite, limit, offset): writer.writerow(export_testcase(row)) except TestJob.DoesNotExist: raise xmlrpclib.Fault(404, "Specified job not found.") except TestSuite.DoesNotExist: raise xmlrpclib.Fault(404, "Specified test suite not found.") return output.getvalue()
def get_testsuite_results_yaml(self, job_id, suite_name, limit=None, offset=None): """ Name ---- `get_testsuite_results_yaml` (`job_id`, `suite_name`, `limit=None`, `offset=None`) Description ----------- Get the suite results of given job id and suite name in YAML format. Arguments --------- `job_id`: string Job id for which the results are required. `suite_name`: string Name of the suite for which the results are required. `limit`: int Limit the number of test cases fetched. `offset`: int Start fetching test cases from a specific point. Return value ------------ This function returns an XML-RPC structures of suite results in YAML format, provided the user is authenticated with an username and token. """ self._authenticate() if not job_id: raise xmlrpclib.Fault(400, "Bad request: TestJob id was not " "specified.") try: job = TestJob.get_by_job_number(job_id) if not job.can_view(self.user): raise xmlrpclib.Fault( 401, "Permission denied for user to job %s" % job_id) yaml_list = [] test_suite = job.testsuite_set.get(name=suite_name) for test_case in get_testcases_with_limit(test_suite, limit, offset): yaml_list.append(export_testcase(test_case)) except TestJob.DoesNotExist: raise xmlrpclib.Fault(404, "Specified job not found.") except TestSuite.DoesNotExist: raise xmlrpclib.Fault(404, "Specified test suite not found.") return yaml.dump(yaml_list)
def suite_yaml(request, job, pk): job = get_object_or_404(TestJob, pk=job) check_request_auth(request, job) test_suite = get_object_or_404(TestSuite, name=pk, job=job) querydict = request.GET offset = querydict.get('offset', default=None) limit = querydict.get('limit', default=None) response = HttpResponse(content_type='text/yaml') filename = "lava_%s.yaml" % test_suite.name response['Content-Disposition'] = 'attachment; filename="%s"' % filename yaml_list = [] testcases = get_testcases_with_limit(test_suite, limit, offset) for test_case in testcases: yaml_list.append(export_testcase(test_case)) yaml.dump(yaml_list, response, Dumper=yaml.CDumper) return response
def suite_yaml(request, job, pk): job = get_object_or_404(TestJob, pk=job) check_request_auth(request, job) test_suite = get_object_or_404(TestSuite, name=pk, job=job) querydict = request.GET offset = int(querydict.get("offset", default=0)) limit = int(querydict.get("limit", default=0)) response = HttpResponse(content_type="text/yaml") filename = "lava_%s.yaml" % test_suite.name response["Content-Disposition"] = 'attachment; filename="%s"' % filename yaml_list = [] testcases = get_testcases_with_limit(test_suite, limit, offset) for test_case in testcases: yaml_list.append(export_testcase(test_case)) yaml.dump(yaml_list, response, Dumper=yaml.CDumper) return response
def get_testsuite_results_yaml(self, job_id, suite_name, limit=None, offset=None): """ Name ---- `get_testsuite_results_yaml` (`job_id`, `suite_name`, `limit=None`, `offset=None`) Description ----------- Get the suite results of given job id and suite name in YAML format. Arguments --------- `job_id`: string Job id for which the results are required. `suite_name`: string Name of the suite for which the results are required. `limit`: int Limit the number of test cases fetched. `offset`: int Start fetching test cases from a specific point. Return value ------------ This function returns an XML-RPC structures of suite results in YAML format, provided the user is authenticated with an username and token. """ self._authenticate() if not job_id: raise xmlrpc.client.Fault(400, "Bad request: TestJob id was not specified.") try: job = TestJob.get_by_job_number(job_id) if not job.can_view(self.user): raise xmlrpc.client.Fault( 401, "Permission denied for user to job %s" % job_id ) yaml_list = [] test_suite = job.testsuite_set.get(name=suite_name) for test_case in get_testcases_with_limit(test_suite, limit, offset): yaml_list.append(export_testcase(test_case)) except TestJob.DoesNotExist: raise xmlrpc.client.Fault(404, "Specified job not found.") except TestSuite.DoesNotExist: raise xmlrpc.client.Fault(404, "Specified test suite not found.") return yaml.dump(yaml_list)
def suite_csv(request, job, pk): job = get_object_or_404(TestJob, pk=job) check_request_auth(request, job) test_suite = get_object_or_404(TestSuite, name=pk, job=job) querydict = request.GET offset = querydict.get('offset', default=None) limit = querydict.get('limit', default=None) response = HttpResponse(content_type='text/csv') filename = "lava_%s.csv" % test_suite.name response['Content-Disposition'] = 'attachment; filename="%s"' % filename writer = csv.DictWriter(response, quoting=csv.QUOTE_ALL, extrasaction='ignore', fieldnames=testcase_export_fields()) writer.writeheader() testcases = get_testcases_with_limit(test_suite, limit, offset) for row in testcases: writer.writerow(export_testcase(row)) return response
def csv(self, request, **kwargs): limit = request.query_params.get("limit", None) offset = request.query_params.get("offset", None) output = io.StringIO() writer = csv.DictWriter( output, quoting=csv.QUOTE_ALL, extrasaction="ignore", fieldnames=testcase_export_fields(), ) writer.writeheader() for row in get_testcases_with_limit(self.get_object(), limit, offset): writer.writerow(export_testcase(row)) response = HttpResponse(output.getvalue(), content_type="application/csv") response["Content-Disposition"] = ( "attachment; filename=suite_%s.csv" % self.get_object().name) return response
def suite_csv(request, job, pk): job = get_object_or_404(TestJob, pk=job) check_request_auth(request, job) test_suite = get_object_or_404(TestSuite, name=pk, job=job) querydict = request.GET offset = querydict.get('offset', default=None) limit = querydict.get('limit', default=None) response = HttpResponse(content_type='text/csv') filename = "lava_%s.csv" % test_suite.name response['Content-Disposition'] = 'attachment; filename="%s"' % filename writer = csv.DictWriter( response, quoting=csv.QUOTE_ALL, extrasaction='ignore', fieldnames=testcase_export_fields()) writer.writeheader() testcases = get_testcases_with_limit(test_suite, limit, offset) for row in testcases: writer.writerow(export_testcase(row)) return response