From 13c317e80c297505c26ce0d55e94989299ab9f15 Mon Sep 17 00:00:00 2001 From: heshunme Date: Mon, 18 Nov 2024 19:47:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=EF=BC=9Amodels+fastapi=E7=99=BB=E5=BD=95=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 +++ .idea/CostEvalPlatform.iml | 8 +++ .idea/dataSources.xml | 20 +++++++ .idea/inspectionProfiles/Project_Default.xml | 53 +++++++++++++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 32 ++++++++++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ main.py | 55 ++++++++++++++++++ models.py | 50 ++++++++++++++++ test.db | Bin 0 -> 16384 bytes 11 files changed, 246 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/CostEvalPlatform.iml create mode 100644 .idea/dataSources.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 main.py create mode 100644 models.py create mode 100644 test.db diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/CostEvalPlatform.iml b/.idea/CostEvalPlatform.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/CostEvalPlatform.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..95b90ca --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,20 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:E:\pyprojects\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 + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..1c665c8 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,53 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..63337c1 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,32 @@ + + + + + + + + + + EditorConfig + + + GitHub 操作 + + + 正则表达式 + + + 版本控制 + + + + + 用户定义 + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..719a6e7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..ba1a1fe --- /dev/null +++ b/main.py @@ -0,0 +1,55 @@ +from fastapi import FastAPI, HTTPException, Response, Depends +from typing import Optional +from datetime import datetime, timedelta +from jose import JWTError, jwt +from models import * + +app = FastAPI() + +# 创建数据库引擎 +engine = create_engine('sqlite:///test.db') + +# 创建所有表 +Base.metadata.create_all(engine) + +# 创建会话 +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) +session = SessionLocal() + +# 用于生成和验证JWT的密钥 +SECRET_KEY = "your_secret_key" +ALGORITHM = "HS256" + + +# 生成JWT token +def create_access_token(data: dict, expires_delta: Optional[timedelta] = None): + to_encode = data.copy() + if expires_delta: + expire = datetime.utcnow() + expires_delta + else: + expire = datetime.utcnow() + timedelta(minutes=15) + to_encode.update({"exp": expire}) + encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) + return encoded_jwt + + +# 登录路由 +@app.post("/api/s1/login") +async def login(response: Response, user_data: dict): + # 查询用户 + user = session.query(User).filter(User.name == user_data['username']).first() + + # 验证用户名和密码 + if not user or user.password != user_data['password']: + raise HTTPException(status_code=401, detail="Login failed") + + # 生成JWT token + token = create_access_token(data={"sub": user.name}) + + # 设置cookie + response.set_cookie(key="session_token", value=token, httponly=True) + + # 关闭数据库会话 + session.close() + + return {"message": "Login successful"} diff --git a/models.py b/models.py new file mode 100644 index 0000000..1a9c4b7 --- /dev/null +++ b/models.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# @Time : 2024/11/18 下午7:00 +# @Author : 河瞬 +# @FileName: models.py +# @Software: PyCharm +from sqlalchemy import create_engine, Column, Integer, String, DateTime, Enum, ForeignKey, Text +from sqlalchemy.orm import relationship, sessionmaker, declarative_base +from datetime import datetime + +# 创建基类 +Base = declarative_base() + + +class Tenant(Base): + __tablename__ = 'Tenant' + id = Column(Integer, primary_key=True) + name = Column(String(100), nullable=False) + users = relationship('User', backref='Tenant', cascade='all, delete-orphan') + projects = relationship('Project', backref='Tenant', cascade='all, delete-orphan') + + +class User(Base): + __tablename__ = 'User' + id = Column(Integer, primary_key=True) + name = Column(String(100), nullable=False) + password = Column(String(100), nullable=False) + role = Column(Integer, nullable=False) # 0: 超级管理员 1:租户管理员 2:评估员 3:审核员 + tenant_id = Column(Integer, ForeignKey('Tenant.id'), nullable=False) + + +class Project(Base): + __tablename__ = 'Project' + id = Column(Integer, primary_key=True) + name = Column(String(100), nullable=False) + requirement = Column(String(200), nullable=True) + start_time = Column(DateTime, nullable=False, default=datetime.utcnow) + deadline = Column(DateTime, nullable=False) + owner_id = Column(Integer, ForeignKey('Tenant.id'), nullable=False) + # owner = relationship('Tenant', backref='projects') + + +if __name__ == "__main__": + # 创建数据库引擎 + engine = create_engine('sqlite:///test.db') + + # 创建所有表 + Base.metadata.create_all(engine) + + # 创建会话 + SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) \ No newline at end of file diff --git a/test.db b/test.db new file mode 100644 index 0000000000000000000000000000000000000000..5d2c5920a6b470ab4c51cf1cb7b612c8dcb2b5e5 GIT binary patch literal 16384 zcmeI&O;6h}7zc2>L6lbNiZn%4#l=)YY9)ji`vS{~fog346Oa%Gq-yel$>U_3 z#?yB?9(bd!l8viBB{RV)osshWhg(x2K>z{}fB*y_009U<00I!0H-VcqD_>bzp?9lV zc6;g|j((```mtJQ3hsEEdCu21XSK)P8mkuzfzMj5$9K74?!L#|<92(U6$_#4D|X_D z=AI+!pEfreQ=_r^c^Sm2uR=YUv}Goxk%^YEKG%VX-!^M{tqz}x^_BE{LHNpOGz?XI z{>r8fM62V7Q}&IYvU=cW%kAt7-r9B3zWO-0!35vog1b$A#A==jWvIMA{X3Vh z*fzbDnbUEi;xt>+LTT6RJOh~|!zlJAHZK9Ap8Bu5&Ne$Mz$E%DfnPLVsaENACj%Ji zjLO_J!l-k0hUcHF*huf@wEUNt2MGcYfB*y_009U<00Izz00bZaf%y{1Q;XQLE4!on z{{tyM%+~>N8VEoD0uX=z1Rwwb2tWV=5P-nD7AVn0Vt-sJ$i%-G4E^-}Z+`kulVx%) dYoUMu1Rwwb2tWV=5P$##AOHafKwz!}{sF9{%dP+b literal 0 HcmV?d00001