SQLAlchemy1.0->SQLAlchemy2.x Column->mapped-column

This commit is contained in:
2024-12-08 10:16:10 +09:00
parent 3aec075927
commit 91df7ed0fa
2 changed files with 107 additions and 107 deletions

View File

@@ -1,7 +1,7 @@
import jwt
from fastapi.security import OAuth2PasswordBearer
from passlib.context import CryptContext
from datetime import datetime, timedelta
from datetime import datetime, timedelta,timezone
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
import os
import base64
@@ -27,9 +27,9 @@ def verify_password(plain_password: str, hashed_password: str) -> bool:
def create_access_token(*, data: dict, expires_delta: timedelta = None):
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
expire = datetime.now(timezone.utc) + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
expire = datetime.now(timezone.utc) + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt

View File

@@ -1,5 +1,5 @@
from sqlalchemy import Boolean, Column, Integer, String, DateTime,ForeignKey,Table
from sqlalchemy.orm import relationship,as_declarative
from sqlalchemy.orm import Mapped,relationship,as_declarative,mapped_column
from datetime import datetime
from app.db.session import Base
from app.core.security import chacha20Decrypt
@@ -28,14 +28,14 @@ rolepermission = Table(
class User(Base):
__tablename__ = "user"
email = Column(String(50), unique=True, index=True, nullable=False)
first_name = Column(String(100))
last_name = Column(String(100))
hashed_password = Column(String(200), nullable=False)
is_active = Column(Boolean, default=True)
is_superuser = Column(Boolean, default=False)
createuserid = Column(Integer,ForeignKey("user.id"))
updateuserid = Column(Integer,ForeignKey("user.id"))
email = mapped_column(String(50), unique=True, index=True, nullable=False)
first_name = mapped_column(String(100))
last_name = mapped_column(String(100))
hashed_password = mapped_column(String(200), nullable=False)
is_active = mapped_column(Boolean, default=True)
is_superuser = mapped_column(Boolean, default=False)
createuserid = mapped_column(Integer,ForeignKey("user.id"))
updateuserid = mapped_column(Integer,ForeignKey("user.id"))
createuser = relationship('User',foreign_keys=[createuserid])
updateuser = relationship('User',foreign_keys=[updateuserid])
roles = relationship("Role",secondary=userrole,back_populates="users")
@@ -44,44 +44,44 @@ class User(Base):
class Role(Base):
__tablename__ = "role"
name = Column(String(100))
description = Column(String(255))
level = Column(Integer)
name = mapped_column(String(100))
description = mapped_column(String(255))
level = mapped_column(Integer)
users = relationship("User",secondary=userrole,back_populates="roles")
permissions = relationship("Permission",secondary=rolepermission,back_populates="roles")
class Permission(Base):
__tablename__ = "permission"
menu = Column(String(100))
function = Column(String(255))
privilege = Column(String(100))
menu = mapped_column(String(100))
function = mapped_column(String(255))
privilege = mapped_column(String(100))
roles = relationship("Role",secondary=rolepermission,back_populates="permissions")
class App(Base):
__tablename__ = "app"
domainurl = Column(String(200), nullable=False)
appname = Column(String(200), nullable=False)
appid = Column(String(100), index=True, nullable=False)
version = Column(Integer)
createuserid = Column(Integer,ForeignKey("user.id"))
updateuserid = Column(Integer,ForeignKey("user.id"))
domainurl = mapped_column(String(200), nullable=False)
appname = mapped_column(String(200), nullable=False)
appid = mapped_column(String(100), index=True, nullable=False)
version = mapped_column(Integer)
createuserid = mapped_column(Integer,ForeignKey("user.id"))
updateuserid = mapped_column(Integer,ForeignKey("user.id"))
createuser = relationship('User',foreign_keys=[createuserid])
updateuser = relationship('User',foreign_keys=[updateuserid])
class AppVersion(Base):
__tablename__ = "appversion"
domainurl = Column(String(200), nullable=False)
appname = Column(String(200), nullable=False)
appid = Column(String(100), index=True, nullable=False)
version = Column(Integer)
versionname = Column(String(200), nullable=False)
comment = Column(String(200), nullable=False)
createuserid = Column(Integer,ForeignKey("user.id"))
updateuserid = Column(Integer,ForeignKey("user.id"))
domainurl = mapped_column(String(200), nullable=False)
appname = mapped_column(String(200), nullable=False)
appid = mapped_column(String(100), index=True, nullable=False)
version = mapped_column(Integer)
versionname = mapped_column(String(200), nullable=False)
comment = mapped_column(String(200), nullable=False)
createuserid = mapped_column(Integer,ForeignKey("user.id"))
updateuserid = mapped_column(Integer,ForeignKey("user.id"))
createuser = relationship('User',foreign_keys=[createuserid])
updateuser = relationship('User',foreign_keys=[updateuserid])
@@ -89,82 +89,82 @@ class AppVersion(Base):
class AppSetting(Base):
__tablename__ = "appsetting"
appid = Column(String(100), index=True, nullable=False)
setting = Column(String(1000))
appid = mapped_column(String(100), index=True, nullable=False)
setting = mapped_column(String(1000))
class Kintone(Base):
__tablename__ = "kintone"
type = Column(Integer, index=True, nullable=False)
name = Column(String(100), nullable=False)
desc = Column(String)
content = Column(String)
type = mapped_column(Integer, index=True, nullable=False)
name = mapped_column(String(100), nullable=False)
desc = mapped_column(String)
content = mapped_column(String)
class Action(Base):
__tablename__ = "action"
name = Column(String(100), index=True, nullable=False)
title = Column(String(200))
subtitle = Column(String(500))
outputpoints = Column(String)
property = Column(String)
categoryid = Column(Integer,ForeignKey("category.id"))
nosort = Column(Integer)
name = mapped_column(String(100), index=True, nullable=False)
title = mapped_column(String(200))
subtitle = mapped_column(String(500))
outputpoints = mapped_column(String)
property = mapped_column(String)
categoryid = mapped_column(Integer,ForeignKey("category.id"))
nosort = mapped_column(Integer)
class Flow(Base):
__tablename__ = "flow"
flowid = Column(String(100), index=True, nullable=False)
appid = Column(String(100), index=True, nullable=False)
eventid = Column(String(100), index=True, nullable=False)
domainurl = Column(String(200))
name = Column(String(200))
content = Column(String)
createuserid = Column(Integer,ForeignKey("user.id"))
updateuserid = Column(Integer,ForeignKey("user.id"))
flowid = mapped_column(String(100), index=True, nullable=False)
appid = mapped_column(String(100), index=True, nullable=False)
eventid = mapped_column(String(100), index=True, nullable=False)
domainurl = mapped_column(String(200))
name = mapped_column(String(200))
content = mapped_column(String)
createuserid = mapped_column(Integer,ForeignKey("user.id"))
updateuserid = mapped_column(Integer,ForeignKey("user.id"))
createuser = relationship('User',foreign_keys=[createuserid])
updateuser = relationship('User',foreign_keys=[updateuserid])
class FlowHistory(Base):
__tablename__ = "flowhistory"
flowid = Column(String(100), index=True, nullable=False)
appid = Column(String(100), index=True, nullable=False)
eventid = Column(String(100), index=True, nullable=False)
domainurl = Column(String(200))
name = Column(String(200))
content = Column(String)
version = Column(Integer)
createuserid = Column(Integer,ForeignKey("user.id"))
updateuserid = Column(Integer,ForeignKey("user.id"))
flowid = mapped_column(String(100), index=True, nullable=False)
appid = mapped_column(String(100), index=True, nullable=False)
eventid = mapped_column(String(100), index=True, nullable=False)
domainurl = mapped_column(String(200))
name = mapped_column(String(200))
content = mapped_column(String)
version = mapped_column(Integer)
createuserid = mapped_column(Integer,ForeignKey("user.id"))
updateuserid = mapped_column(Integer,ForeignKey("user.id"))
createuser = relationship('User',foreign_keys=[createuserid])
updateuser = relationship('User',foreign_keys=[updateuserid])
class Tenant(Base):
__tablename__ = "tenant"
tenantid = Column(String(100), index=True, nullable=False)
name = Column(String(200))
licence = Column(String(200))
startdate = Column(DateTime)
enddate = Column(DateTime)
tenantid = mapped_column(String(100), index=True, nullable=False)
name = mapped_column(String(200))
licence = mapped_column(String(200))
startdate = mapped_column(DateTime)
enddate = mapped_column(DateTime)
class Domain(Base):
__tablename__ = "domain"
tenantid = Column(String(100), index=True, nullable=False)
name = Column(String(100), nullable=False)
url = Column(String(200), nullable=False)
kintoneuser = Column(String(100), nullable=False)
kintonepwd = Column(String(100), nullable=False)
is_active = Column(Boolean, default=True)
tenantid = mapped_column(String(100), index=True, nullable=False)
name = mapped_column(String(100), nullable=False)
url = mapped_column(String(200), nullable=False)
kintoneuser = mapped_column(String(100), nullable=False)
kintonepwd = mapped_column(String(100), nullable=False)
is_active = mapped_column(Boolean, default=True)
def decrypt_kintonepwd(self):
decrypted_pwd = chacha20Decrypt(self.kintonepwd)
return decrypted_pwd
createuserid = Column(Integer,ForeignKey("user.id"))
updateuserid = Column(Integer,ForeignKey("user.id"))
ownerid = Column(Integer,ForeignKey("user.id"))
createuserid = mapped_column(Integer,ForeignKey("user.id"))
updateuserid = mapped_column(Integer,ForeignKey("user.id"))
ownerid = mapped_column(Integer,ForeignKey("user.id"))
createuser = relationship('User',foreign_keys=[createuserid])
updateuser = relationship('User',foreign_keys=[updateuserid])
owner = relationship('User',foreign_keys=[ownerid])
@@ -173,11 +173,11 @@ class Domain(Base):
class UserDomain(Base):
__tablename__ = "userdomain"
userid = Column(Integer,ForeignKey("user.id"))
domainid = Column(Integer,ForeignKey("domain.id"))
is_default = Column(Boolean, default=False)
createuserid = Column(Integer,ForeignKey("user.id"))
updateuserid = Column(Integer,ForeignKey("user.id"))
userid = mapped_column(Integer,ForeignKey("user.id"))
domainid = mapped_column(Integer,ForeignKey("domain.id"))
is_default = mapped_column(Boolean, default=False)
createuserid = mapped_column(Integer,ForeignKey("user.id"))
updateuserid = mapped_column(Integer,ForeignKey("user.id"))
domain = relationship("Domain")
user = relationship("User",foreign_keys=[userid])
createuser = relationship('User',foreign_keys=[createuserid])
@@ -186,51 +186,51 @@ class UserDomain(Base):
class Event(Base):
__tablename__ = "event"
category = Column(String(100), nullable=False)
type = Column(String(100), nullable=False)
eventid= Column(String(100), nullable=False)
function = Column(String(500), nullable=False)
mobile = Column(Boolean, default=False)
eventgroup = Column(Boolean, default=False)
category = mapped_column(String(100), nullable=False)
type = mapped_column(String(100), nullable=False)
eventid= mapped_column(String(100), nullable=False)
function = mapped_column(String(500), nullable=False)
mobile = mapped_column(Boolean, default=False)
eventgroup = mapped_column(Boolean, default=False)
class EventAction(Base):
__tablename__ = "eventaction"
eventid = Column(String(100),ForeignKey("event.eventid"))
actionid = Column(Integer,ForeignKey("action.id"))
eventid = mapped_column(String(100),ForeignKey("event.eventid"))
actionid = mapped_column(Integer,ForeignKey("action.id"))
class ErrorLog(Base):
__tablename__ = "errorlog"
title = Column(String(50))
location = Column(String(500))
content = Column(String(5000))
title = mapped_column(String(50))
location = mapped_column(String(500))
content = mapped_column(String(5000))
class OperationLog(Base):
__tablename__ = "operationlog"
tenantid = Column(String(100))
domainurl = Column(String(200))
userid = Column(Integer,ForeignKey("user.id"))
operation = Column(String(200))
function = Column(String(200))
detail = Column(String(200))
tenantid = mapped_column(String(100))
domainurl = mapped_column(String(200))
userid = mapped_column(Integer,ForeignKey("user.id"))
operation = mapped_column(String(200))
function = mapped_column(String(200))
detail = mapped_column(String(200))
user = relationship('User')
class KintoneFormat(Base):
__tablename__ = "kintoneformat"
name = Column(String(50))
startrow =Column(Integer)
startcolumn =Column(Integer)
typecolumn =Column(Integer)
codecolumn =Column(Integer)
field = Column(String(5000))
trueformat = Column(String(10))
name = mapped_column(String(50))
startrow =mapped_column(Integer)
startmapped_column =mapped_column(Integer)
typemapped_column =mapped_column(Integer)
codemapped_column =mapped_column(Integer)
field = mapped_column(String(5000))
trueformat = mapped_column(String(10))
class Category(Base):
__tablename__ = "category"
categoryname = Column(String(20))
nosort = Column(Integer)
categoryname = mapped_column(String(20))
nosort = mapped_column(Integer)