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] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E7=A7=9F=E6=88=B7?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=9A=84=E6=89=80=E6=9C=89=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E6=9A=82=E6=97=B6=E5=8E=BB=E9=99=A4=E4=BA=86=E6=9D=83?= =?UTF-8?q?=E9=99=90=E7=AE=A1=E7=90=86=E4=BB=A5=E6=B5=8B=E8=AF=95=E4=BB=A3?= =?UTF-8?q?=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