async def post(self, *args, **kwargs): """ 新增项目数据 """ param = self.request.body.decode('utf-8') param = json.loads(param) form = ProjectForm.from_json(param) name = form.name.data if form.validate(): try: await self.application.objects.get(Project, name=name) return self.json(JsonResponse(code=10007)) except Project.DoesNotExist: project = await self.application.objects.create( Project, name=name, desc=form.desc.data, env=form.env.data, creator=self.current_user) return self.json( JsonResponse(code=1, data={"projectId": project.id})) else: self.set_status(400) return self.json(JsonResponse(code=10004, msg=form.errors))
async def patch(self, project_id, *args, **kwargs): param = self.request.body.decode('utf-8') param = json.loads(param) form = ProjectForm.from_json(param) if form.validate(): name = form.name.data desc = form.desc.data try: existed_project = await self.application.objects.get( Project, id=int(project_id)) existed_project.name = name existed_project.desc = desc await self.application.objects.update(existed_project) return self.json( Result(code=1, msg="项目更新成功!", data={"id": project_id})) except Project.DoesNotExist: self.set_status(404) return self.json(Result(code=10020, msg="项目不存在!")) else: self.set_status(400) return self.json(Result(code=10090, msg=form.errors))
async def patch(self, project_id, *args, **kwargs): """ 更新项目数据 :param project_id: 更新的项目id """ param = self.request.body.decode('utf-8') param = json.loads(param) form = ProjectForm.from_json(param) if form.validate(): try: existed_project = await self.application.objects.get( Project, id=int(project_id)) existed_project.name = form.name.data existed_project.env = form.env.data existed_project.desc = form.desc.data await self.application.objects.update(existed_project) return self.json(JsonResponse(code=1, data={"id": project_id})) except Project.DoesNotExist: self.set_status(404) return self.json(JsonResponse(code=10009)) else: self.set_status(400) return self.json(JsonResponse(code=10004, msg=form.errors))
async def post(self, *args, **kwargs): param = self.request.body.decode('utf-8') param = json.loads(param) form = ProjectForm.from_json(param) name = form.name.data desc = form.desc.data if name is None: return self.json(Result(code=10080, msg="参数有误, 参数name不可缺少")) if desc is None: return self.json(Result(code=10080, msg="参数有误, 参数desc不可缺少")) if form.validate(): try: existed_project = await self.application.objects.get(Project, name=name) return self.json(Result(code=10020, msg='这个项目已经被创建!')) except Project.DoesNotExist: project = await self.application.objects.create( Project, name=name, desc=desc, creator=self.current_user) return self.json( Result(code=1, msg="创建项目成功!", data={"projectId": project.id})) else: self.set_status(400) return self.json(Result(code=10090, msg=form.errors))