145 lines
5.1 KiB
Python
145 lines
5.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
# @Time : 2024/11/19 下午8:05
|
|
# @FileName: manage_project.py
|
|
# @Software: PyCharm
|
|
from fastapi import HTTPException, Response, Depends, APIRouter
|
|
from typing import Optional, Annotated
|
|
from datetime import datetime, timedelta
|
|
from jose import JWTError, jwt
|
|
from sqlalchemy import delete
|
|
|
|
from sqlmodel import select
|
|
|
|
from models import Tenant, User, Project, ProjectUserLink
|
|
from dependencies import *
|
|
|
|
from typing import List
|
|
|
|
router = APIRouter()
|
|
TenantRole = 1
|
|
|
|
|
|
# 列举所有项目
|
|
@router.get("/api/s1/project")
|
|
async def get_project(response: Response, session: SessionDep):
|
|
projects = session.query(Project).filter().all()
|
|
if not projects:
|
|
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):
|
|
project_id = data.get("project_id")
|
|
name = data["name"]
|
|
requirement = data["requirement"]
|
|
start_time_str = data["start_time"]
|
|
deadline_str = data["deadline"]
|
|
estimators = data["estimators"]
|
|
auditors = data["auditors"]
|
|
|
|
# 验证是否缺少必要参数
|
|
if not name or not requirement or not start_time_str or not deadline_str:
|
|
raise HTTPException(status_code=400, detail="Need more name/requirement/start_time/deadline")
|
|
|
|
# 验证开始时间是否早于结束时间
|
|
start_time = datetime.strptime(start_time_str, "%Y-%m-%d")
|
|
deadline = datetime.strptime(deadline_str, "%Y-%m-%d")
|
|
if start_time > deadline:
|
|
raise HTTPException(status_code=400, detail="Start time must be before deadline")
|
|
|
|
# 验证评估审核员是否存在
|
|
query_estimators = select(User).where(User.username.in_(estimators))
|
|
users_estimators = session.exec(query_estimators).all()
|
|
query_auditors = select(User).where(User.username.in_(auditors))
|
|
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}")
|
|
|
|
# 更新项目还是新增项目
|
|
if project_id:
|
|
# 查找现有项目
|
|
project = session.get(Project, project_id)
|
|
if not project:
|
|
raise HTTPException(status_code=404, detail="Project not found")
|
|
|
|
# 更新项目内容
|
|
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,
|
|
owner_id=1 # 假设owner_id是1
|
|
)
|
|
session.add(project)
|
|
|
|
# 处理项目和用户的关联
|
|
# 先清除现有的关联
|
|
# 生成删除语句并执行
|
|
print(project_id) #测试用
|
|
stmt = delete(ProjectUserLink).where(ProjectUserLink.project_id == project.id)
|
|
session.execute(stmt)
|
|
session.commit() # 提交事务
|
|
|
|
# 重新建立与评估员和审核员的关系
|
|
for username in estimators:
|
|
user = next((user for user in users_estimators if user.username == username), None)
|
|
if user:
|
|
project_user_link = ProjectUserLink(project_id=project.id, user_id=user.id)
|
|
session.add(project_user_link)
|
|
|
|
for username in auditors:
|
|
user = next((user for user in users_auditors if user.username == username), None)
|
|
if user:
|
|
project_user_link = ProjectUserLink(project_id=project.id, user_id=user.id)
|
|
session.add(project_user_link)
|
|
|
|
# 提交事务
|
|
session.commit()
|
|
session.refresh(project)
|
|
|
|
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"} |