def update_status(request, project_id, api_id): if request.method != CommonData.Method.GET.value: data = CommonData.response_data(RequetMethodError, "Method is invalid") return HttpResponse(json.dumps(data), content_type="application/json") if ('status' not in request.GET.keys()): data = CommonData.response_data(MissingParametersError, "缺少参数") return HttpResponse(json.dumps(data), content_type="application/json") status = request.GET['status'] api = ApiDao.get_api_with_id(api_id) if api is not None: api.status = status result = ApiDao.update(api) if result is False: data = CommonData.response_data(DaoOperationError, "Update API Faild") return HttpResponse(json.dumps(data), content_type="application/json") else: data = CommonData.response_data(Success, "Success") return HttpResponse(json.dumps(data), content_type="application/json") else: data = CommonData.response_data(DaoOperationError, "API is not exist") return HttpResponse(json.dumps(data), content_type="application/json")
def update(request, project_id, api_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 = ApiUpdateForm(data=request.POST) if form.is_valid(): model = ApiDao.get_api_with_id(api_id) 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') result = ApiDao.update(model) if result is False: data = CommonData.response_data(DaoOperationError, "API update faild") return HttpResponse(json.dumps(data), content_type="application/json") else: data = CommonData.response_data(Success, "Sucsses") 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")
def star(request, project_id, api_id): if request.method != CommonData.Method.GET.value: data = CommonData.response_data(RequetMethodError, "Method is invalid") return HttpResponse(json.dumps(data), content_type="application/json") api = ApiDao.get_api_with_id(api_id) if api is not None: api.star = not api.star print(api.star) result = ApiDao.update(api) if result is False: data = CommonData.response_data(DaoOperationError, "Update API Faild") return HttpResponse(json.dumps(data), content_type="application/json") else: data = CommonData.response_data(Success, "Success") return HttpResponse(json.dumps(data), content_type="application/json") else: data = CommonData.response_data(DaoOperationError, "API is not exist") return HttpResponse(json.dumps(data), content_type="application/json")
def update_status(request, project_id, api_id): if request.method != CommonData.Method.GET.value: return HttpResponse(Response.methodInvalidResponse().toJson(), content_type='application/json') if ('status' not in request.GET.keys()): response = Response(MissingParametersError, 'Has no param "status"', {}) return HttpResponse(response.toJson(), content_type='application/json') status = request.GET['status'] api = ApiDao.get_api_with_id(api_id) if api is not None: api.status = status result = ApiDao.update(api) if result is False: response = Response(DaoOperationError, 'Update API Failed', {}) return HttpResponse(response.toJson(), content_type='application/json') else: response = Response(Success, 'Success', {}) return HttpResponse(response.toJson(), content_type='application/json') else: response = Response(DaoOperationError, 'API is not exist', {}) return HttpResponse(response.toJson(), content_type='application/json')
def batch_update_status(request): if request.method != CommonData.Method.GET.value: response = Response(RequestMethodError, 'Method is invalid', {}) return HttpResponse(response.toJson(), content_type='application/json') if 'api_ids[]' not in request.GET.keys(): response = Response(MissingParametersError, 'Missing Parameters', {}) return HttpResponse(response.toJson(), content_type='application/json') if ('status' not in request.GET.keys()): response = Response(MissingParametersError, 'Has no param "status"', {}) return HttpResponse(response.toJson(), content_type='application/json') status = int(request.GET['status']) enum_list = [e.value for e in Api.Status] if status not in enum_list: response = Response(MissingParametersError, 'status 参数错误', {}) return HttpResponse(response.toJson(), content_type='application/json') api_ids = request.GET.getlist('api_ids[]') for api_id in api_ids: api = ApiDao.get_api_with_id(api_id) if api is not None: api.status = status ApiDao.update(api) response = Response(Success, 'Success', {}) return HttpResponse(response.toJson(), content_type='application/json')
def get_private_res_template_with_api_id(api_id): try: api = ApiDao.get_api_with_id(api_id) if api is None: return None res = api.resTemplate return res except: return None
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')
def detail(request, project_id, api_id): if request.method != CommonData.Method.GET.value: data = CommonData.response_data(RequetMethodError, "Method is invalid") return HttpResponse(json.dumps(data), content_type="application/json") isOriginal = True if 'isOriginal' in request.GET.keys(): isOriginal = False api = ApiDao.get_api_with_id(api_id=api_id) data = CommonData.response_data(Success, "Success") if isOriginal == False: api.responseJson = json.loads(api.responseJson) data['api'] = api.as_dict() return HttpResponse(json.dumps(data, default=datetime2string), content_type="application/json")
def move(request): apis = ApiDao.get_all_api_list() response = Response(Success, 'Success', list(apis)) for api in apis: res = ResTemplate() res.name = uuid.uuid4() res.mimeType = 0 res.responseJson = api['responseJson'] res.type = ResTemplate.Type.BelongsToProject.value project = Project.objects.get(project_id=int(api['project'])) project.save() res.project = project result = ResTemplateDao.create(res) a = ApiDao.get_api_with_id(api['api_id']) a.resTemplate = result a.save() return HttpResponse(response.toJson(), content_type='application/json')
def detail(request, project_id, api_id): if request.method != CommonData.Method.GET.value: return HttpResponse(Response.methodInvalidResponse().toJson(), content_type='application/json') isOriginal = True if 'isOriginal' in request.GET.keys(): isOriginal = False api = ApiDao.get_api_with_id(api_id=api_id) response = Response(Success, 'Success', {}) dic = api.as_dict() if isOriginal == False: dic['responseJson'] = json.loads(api.resTemplate.responseJson) else: dic['responseJson'] = api.resTemplate.responseJson response.data = dic return HttpResponse(response.toJson(), content_type='application/json')
def star(request, project_id, api_id): if request.method != CommonData.Method.GET.value: return HttpResponse(Response.methodInvalidResponse().toJson(), content_type='application/json') api = ApiDao.get_api_with_id(api_id) if api is not None: api.star = not api.star result = ApiDao.update(api) if result is False: response = Response(DaoOperationError, 'Update API Failed', {}) return HttpResponse(response.toJson(), content_type='application/json') else: response = Response(Success, 'Success', {}) return HttpResponse(response.toJson(), content_type='application/json') else: response = Response(DaoOperationError, 'API is not exist', {}) return HttpResponse(response.toJson(), content_type='application/json')
def update(request, project_id, api_id): if request.method != CommonData.Method.POST.value: return HttpResponse(Response.methodInvalidResponse().toJson(), content_type='application/json') form = ApiUpdateForm(data=request.POST) if form.is_valid(): model = ApiDao.get_api_with_id(api_id) 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') res = ResTemplateDao.get_private_res_template_with_api_id(api_id) res.responseJson = form.clean().get('responseJson') ResTemplateDao.update(res) result = ApiDao.update(model) if result is False: response = Response(DaoOperationError, 'API update failed', {}) return HttpResponse(response.toJson(), content_type='application/json') else: response = Response(Success, 'Success', {}) return HttpResponse(response.toJson(), content_type='application/json') else: response = Response(FormParseError, 'Form parse failed', {}) return HttpResponse(response.toJson(), content_type='application/json')