Exemplo n.º 1
0
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")))
Exemplo n.º 2
0
def pt_job_id_json(request, api_ver, project_id, job_id):

    if request.method == 'PATCH':
        try:
            data = json.loads(request.body.decode("utf-8"))
        except json.JSONDecodeError:
            return HttpResponseBadRequest("Wrong json")

        try:
            job_id = data['job_id']
            link = data['link']
        except KeyError:
            return HttpResponseBadRequest("Wrong data in json")

        try:
            JobModel.pt_change_regression_link(job_id, link)
        except JobModel.DoesNotExist:
            HttpResponseBadRequest("There is no such job")
        except KeyError:
            return HttpResponseBadRequest(
                "Job has already been in this status")

        return HttpResponse("OK")

    elif request.method in ['PUT', 'GET', 'DELETE']:
        try:
            job = JobModel.objects.get(pk=job_id)
        except JobModel.DoesNotExist:
            return JsonResponse([], safe=False, status=http.client.NOT_FOUND)

        if 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))

            job.pt_update(body)
            messages.success(request,
                             "job #%s was successfully edited" % str(job_id))
            return HttpResponse("OK")

        elif request.method == 'GET':
            return JsonResponse(JobNestedSerializer(job).data, safe=False)

        elif request.method == 'DELETE':
            job.deleted = True
            job.save()
            messages.success(request, "job #%s was deleted" % str(job_id))
            return JsonResponse([], safe=False)

    return JsonResponse([], safe=False, status=http.client.NOT_IMPLEMENTED)
Exemplo n.º 3
0
def pt_job_id_json(request, api_ver, project_id, job_id):

    if request.method == 'PATCH':
        try:
            data = json.loads(request.body.decode("utf-8"))
        except json.JSONDecodeError:
            return HttpResponseBadRequest("Wrong json")

        try:
            job_id = data['job_id']
            link = data['link']
        except KeyError:
            return HttpResponseBadRequest("Wrong data in json")

        try:
            JobModel.pt_change_regression_link(job_id, link)
        except JobModel.DoesNotExist:
            HttpResponseBadRequest("There is no such job")
        except KeyError:
            return HttpResponseBadRequest(
                "Job has already been in this status")

        return HttpResponse("OK")

    if request.method == 'GET' or request.method == 'DELETE':
        try:
            job = JobModel.objects.get(pk=job_id)
        except JobModel.DoesNotExist:
            return JsonResponse([], safe=False, status=http.client.NOT_FOUND)

        if request.method == 'GET':
            return JsonResponse(JobNestedSerializer(job).data, safe=False)
        else:
            job.deleted = True
            job.save()
            messages.success(request, "job #%s was deleted" % str(job_id))
            return JsonResponse([], safe=False)

    return JsonResponse([], safe=False, status=http.client.NOT_IMPLEMENTED)
Exemplo n.º 4
0
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)