minor DB refactoring

This commit is contained in:
Jörn-Michael Miehe 2022-03-22 16:29:02 +00:00
parent 12d39bb923
commit 5dce05816f
2 changed files with 33 additions and 34 deletions

View file

@ -21,7 +21,8 @@ class User(ORMBaseModel):
password = Column(String) password = Column(String)
capabilities = relationship("UserCapability", lazy="joined") capabilities = relationship("UserCapability", lazy="joined")
certificates = relationship("Certificate", lazy="joined") certificates = relationship("Certificate", lazy="select")
distinguished_names = relationship("DistinguishedName", lazy="select")
@classmethod @classmethod
def load(cls, db: Session, name: str) -> User | None: def load(cls, db: Session, name: str) -> User | None:
@ -52,6 +53,7 @@ class DistinguishedName(ORMBaseModel):
id = Column(Integer, primary_key=True, autoincrement=True) id = Column(Integer, primary_key=True, autoincrement=True)
owner_name = Column(String, ForeignKey("users.name"))
cn_only = Column(Boolean, default=True) cn_only = Column(Boolean, default=True)
country = Column(String(2)) country = Column(String(2))
state = Column(String) state = Column(String)
@ -61,8 +63,6 @@ class DistinguishedName(ORMBaseModel):
email = Column(String) email = Column(String)
common_name = Column(String) common_name = Column(String)
certificates = relationship("Certificate", lazy="joined")
UniqueConstraint( UniqueConstraint(
country, country,
state, state,
@ -82,3 +82,5 @@ class Certificate(ORMBaseModel):
owner_name = Column(String, ForeignKey("users.name")) owner_name = Column(String, ForeignKey("users.name"))
dn_id = Column(Integer, ForeignKey("distinguished_names.id")) dn_id = Column(Integer, ForeignKey("distinguished_names.id"))
expiry = Column(DateTime, default=datetime.datetime.now) expiry = Column(DateTime, default=datetime.datetime.now)
distinguished_name = relationship("DistinguishedName", lazy="joined")

View file

@ -16,6 +16,30 @@ from sqlalchemy.orm import Session
from . import models from . import models
##########
# table: distinguished_names
##########
class DistinguishedNameBase(BaseModel):
cn_only: bool
country: str
state: str
city: str
organization: str
organizational_unit: str
email: str
common_name: str
class DistinguishedNameCreate(DistinguishedNameBase):
pass
class DistinguishedName(DistinguishedNameBase):
class Config:
orm_mode = True
########## ##########
# table: certificates # table: certificates
########## ##########
@ -26,12 +50,11 @@ class CertificateBase(BaseModel):
class CertificateCreate(CertificateBase): class CertificateCreate(CertificateBase):
owner_name: str pass
dn_id: int
class Certificate(CertificateBase): class Certificate(CertificateBase):
id: int distinguished_name: DistinguishedName
class Config: class Config:
orm_mode = True orm_mode = True
@ -76,8 +99,9 @@ class UserCreate(UserBase):
class User(UserBase): class User(UserBase):
certificates: list[Certificate] = []
capabilities: list[UserCapability] = [] capabilities: list[UserCapability] = []
distinguished_names: list[DistinguishedName] = []
certificates: list[Certificate] = []
class Config: class Config:
orm_mode = True orm_mode = True
@ -178,30 +202,3 @@ class User(UserBase):
) )
db.commit() db.commit()
##########
# table: distinguished_names
##########
class DistinguishedNameBase(BaseModel):
cn_only: bool
country: str
state: str
city: str
organization: str
organizational_unit: str
email: str
common_name: str
class DistinguishedNameCreate(DistinguishedNameBase):
pass
class DistinguishedName(DistinguishedNameBase):
id: int
certificates: list[Certificate]
class Config:
orm_mode = True