From 17cf72777f9988468445666e75129a334175f431 Mon Sep 17 00:00:00 2001 From: MiLla <18826902282@163.com> Date: Tue, 19 Nov 2024 20:15:57 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/manage_tanant.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/manage_tanant.py b/api/manage_tanant.py index 7934876..be34de0 100644 --- a/api/manage_tanant.py +++ b/api/manage_tanant.py @@ -14,7 +14,7 @@ from dependencies import * router = APIRouter() - +#推送修改用注释 @router.get(...) def example(): return "hello" From 5864efcf3e5942d67f52811724fda4423d979487 Mon Sep 17 00:00:00 2001 From: MiLla <18826902282@163.com> Date: Tue, 19 Nov 2024 21:38:36 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=B0=86=E5=90=84=E4=B8=AAapi.py=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E6=8E=89=E4=BA=86=EF=BC=8C=E7=A7=9F=E6=88=B7=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E5=AE=8C=E6=88=90=E4=BA=86=E5=88=97=E4=B8=BE?= =?UTF-8?q?=E6=89=80=E6=9C=89=E7=A7=9F=E6=88=B7=EF=BC=8C=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=AE=80=E5=8D=95=E5=AE=8C=E6=88=90=E4=BA=86?= =?UTF-8?q?=E5=88=97=E4=B8=BE=E6=89=80=E6=9C=89=E9=A1=B9=E7=9B=AE=E3=80=82?= =?UTF-8?q?=20=EF=BC=88=E8=AE=BE=E7=BD=AEpython=E7=8E=AF=E5=A2=83=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E8=87=AA=E5=8A=A8=E5=8A=A0=E4=BA=86=E4=BA=9B?= =?UTF-8?q?=E5=A5=87=E5=A5=87=E6=80=AA=E6=80=AA=E7=9A=84=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=9B=B4=E6=94=B9=EF=BC=8C=E5=BA=94=E8=AF=A5=E4=B8=8D=E8=87=B3?= =?UTF-8?q?=E4=BA=8E=E5=87=BA=E9=97=AE=E9=A2=98=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/CostEvalPlatform.iml | 2 +- .idea/dataSources.xml | 21 +++++++++++++++++++++ .idea/misc.xml | 2 +- api/manage_project.py | 21 ++++++++++++++++++--- api/manage_tanant.py | 18 ++++++++++++++---- api/manage_user.py | 6 +++--- 6 files changed, 58 insertions(+), 12 deletions(-) diff --git a/.idea/CostEvalPlatform.iml b/.idea/CostEvalPlatform.iml index d0876a7..909438d 100644 --- a/.idea/CostEvalPlatform.iml +++ b/.idea/CostEvalPlatform.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 95b90ca..e38120e 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -16,5 +16,26 @@ + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:C:\Users\15089\PycharmProjects\CostEvalPlatform\test.db + $ProjectFileDir$ + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/xerial/sqlite-jdbc/3.45.1.0/sqlite-jdbc-3.45.1.0.jar + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/xerial/sqlite-jdbc/3.45.1.0/sqlite-jdbc-3.45.1.0.jar + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 63337c1..2f33712 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -28,5 +28,5 @@ - + \ No newline at end of file diff --git a/api/manage_project.py b/api/manage_project.py index d833e8d..ed5d3ea 100644 --- a/api/manage_project.py +++ b/api/manage_project.py @@ -12,9 +12,24 @@ from sqlmodel import select from models import Tenant, User, Project from dependencies import * +from typing import List + router = APIRouter() -@router.get(...) -def example(): - return "hello" +#列举所有项目 +@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(project_detail:dict, session: SessionDep): +# project = { +# "name": project_detail["name"], +# +# } +# return {"newProject": project} \ No newline at end of file diff --git a/api/manage_tanant.py b/api/manage_tanant.py index be34de0..250e259 100644 --- a/api/manage_tanant.py +++ b/api/manage_tanant.py @@ -14,7 +14,17 @@ from dependencies import * router = APIRouter() -#推送修改用注释 -@router.get(...) -def example(): - return "hello" +# 推送修改用注释 +# @router.get(...) +# def example(): +# return "hello" + +# 列举所有租户 +@router.get("/api/s1/tenant") +async def get_tenant(response: Response, session: SessionDep): + tenant = session.query(Tenant).filter().all() + if not tenant: + raise HTTPException(status_code=404, detail="Tenant not found") + return {"tenant": tenant} + +# 新增和修改租户 diff --git a/api/manage_user.py b/api/manage_user.py index 59cf9ad..6e075de 100644 --- a/api/manage_user.py +++ b/api/manage_user.py @@ -16,6 +16,6 @@ from dependencies import * router = APIRouter() -@router.get(...) -def example(): - return "hello" +# @router.get(...) +# def example(): +# return "hello" From 20b7429aea4fd2c256fc059d705abd19d6f975d6 Mon Sep 17 00:00:00 2001 From: MiLla <18826902282@163.com> Date: Wed, 20 Nov 2024 14:45:46 +0800 Subject: [PATCH 3/8] =?UTF-8?q?dependencies=E4=B8=AD=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=BA=9Bimport=20=E9=A1=B9=E7=9B=AE=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E4=B8=AD=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=A4=A7=E6=A6=82?= =?UTF-8?q?=E6=98=AF=E6=B2=A1=E9=97=AE=E9=A2=98=E7=9A=84=E5=90=A7=EF=BC=88?= =?UTF-8?q?=E6=9A=82=E6=97=B6=E5=8E=BB=E9=99=A4=E4=BA=86=E8=BA=AB=E4=BB=BD?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/manage_project.py | 147 +++++++++++++++++++++++++++++++++++++++--- dependencies.py | 30 ++++++++- 2 files changed, 166 insertions(+), 11 deletions(-) diff --git a/api/manage_project.py b/api/manage_project.py index ed5d3ea..4c1d669 100644 --- a/api/manage_project.py +++ b/api/manage_project.py @@ -6,30 +6,159 @@ 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 +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): +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(project_detail:dict, session: SessionDep): -# project = { -# "name": project_detail["name"], +# 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"] # -# } -# return {"newProject": project} \ No newline at end of file +# #验证是否缺少必要参数 +# 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") + 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) + + # 处理项目和用户的关联 + # 先清除现有的关联 + # 生成删除语句并执行 + 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 {"newProject": project, + "refreshProject.id": project.id, + } diff --git a/dependencies.py b/dependencies.py index d5e01e7..f6c480d 100644 --- a/dependencies.py +++ b/dependencies.py @@ -4,10 +4,12 @@ # @FileName: dependencies.py # @Software: PyCharm from typing import Annotated -from fastapi import Depends +from fastapi import Depends, Request, HTTPException, Cookie, Response +from jose import jwt, JWTError from database import engine -from sqlmodel import Session +from sqlmodel import Session, select from config import Settings +from models import User def get_session(): @@ -22,3 +24,27 @@ def get_settings(): SessionDep = Annotated[Session, Depends(get_session)] SettingsDep = get_settings() + + +def get_current_user(response: Response, session_token: Annotated[str | None, Cookie()] = None, db: SessionDep = None, + settings: SettingsDep = SettingsDep): + if not session_token: + response.set_cookie(key="session_token", value="", httponly=True) + raise HTTPException(status_code=401, detail="Not authenticated", ) + + try: + payload = jwt.decode(session_token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) + user_id = payload.get("id") + if user_id is None: + response.set_cookie(key="session_token", value="", httponly=True) + raise HTTPException(status_code=401, detail="Invalid token") + except JWTError: + response.set_cookie(key="session_token", value="", httponly=True) + raise HTTPException(status_code=401, detail="Invalid token") + + user = db.exec(select(User).where(User.id == user_id)).first() + if not user: + response.set_cookie(key="session_token", value="", httponly=True) + raise HTTPException(status_code=401, detail="User not found") + + return user From a8e9a4920bff747ed0588093f788f294fd602695 Mon Sep 17 00:00:00 2001 From: MiLla <18826902282@163.com> Date: Wed, 20 Nov 2024 14:55:36 +0800 Subject: [PATCH 4/8] =?UTF-8?q?dependencies=E4=B8=AD=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=BA=9Bimport=20=E9=A1=B9=E7=9B=AE=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E4=B8=AD=EF=BC=8C=E6=96=B0=E5=A2=9E=E4=B8=8E=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E9=83=BD=E5=B7=B2=E5=AE=8C=E6=88=90=EF=BC=88=E6=9A=82?= =?UTF-8?q?=E6=97=B6=E5=8E=BB=E9=99=A4=E4=BA=86=E8=BA=AB=E4=BB=BD=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=EF=BC=89=20ps.=E7=8E=B0=E5=9C=A8=E7=94=A8=E7=9A=84jso?= =?UTF-8?q?n=E6=98=AF=E5=90=A6=E5=B8=A6=E6=9C=89project=5Fid=E6=9D=A5?= =?UTF-8?q?=E5=8C=BA=E5=88=AB=E6=96=B0=E5=A2=9E=E4=B8=8E=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/manage_project.py | 1 + 1 file changed, 1 insertion(+) diff --git a/api/manage_project.py b/api/manage_project.py index 4c1d669..7db113c 100644 --- a/api/manage_project.py +++ b/api/manage_project.py @@ -138,6 +138,7 @@ async def create_project(data: dict, session: SessionDep): # 处理项目和用户的关联 # 先清除现有的关联 # 生成删除语句并执行 + print(project_id) #测试用 stmt = delete(ProjectUserLink).where(ProjectUserLink.project_id == project.id) session.execute(stmt) session.commit() # 提交事务 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 5/8] =?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 From 31b74e8fffa1dbe37bd8fea9c6efde44c7b658b5 Mon Sep 17 00:00:00 2001 From: MiLla <18826902282@163.com> Date: Wed, 20 Nov 2024 18:31:35 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BF=AE=E4=BA=86=E4=BA=9B=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E7=AE=A1=E7=90=86=E7=9A=84bug=EF=BC=8C=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E4=BA=86=E4=B8=8D=E5=B8=A6=E6=9D=83=E9=99=90=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=9A=84=E5=88=97=E4=B8=BE=E6=89=80=E6=9C=89=E7=A7=9F?= =?UTF-8?q?=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/manage_project.py | 6 +-- api/manage_tanant.py | 99 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 98 insertions(+), 7 deletions(-) diff --git a/api/manage_project.py b/api/manage_project.py index ac20ed7..0a3132b 100644 --- a/api/manage_project.py +++ b/api/manage_project.py @@ -43,8 +43,8 @@ async def create_project(data: dict, session: SessionDep): 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") + start_time = datetime.strptime(start_time_str, "%Y-%m-%dT%H:%M:%S") + deadline = datetime.strptime(deadline_str, "%Y-%m-%dT%H:%M:%S") if start_time > deadline: raise HTTPException(status_code=400, detail="Start time must be before deadline") @@ -82,7 +82,7 @@ async def create_project(data: dict, session: SessionDep): requirement=requirement, start_time=start_time, deadline=deadline, - owner_id=1 # 假设owner_id是1 + owner_id=1 # 假设owner_id是1,之后应该是通过token获取owner_id吧 ) session.add(project) diff --git a/api/manage_tanant.py b/api/manage_tanant.py index 250e259..4aafaa6 100644 --- a/api/manage_tanant.py +++ b/api/manage_tanant.py @@ -20,11 +20,102 @@ router = APIRouter() # return "hello" # 列举所有租户 +from fastapi import HTTPException, Response +from sqlalchemy.orm import Session +from models import Tenant, User # 假设你已导入 Tenant 和 User 模型 +from dependencies import SessionDep # 假设 SessionDep 是数据库会话的依赖 + + +#列举所有租户 +from fastapi import HTTPException, Response +from sqlalchemy.orm import Session +from models import Tenant, User # 假设你已导入 Tenant 和 User 模型 +from dependencies import SessionDep # 假设 SessionDep 是数据库会话的依赖 + + @router.get("/api/s1/tenant") async def get_tenant(response: Response, session: SessionDep): - tenant = session.query(Tenant).filter().all() - if not tenant: - raise HTTPException(status_code=404, detail="Tenant not found") - return {"tenant": tenant} + tenants = session.query(Tenant).all() # 获取所有租户 + if not tenants: + raise HTTPException(status_code=404, detail="No tenants found") + + tenant_data = [] + for tenant in tenants: + # 获取该租户中 role=1 的第一个用户(如果存在) + tenant_user = next((user for user in tenant.users if user.role == 1), None) + + # 获取该租户中除了 role=1 以外的用户数量 + user_num = len([user for user in tenant.users if user.role != 1]) + + # 构建租户信息 + tenant_info = { + "name": tenant.name, + "username": tenant_user.username if tenant_user else None, # 如果找到 role=1 的用户,返回其 username + "user_num": user_num # 除去 role=1 的用户数量 + } + + tenant_data.append(tenant_info) + + return {"tenants": tenant_data} # 新增和修改租户 +@router.post("/api/s1/tenant") +async def create_tenant(data: dict, session: SessionDep): + tenant_id = data.get("tenant_id") + name = data["name"] + username = data["username"] + password = data["password"] + + # 验证是否缺少必要参数 + if not name: + raise HTTPException(status_code=400, detail="Need to provide name") + + + # 更新租户还是新增租户 + if tenant_id: + # 查找现有租户 + tenant = session.get(Tenant, tenant_id) + if not tenant: + raise HTTPException(status_code=404, detail="Tenant not found") + + # 更新项目内容 + tenant.name = name + else: + # 新增项目 + project = Project( + name=name, + requirement=requirement, + start_time=start_time, + deadline=deadline, + owner_id=1 # 假设owner_id是1,之后应该是通过token获取owner_id吧 + ) + 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, + } \ No newline at end of file From dfe85ce56523ea4522cbceafdc83a36caef01201 Mon Sep 17 00:00:00 2001 From: MiLla <18826902282@163.com> Date: Wed, 20 Nov 2024 19:24:23 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E7=A7=9F?= =?UTF-8?q?=E6=88=B7=E7=AE=A1=E7=90=86=E7=9A=84=E6=89=80=E6=9C=89=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E6=9A=82=E6=97=B6=E5=8E=BB=E9=99=A4=E4=BA=86?= =?UTF-8?q?=E6=9D=83=E9=99=90=E7=AE=A1=E7=90=86=E4=BB=A5=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/manage_tanant.py | 137 +++++++++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 58 deletions(-) diff --git a/api/manage_tanant.py b/api/manage_tanant.py index 4aafaa6..9e3c89f 100644 --- a/api/manage_tanant.py +++ b/api/manage_tanant.py @@ -6,6 +6,7 @@ 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 @@ -14,25 +15,12 @@ from dependencies import * router = APIRouter() -# 推送修改用注释 -# @router.get(...) -# def example(): -# return "hello" - -# 列举所有租户 from fastapi import HTTPException, Response from sqlalchemy.orm import Session from models import Tenant, User # 假设你已导入 Tenant 和 User 模型 from dependencies import SessionDep # 假设 SessionDep 是数据库会话的依赖 - #列举所有租户 -from fastapi import HTTPException, Response -from sqlalchemy.orm import Session -from models import Tenant, User # 假设你已导入 Tenant 和 User 模型 -from dependencies import SessionDep # 假设 SessionDep 是数据库会话的依赖 - - @router.get("/api/s1/tenant") async def get_tenant(response: Response, session: SessionDep): tenants = session.query(Tenant).all() # 获取所有租户 @@ -60,62 +48,95 @@ async def get_tenant(response: Response, session: SessionDep): # 新增和修改租户 @router.post("/api/s1/tenant") -async def create_tenant(data: dict, session: SessionDep): - tenant_id = data.get("tenant_id") +async def create_or_update_tenant(data: dict, session: SessionDep): name = data["name"] username = data["username"] - password = data["password"] + password = data.get("password", "") # 默认为空字符串 # 验证是否缺少必要参数 - if not name: - raise HTTPException(status_code=400, detail="Need to provide name") + if not name or not username: + raise HTTPException(status_code=400, detail="Need more name/username") + # 查找用户 + user_query = select(User).where(User.username == username) + existing_user = session.exec(user_query).first() - # 更新租户还是新增租户 - if tenant_id: - # 查找现有租户 - tenant = session.get(Tenant, tenant_id) - if not tenant: - raise HTTPException(status_code=404, detail="Tenant not found") - - # 更新项目内容 - tenant.name = name + # 如果密码为空,更新租户信息 + if password == "": + print("密码为空") #测试用 + # 如果用户不存在,返回错误 + if not existing_user: + raise HTTPException(status_code=404, detail="User not found") + else: + # 如果找到了对应的 User + # 使用 user.tenant_id 查找对应的 Tenant + tenant = session.get(Tenant, existing_user.tenant_id) + # 如果 Tenant 存在,更新 Tenant 的 name 字段 + if tenant: + tenant.name = name + session.commit() # 提交更新 + else: + raise HTTPException(status_code=404, detail="Tenant not found") + return {"message": "Tenant and User update successfully"} else: - # 新增项目 - project = Project( + print("密码不为空") #测试用 + # 如果密码不为空,执行创建新租户和用户的操作 + if existing_user: + # 如果用户已存在,返回错误 + raise HTTPException(status_code=409, detail="User already exists") + + # 检查租户是否已存在 + tenant_query = select(Tenant).where(Tenant.name == name) + existing_tenant = session.exec(tenant_query).first() + + if existing_tenant: + raise HTTPException(status_code=409, detail="Tenant name already exists") + + # 创建新租户 + tenant = Tenant( name=name, - requirement=requirement, - start_time=start_time, - deadline=deadline, - owner_id=1 # 假设owner_id是1,之后应该是通过token获取owner_id吧 + username=username, + password=password, # 实际使用时应加密密码 ) - session.add(project) + session.add(tenant) + session.commit() + session.refresh(tenant) - # 处理项目和用户的关联 - # 先清除现有的关联 - # 生成删除语句并执行 - print(project_id) #测试用 - stmt = delete(ProjectUserLink).where(ProjectUserLink.project_id == project.id) + # 创建新用户 + user = User( + username=username, + password=password, # 同样需要加密密码 + role=1, # 默认role为1 + tenant_id = tenant.id, + ) + session.add(user) + + # 提交事务 + session.commit() + session.refresh(tenant) + + return {"message": "Tenant and User added successfully"} + +#删除租户 +@router.delete("/api/s1/tenant") +async def delete_tenant(data: dict, session: SessionDep): + tenant_name = data.get("name") + + if not tenant_name: + raise HTTPException(status_code=400, detail="Tenant name is required") + + # 查找租户 + tenant = session.exec( + select(Tenant).where(Tenant.name == tenant_name)).first() + if not tenant: + raise HTTPException(status_code=404, detail="Tenant not found") + + # 删除与租户相关的用户 + stmt = delete(User).where(User.tenant_id == tenant.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.delete(tenant) session.commit() - session.refresh(project) - return {"message": "Added or updated successfully", - "information": project, - } \ No newline at end of file + return {"detail": "Tenant deleted successfully"} \ No newline at end of file From 601cad31ebc1f8b3fae08d09362d12525b9943e1 Mon Sep 17 00:00:00 2001 From: MiLla <18826902282@163.com> Date: Wed, 20 Nov 2024 19:48:02 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86bug?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=8C=E6=97=A0=E6=9D=83=E9=99=90=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/manage_project.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/api/manage_project.py b/api/manage_project.py index 0a3132b..74b6279 100644 --- a/api/manage_project.py +++ b/api/manage_project.py @@ -25,7 +25,17 @@ 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} + return { + "projects": [ + { + "name": project.name, + "requirement": project.requirement, + "start_time": project.start_time, + "deadline": project.deadline + } + for project in projects + ] + } #新增与修改项目 @router.post("/api/s1/project") @@ -77,6 +87,11 @@ async def create_project(data: dict, session: SessionDep): project.deadline = deadline else: # 新增项目 + exist_project = session.exec(select(Project).where(Project.name == name)).first() + print(exist_project) #测试用 + if exist_project: + raise HTTPException(status_code=404, detail="Project already exists") + project = Project( name=name, requirement=requirement, @@ -117,25 +132,26 @@ async def create_project(data: dict, session: SessionDep): #删除项目 @router.delete("/api/s1/project") -async def delete_project(data : dict, session: SessionDep): - project_id = data.get("project_id") +async def delete_project(data: dict, session: SessionDep): + project_name = data.get("name") + # 权限检查:只有管理员才可以删除项目 # 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") + if not project_name: + raise HTTPException(status_code=400, detail="Project name is required") # 查找项目 project = session.exec( - select(Project).where(Project.id == project_id)).first() + select(Project).where(Project.name == project_name)).first() + if not project: - raise HTTPException(status_code=404, - detail="Project not found or you do not have permission to delete this project") + raise HTTPException(status_code=404,detail="Project not found") # 删除与项目相关的用户链接 # 先清除现有的关联 - stmt = delete(ProjectUserLink).where(ProjectUserLink.project_id == project_id) + stmt = delete(ProjectUserLink).where(ProjectUserLink.project_id == project.id) session.execute(stmt) # 删除项目