def pt_comparison_id_json(request, api_ver, project_id, cmp_id): if request.method in ['PUT', 'GET', 'DELETE']: try: comparison = ComparisonModel.objects.get(pk=cmp_id) except ComparisonModel.DoesNotExist: return JsonResponse([], safe=False, status=http.client.NOT_FOUND) if request.method == 'DELETE': comparison.deleted = True comparison.save() messages.success(request, "comparison #%s was deleted" % str(cmp_id)) return JsonResponse([], safe=False) elif request.method == 'PUT': if request.content_type != "application/json": return HttpResponseBadRequest( "content_type must be 'application/json'") body_unicode = request.body.decode('utf-8') try: body = json.loads(body_unicode) except ValueError as ve: return HttpResponseBadRequest( "unable to parse JSON data. Error : {0}".format(ve)) try: ComparisonModel.pt_validate_json(body) except SuspiciousOperation as e: return HttpResponseBadRequest(e) comparison.pt_update(body) messages.success( request, "comparison #%s was successfully edited" % str(cmp_id)) return HttpResponse("OK") elif request.method == 'GET': try: return JsonResponse(ComparisonNestedSerializer( ComparisonModel.objects.get(pk=cmp_id)).data, safe=False) except ComparisonModel.DoesNotExist: return JsonResponse([], safe=False) return JsonResponse([], safe=False, status=http.client.NOT_IMPLEMENTED)
def pt_comparison_all_json(request, api_ver, project_id): class ComparisonJson(BaseDatatableView): # The model we're going to show model = ComparisonModel # define the columns that will be returned columns = [ '', 'id', 'updated', 'project', 'env_node', 'suite_ver', 'title', 'jobs', 'tests_total', 'tests_completed', 'tests_failed' ] # define column names that will be used in sorting # order is important and should be same as order of columns # displayed by datatables. For non sortable columns use empty # value like '' order_columns = [ '', 'id', 'updated', 'project', 'env_node', 'suite_ver', 'title', 'jobs', 'tests_total', 'tests_completed', 'tests_failed' ] # set max limit of records returned, this is used to protect our site if someone tries to attack our site # and make it return huge amount of data max_display_length = 5000 def filter_queryset(self, qs): # filter by project & deleted only, search filtering is performed in-mem in prepare_results if int(project_id) != 0: qs = qs.filter(Q(project_id=project_id)) qs = qs.filter(Q(deleted=False)).prefetch_related( "_jobs", "project") return qs def paging(self, qs): # disable default paging, it has stupid default=10. For client-side DataTables we need to return all return qs def prepare_results(self, qs): return ComparisonSimpleSerializer(qs, many=True).data if request.method == "POST": body_unicode = request.body.decode('utf-8') body = json.loads(body_unicode) try: ComparisonModel.pt_validate_json(body) except SuspiciousOperation as e: return HttpResponseBadRequest(e) cmp = ComparisonModel(project=ProjectModel.pt_get_by_id(project_id)) try: cmp.pt_update(body) except SuspiciousOperation as e: return HttpResponseBadRequest(e) return JsonResponse({'id': cmp.id}, safe=False) return ComparisonJson.as_view()(request)
def pt_comparison_all_json(request, api_ver, project_id): class ComparisonJson(BaseDatatableView): # The model we're going to show model = ComparisonModel # define the columns that will be returned columns = [ '', 'id', 'updated', 'env_node', 'suite_ver', 'title', 'jobs', 'tests_total', 'tests_completed', 'tests_failed' ] # define column names that will be used in sorting # order is important and should be same as order of columns # displayed by datatables. For non sortable columns use empty # value like '' order_columns = [ '', 'id', 'updated', 'env_node', 'suite_ver', 'title', 'jobs', 'tests_total', 'tests_completed', 'tests_failed' ] # set max limit of records returned, this is used to protect our site if someone tries to attack our site # and make it return huge amount of data max_display_length = 5000 def render_column(self, row, column): # We want to render user as a custom column if column == 'tests_total': return '{0} {1}'.format(row.tests_total, row.tests_completed) else: return super(JobJson, self).render_column(row, column) def filter_queryset(self, qs): # use parameters passed in GET request to filter queryset # simple example: search = self.request.GET.get(u'search[value]', None) if search: qs = qs.filter( Q(title__icontains=search) | Q(suite_ver__icontains=search)) if project_id != 0: qs = qs.filter(Q(project_id=project_id)) qs = qs.filter(Q(deleted=False)) return qs def prepare_results(self, qs): return ComparisonSimpleSerializer(qs, many=True).data if request.method == "POST": body_unicode = request.body.decode('utf-8') body = json.loads(body_unicode) try: ComparisonModel.pt_validate_json(body) except SuspiciousOperation as e: return HttpResponseBadRequest(e) cmp = ComparisonModel(project=ProjectModel.pt_get_by_id(project_id)) try: cmp.pt_update(body) except SuspiciousOperation as e: return HttpResponseBadRequest(e) return JsonResponse({'id': cmp.id}, safe=False) return ComparisonJson.as_view()(request)
def get_type_or_none(types, json_data, type_name): return ComparisonModel._pt_get_type(types, json_data, type_name, not_found_rv=None)