From 5dce05816f9de911b352ef5f44d7c303c4f6e0e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 22 Mar 2022 16:29:02 +0000 Subject: [PATCH] minor DB refactoring --- api/kiwi_vpn_api/db/models.py | 8 +++-- api/kiwi_vpn_api/db/schemas.py | 59 ++++++++++++++++------------------ 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/api/kiwi_vpn_api/db/models.py b/api/kiwi_vpn_api/db/models.py index 8a7968d..9d3949e 100644 --- a/api/kiwi_vpn_api/db/models.py +++ b/api/kiwi_vpn_api/db/models.py @@ -21,7 +21,8 @@ class User(ORMBaseModel): password = Column(String) capabilities = relationship("UserCapability", lazy="joined") - certificates = relationship("Certificate", lazy="joined") + certificates = relationship("Certificate", lazy="select") + distinguished_names = relationship("DistinguishedName", lazy="select") @classmethod def load(cls, db: Session, name: str) -> User | None: @@ -52,6 +53,7 @@ class DistinguishedName(ORMBaseModel): id = Column(Integer, primary_key=True, autoincrement=True) + owner_name = Column(String, ForeignKey("users.name")) cn_only = Column(Boolean, default=True) country = Column(String(2)) state = Column(String) @@ -61,8 +63,6 @@ class DistinguishedName(ORMBaseModel): email = Column(String) common_name = Column(String) - certificates = relationship("Certificate", lazy="joined") - UniqueConstraint( country, state, @@ -82,3 +82,5 @@ class Certificate(ORMBaseModel): owner_name = Column(String, ForeignKey("users.name")) dn_id = Column(Integer, ForeignKey("distinguished_names.id")) expiry = Column(DateTime, default=datetime.datetime.now) + + distinguished_name = relationship("DistinguishedName", lazy="joined") diff --git a/api/kiwi_vpn_api/db/schemas.py b/api/kiwi_vpn_api/db/schemas.py index 935d7d5..3f7e5b5 100644 --- a/api/kiwi_vpn_api/db/schemas.py +++ b/api/kiwi_vpn_api/db/schemas.py @@ -16,6 +16,30 @@ from sqlalchemy.orm import Session 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 ########## @@ -26,12 +50,11 @@ class CertificateBase(BaseModel): class CertificateCreate(CertificateBase): - owner_name: str - dn_id: int + pass class Certificate(CertificateBase): - id: int + distinguished_name: DistinguishedName class Config: orm_mode = True @@ -76,8 +99,9 @@ class UserCreate(UserBase): class User(UserBase): - certificates: list[Certificate] = [] capabilities: list[UserCapability] = [] + distinguished_names: list[DistinguishedName] = [] + certificates: list[Certificate] = [] class Config: orm_mode = True @@ -178,30 +202,3 @@ class User(UserBase): ) 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