2024-11-19 12:08:21 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
# @Time : 2024/11/19 下午8:05
|
|
|
|
|
|
# @FileName: manage_project.py
|
|
|
|
|
|
# @Software: PyCharm
|
2024-11-20 14:13:01 +00:00
|
|
|
|
from datetime import datetime
|
2024-11-21 08:38:41 +00:00
|
|
|
|
from typing import List
|
2024-11-19 12:08:21 +00:00
|
|
|
|
|
2024-11-20 08:25:36 +00:00
|
|
|
|
from fastapi import APIRouter
|
2024-11-20 14:13:01 +00:00
|
|
|
|
from sqlalchemy import delete
|
2024-11-19 12:08:21 +00:00
|
|
|
|
|
2024-11-20 14:13:01 +00:00
|
|
|
|
from dependencies import *
|
|
|
|
|
|
from models import Project, ProjectUserLink
|
2024-11-19 13:38:36 +00:00
|
|
|
|
|
2024-11-19 12:08:21 +00:00
|
|
|
|
router = APIRouter()
|
2024-11-20 06:45:46 +00:00
|
|
|
|
TenantRole = 1
|
2024-11-19 12:08:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
2024-11-20 06:45:46 +00:00
|
|
|
|
# 列举所有项目
|
2024-11-19 13:38:36 +00:00
|
|
|
|
@router.get("/api/s1/project")
|
2024-11-20 13:02:44 +00:00
|
|
|
|
async def get_project(response: Response, session: SessionDep, current_user: User = Depends(get_current_user)):
|
|
|
|
|
|
# 只有角色为 0、1、2 或 3 的用户才可以访问
|
|
|
|
|
|
if current_user.role == 0:
|
|
|
|
|
|
# 角色为0,显示所有项目
|
|
|
|
|
|
projects = session.query(Project).all()
|
|
|
|
|
|
elif current_user.role == 1:
|
|
|
|
|
|
# 角色为1,显示tenant_id匹配的项目(即属于当前租户的项目)
|
|
|
|
|
|
projects = session.query(Project).filter(Project.owner_id == current_user.tenant_id).all()
|
|
|
|
|
|
elif current_user.role in [2, 3]:
|
|
|
|
|
|
# 角色为2或3,显示与当前用户相关联的项目
|
|
|
|
|
|
projects = (
|
|
|
|
|
|
session.query(Project)
|
|
|
|
|
|
.join(ProjectUserLink)
|
|
|
|
|
|
.filter(ProjectUserLink.user_id == current_user.id)
|
|
|
|
|
|
.all()
|
|
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="You do not have permission to view projects.")
|
|
|
|
|
|
|
2024-11-19 13:38:36 +00:00
|
|
|
|
if not projects:
|
2024-11-20 13:02:44 +00:00
|
|
|
|
raise HTTPException(status_code=404, detail="Project not found or you have no projects.")
|
|
|
|
|
|
|
|
|
|
|
|
# 返回项目的基本信息
|
2024-11-21 03:17:19 +00:00
|
|
|
|
return [
|
|
|
|
|
|
{
|
|
|
|
|
|
"name": project.name,
|
|
|
|
|
|
"requirement": project.requirement,
|
|
|
|
|
|
"start_time": project.start_time,
|
|
|
|
|
|
"deadline": project.deadline
|
|
|
|
|
|
}
|
|
|
|
|
|
for project in projects
|
|
|
|
|
|
]
|
2024-11-19 13:38:36 +00:00
|
|
|
|
|
2024-11-20 14:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
# 新增与修改项目
|
2024-11-20 06:45:46 +00:00
|
|
|
|
@router.post("/api/s1/project")
|
2024-11-20 13:02:44 +00:00
|
|
|
|
async def create_project(data: dict, session: SessionDep, current_user: User = Depends(get_current_user)):
|
|
|
|
|
|
if current_user.role != 1:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="Only Tenant admin users can add or update projects.")
|
|
|
|
|
|
|
2024-11-21 08:38:41 +00:00
|
|
|
|
# project_id = data.get("project_id")
|
2024-11-21 04:13:19 +00:00
|
|
|
|
name = data.get("name")
|
|
|
|
|
|
requirement = data.get("requirement")
|
|
|
|
|
|
start_time_str = data.get("start_time")
|
|
|
|
|
|
deadline_str = data.get("deadline")
|
|
|
|
|
|
estimators = data.get("estimator")
|
|
|
|
|
|
auditors = data.get("auditor")
|
2024-11-20 06:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
# 验证是否缺少必要参数
|
|
|
|
|
|
if not name or not requirement or not start_time_str or not deadline_str:
|
2024-11-21 04:13:19 +00:00
|
|
|
|
raise HTTPException(status_code=400, detail="Need more details")
|
2024-11-20 06:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
# 验证开始时间是否早于结束时间
|
2024-11-21 04:13:19 +00:00
|
|
|
|
# 去掉 'Z' 和毫秒部分
|
|
|
|
|
|
start_time_str = start_time_str.split('.')[0].rstrip('Z')
|
|
|
|
|
|
deadline_str = deadline_str.split('.')[0].rstrip('Z')
|
|
|
|
|
|
|
2024-11-20 10:31:35 +00:00
|
|
|
|
start_time = datetime.strptime(start_time_str, "%Y-%m-%dT%H:%M:%S")
|
|
|
|
|
|
deadline = datetime.strptime(deadline_str, "%Y-%m-%dT%H:%M:%S")
|
2024-11-21 04:13:19 +00:00
|
|
|
|
|
2024-11-20 06:45:46 +00:00
|
|
|
|
if start_time > deadline:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail="Start time must be before deadline")
|
|
|
|
|
|
|
2024-11-21 02:59:14 +00:00
|
|
|
|
# 验证是否有传入评估/审核员
|
|
|
|
|
|
if not estimators or not auditors:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail="Need more estimators or auditors")
|
2024-11-20 06:45:46 +00:00
|
|
|
|
|
2024-11-21 08:38:41 +00:00
|
|
|
|
users: List[User] = []
|
|
|
|
|
|
# 验证评估审核员是否存在
|
|
|
|
|
|
for username in estimators + auditors:
|
|
|
|
|
|
query_estimator = select(User).where(User.username == username)
|
|
|
|
|
|
if user := session.exec(query_estimator).first():
|
|
|
|
|
|
users.append(user)
|
2024-11-20 06:45:46 +00:00
|
|
|
|
|
2024-11-21 08:38:41 +00:00
|
|
|
|
project = session.exec(select(Project).where(Project.name == name)).first()
|
|
|
|
|
|
if project and project.owner_id != current_user.tenant_id:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="You do not have permission to modify this project.")
|
2024-11-20 06:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
# 更新项目还是新增项目
|
2024-11-21 08:38:41 +00:00
|
|
|
|
if project:
|
2024-11-20 06:45:46 +00:00
|
|
|
|
# 更新项目内容
|
|
|
|
|
|
project.name = name
|
|
|
|
|
|
project.requirement = requirement
|
|
|
|
|
|
project.start_time = start_time
|
|
|
|
|
|
project.deadline = deadline
|
|
|
|
|
|
else:
|
|
|
|
|
|
project = Project(
|
|
|
|
|
|
name=name,
|
|
|
|
|
|
requirement=requirement,
|
|
|
|
|
|
start_time=start_time,
|
|
|
|
|
|
deadline=deadline,
|
2024-11-21 02:59:14 +00:00
|
|
|
|
owner_id=current_user.tenant_id,
|
2024-11-20 06:45:46 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# 处理项目和用户的关联
|
|
|
|
|
|
# 先清除现有的关联
|
|
|
|
|
|
# 生成删除语句并执行
|
2024-11-21 08:38:41 +00:00
|
|
|
|
project.users = []
|
|
|
|
|
|
project.users = users
|
|
|
|
|
|
session.add(project)
|
|
|
|
|
|
session.commit()
|
2024-11-20 06:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
# 提交事务
|
|
|
|
|
|
session.commit()
|
|
|
|
|
|
session.refresh(project)
|
|
|
|
|
|
|
2024-11-20 07:07:21 +00:00
|
|
|
|
return {"message": "Added or updated successfully",
|
|
|
|
|
|
"information": project,
|
2024-11-20 06:45:46 +00:00
|
|
|
|
}
|
2024-11-20 07:07:21 +00:00
|
|
|
|
|
2024-11-20 13:02:44 +00:00
|
|
|
|
|
2024-11-20 14:04:13 +00:00
|
|
|
|
# 删除项目
|
|
|
|
|
|
@router.delete("/api/s1/project")
|
2024-11-21 08:38:41 +00:00
|
|
|
|
async def delete_project(name: str, session: SessionDep, current_user: User = Depends(get_current_user)):
|
2024-11-20 13:02:44 +00:00
|
|
|
|
if current_user.role != 1:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="Only Tenant admin users can delete projects.")
|
|
|
|
|
|
|
2024-11-21 08:38:41 +00:00
|
|
|
|
# project_name = data.get("name")
|
|
|
|
|
|
project_name = name
|
2024-11-20 11:48:02 +00:00
|
|
|
|
|
|
|
|
|
|
if not project_name:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail="Project name is required")
|
2024-11-20 07:07:21 +00:00
|
|
|
|
|
|
|
|
|
|
# 查找项目
|
|
|
|
|
|
project = session.exec(
|
2024-11-20 11:48:02 +00:00
|
|
|
|
select(Project).where(Project.name == project_name)).first()
|
|
|
|
|
|
|
2024-11-20 07:07:21 +00:00
|
|
|
|
if not project:
|
2024-11-20 14:04:13 +00:00
|
|
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
2024-11-19 12:08:21 +00:00
|
|
|
|
|
2024-11-20 07:07:21 +00:00
|
|
|
|
# 删除与项目相关的用户链接
|
|
|
|
|
|
# 先清除现有的关联
|
2024-11-20 11:48:02 +00:00
|
|
|
|
stmt = delete(ProjectUserLink).where(ProjectUserLink.project_id == project.id)
|
2024-11-20 07:07:21 +00:00
|
|
|
|
session.execute(stmt)
|
2024-11-19 12:08:21 +00:00
|
|
|
|
|
2024-11-20 07:07:21 +00:00
|
|
|
|
# 删除项目
|
|
|
|
|
|
session.delete(project)
|
|
|
|
|
|
session.commit()
|
2024-11-19 13:23:59 +00:00
|
|
|
|
|
2024-11-20 14:04:13 +00:00
|
|
|
|
return {"detail": "Project deleted successfully"}
|