def _pt_upload_job_json(data, job_title=None, project_name=None): try: j = json.loads(data) except ValueError as e: return HttpResponse("can't load json: %s" % str(e), status = http.client.UNSUPPORTED_MEDIA_TYPE) if job_title: j['job_title'] = job_title if project_name: j['project_name'] = project_name uuid = j.get('uuid', None) append = j.get('append', False) if not uuid: raise SuspiciousOperation("job 'uuid' is no set") if not pt_is_valid_uuid(uuid): raise SuspiciousOperation("job 'uuid' '%s' is not invalid, it must be version1 UUID" % uuid) new = False try: job = JobModel.objects.get(uuid=uuid) except JobModel.DoesNotExist: new = True job = JobModel(title=j['job_title'], uuid=uuid) try: job.ptUpdate(j) except SuspiciousOperation as e: return HttpResponse(str(e), status = http.client.BAD_REQUEST) return HttpResponse("OK, job %d has been %s" % (job.id, "created" if new else ("appended" if append else "updated")))
def ptJobAllJson(request, api_ver, project_id): class JobJson(BaseDatatableView): # The model we're going to show model = JobModel # define the columns that will be returned columns = [ '', 'id', 'end', 'env_node', 'suite_ver', 'title', 'duration', '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', 'end', 'env_node', 'suite_ver', 'title', 'duration', '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)) # more advanced example using extra parameters # filter_title = self.request.GET.get(u'title', None) # # if filter_title: # title_parts = filter_title.split(' ') # qs_params = None # for part in title_parts: # q = Q(title__istartswith=part)|Q(title__istartswith=part) # qs_params = qs_params | q if qs_params else q # qs = qs.filter(qs_params) return qs def prepare_results(self, qs): return JobSimpleSerializer(qs, many=True).data if request.method == 'POST': body_unicode = request.body.decode('utf-8') body = json.loads(body_unicode) try: JobModel.ptValidateJson(body) except SuspiciousOperation as e: return HttpResponseBadRequest(e) uuid = body.get('uuid', None) append = body.get('append', False) replace = body.get('replace', False) job = None if uuid: try: job = JobModel.objects.get(uuid=uuid) except JobModel.DoesNotExist: pass if not job: if replace: return HttpResponseBadRequest( "Job with uuid %s doesn't exist" % uuid) job = JobModel(title=body['job_title'], uuid=body['uuid']) job.ptUpdate(body) return HttpResponse("OK") return JobJson.as_view()(request)