From 0594809a485a12d302c073f96a945a16ec9d6b4a Mon Sep 17 00:00:00 2001 From: MiLla <18826902282@163.com> Date: Wed, 20 Nov 2024 15:07:21 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86=5F?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=A1=B9=E7=9B=AE=E5=8A=9F=E8=83=BD=E5=AE=8C?= =?UTF-8?q?=E6=88=90=EF=BC=8C=E4=BE=9D=E6=97=A7=E6=98=AF=E6=9A=82=E6=97=B6?= =?UTF-8?q?=E5=8E=BB=E9=99=A4=E4=BA=86=E6=9D=83=E9=99=90=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E4=BB=A5=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/manage_project.py | 84 +++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 52 deletions(-) diff --git a/api/manage_project.py b/api/manage_project.py index 7db113c..ac20ed7 100644 --- a/api/manage_project.py +++ b/api/manage_project.py @@ -27,56 +27,7 @@ async def get_project(response: Response, session: SessionDep): raise HTTPException(status_code=404, detail="Project not found") return {"projects": projects} - -# 新增和修改项目 -# @router.post("/api/s1/project") -# async def create_project(data:dict, session: SessionDep, current_user: User = Depends(get_current_user)): -# if current_user.role != TenantRole: -# raise HTTPException(status_code=403, detail="Only tenant users can add or update project") -# name = data["name"] -# requirement = data["requirement"] -# start_time = data["start_time"] -# deadline = data["deadline"] -# estimators = data["estimator"] -# auditors = data["auditor"] -# -# #验证是否缺少必要参数 -# if not name or not requirement or not start_time or not deadline or not estimators or not auditors: -# raise HTTPException(status_code=400, detail="Need more details") -# -# #验证开始时间是否早于结束时间 -# if datetime.fromisoformat(start_time) > datetime.fromisoformat(deadline): -# raise HTTPException(status_code=400, detail="Start time must be before deadline") -# -# #验证评估审核员是否存在 -# query_estimators = select(User).where(User.username.in_(estimators), User.tenant_id == current_user.tenant_id) -# users_estimators = session.exec(query_estimators).all() -# query_auditors = select(User).where(User.username.in_(auditors), User.tenant_id == current_user.tenant_id) -# users_auditors = session.exec(query_auditors).all() -# # 提取出所有查询到的 -# existing_estimators = {user.username for user in users_estimators} -# existing_auditors = {user.username for user in users_auditors} -# -# # 验证是否所有的username都存在于数据库中 -# missing_usernames = (set(auditors) | set(estimators)) - existing_estimators - existing_auditors -# -# if missing_usernames: -# raise HTTPException(status_code=404, detail=f"Missing usernames:{missing_usernames}") -# -# newProject = Project( -# name=name, -# requirement=requirement, -# start_time=start_time, -# deadline=deadline, -# owner_id = current_user.tenant_id, -# ) -# session.add(newProject) -# session.commit() -# session.refresh(newProject) -# -# return {"newProject": newProject, -# "refreshProject.id": newProject.id, -# } +#新增与修改项目 @router.post("/api/s1/project") async def create_project(data: dict, session: SessionDep): project_id = data.get("project_id") @@ -160,6 +111,35 @@ async def create_project(data: dict, session: SessionDep): session.commit() session.refresh(project) - return {"newProject": project, - "refreshProject.id": project.id, + return {"message": "Added or updated successfully", + "information": project, } + +#删除项目 +@router.delete("/api/s1/project") +async def delete_project(data : dict, session: SessionDep): + project_id = data.get("project_id") + # 权限检查:只有管理员才可以删除项目 + # if current_user.role != 1: + # raise HTTPException(status_code=403, detail="Only admin users can delete projects") + + if not project_id: + raise HTTPException(status_code=400, detail="Project ID is required") + + # 查找项目 + project = session.exec( + select(Project).where(Project.id == project_id)).first() + if not project: + raise HTTPException(status_code=404, + detail="Project not found or you do not have permission to delete this project") + + # 删除与项目相关的用户链接 + # 先清除现有的关联 + stmt = delete(ProjectUserLink).where(ProjectUserLink.project_id == project_id) + session.execute(stmt) + + # 删除项目 + session.delete(project) + session.commit() + + return {"detail": "Project deleted successfully"} \ No newline at end of file