Example #1
0
    def copy(request, api_id):
        if request.method != CommonData.Method.GET.value:
            return HttpResponse(Response.methodInvalidResponse().toJson(), content_type='application/json')
        if ('target_project_id' not in request.GET.keys()):
            response = Response(MissingParametersError, 'Has no param "status"', {})
            return HttpResponse(response.toJson(), content_type='application/json')

        target_project_id = request.GET['target_project_id']

        api = ApiDao.get_api_with_id(api_id)
        result = ApiDao.get_api(target_project_id, api.path, api.method)
        if result is not None:
            response = Response(APIAlreadyExist,
                                '该项目下已存在相同 Method 和 Path 的 API',
                                {})
            return HttpResponse(response.toJson(), content_type='application/json')

        project = ProjectDao.get_project_with_id(target_project_id)

        new_api = Api()
        new_api.path = api.path
        new_api.method = api.method
        new_api.name = api.name
        new_api.note = api.note
        new_api.status = api.status


        original_res = ResTemplateDao.get_private_res_template_with_api_id(api_id)
        res = ResTemplate()
        res.type = original_res.type
        res.name = uuid.uuid4()
        res.mimeType = original_res.mimeType
        res.responseJson = original_res.responseJson
        res.project = project
        result = ResTemplateDao.create(res)

        new_api.resTemplate = result
        result_api = ApiDao.create(new_api)

        project.apis.add(result_api)
        project.save()
        if result_api is None:
            response = Response(DaoOperationError,
                                'API create failed',
                                {})
            return HttpResponse(response.toJson(), content_type='application/json')
        response = Response(Success,
                            'API copy success',
                            {})
        return HttpResponse(response.toJson(), content_type='application/json')
Example #2
0
    def create(request, project_id):
        if request.method != CommonData.Method.POST.value:
            return HttpResponse(Response.methodInvalidResponse().toJson(), content_type='application/json')

        form = ApiCreateForm(data=request.POST)
        # check whether it's valid:
        if form.is_valid():
            model = Api()
            model.path = form.clean().get('path')
            if model.path.startswith('/', ):
                model.path = model.path[1:]
            model.method = form.clean().get('method')
            model.name = form.clean().get('name')
            model.note = form.clean().get('note')
            model.status = form.clean().get('status')

            project = ProjectDao.get_project_with_id(project_id)
            res = ResTemplate()
            res.type = ResTemplate.Type.BelongsToProject.value
            res.name = uuid.uuid4()
            res.mimeType = ResTemplate.MIMEType.ApplicationJson.value
            res.responseJson = form.clean().get('responseJson')
            res.project = project
            result = ResTemplateDao.create(res)
            model.resTemplate = result

            api = ApiDao.create(model)

            project.apis.add(api)
            project.save()
            if api is None:
                response = Response(DaoOperationError,
                                    'API create failed',
                                    {})
                return HttpResponse(response.toJson(), content_type='application/json')
            else:
                response = Response(Success,
                                    'Success',
                                    model_to_dict(api))
                return HttpResponse(response.toJson(), content_type='application/json')
        else:
            return HttpResponse(Response.formParseErrorResponse().toJson(), content_type='application/json')
Example #3
0
    def create(request, project_id):
        if request.method != CommonData.Method.POST.value:
            data = CommonData.response_data(RequetMethodError,
                                            "Method is invalid")
            return HttpResponse(json.dumps(data),
                                content_type="application/json")

        form = ApiCreateForm(data=request.POST)
        # check whether it's valid:
        if form.is_valid():
            model = Api()
            model.path = form.clean().get('path')
            if model.path.startswith('/', ):
                model.path = model.path[1:]
            model.method = form.clean().get('method')
            model.name = form.clean().get('name')
            model.note = form.clean().get('note')
            model.status = form.clean().get('status')
            model.responseJson = form.clean().get('responseJson')
            api = ApiDao.create(model)
            project = ProjectDao.get_project_with_id(project_id)
            project.apis.add(api)
            project.save()
            if api is None:
                data = CommonData.response_data(DaoOperationError,
                                                "API create faild")
                return HttpResponse(json.dumps(data),
                                    content_type="application/json")
            else:
                data = CommonData.response_data(Success, "sucsses")
                data['api'] = model_to_dict(api)
                return HttpResponse(json.dumps(data),
                                    content_type="application/json")
        else:
            data = CommonData.response_data(FormParseError, "form parse faild")
            return HttpResponse(json.dumps(data),
                                content_type="application/json")