eliminate db.DB

This commit is contained in:
Jörn-Michael Miehe 2022-03-16 16:25:03 +00:00
parent e9785f0076
commit f89faf2d44
3 changed files with 11 additions and 12 deletions

View file

@ -8,15 +8,13 @@ from peewee import (BooleanField, CharField, DatabaseProxy, DateTimeField,
from .config import CRYPT_CONTEXT from .config import CRYPT_CONTEXT
DB = DatabaseProxy()
class ORMBaseModel(Model):
class BaseModel(Model):
class Meta: class Meta:
database = DB database = DatabaseProxy()
class User(BaseModel): class User(ORMBaseModel):
name = CharField(unique=True) name = CharField(unique=True)
password = CharField() password = CharField()
@ -36,12 +34,12 @@ class User(BaseModel):
return False return False
class UserCapability(BaseModel): class UserCapability(ORMBaseModel):
user = ForeignKeyField(User, backref="capabilities") user = ForeignKeyField(User, backref="capabilities")
capability = CharField() capability = CharField()
class DistinguishedName(BaseModel): class DistinguishedName(ORMBaseModel):
cn_only = BooleanField(default=True) cn_only = BooleanField(default=True)
common_name = CharField() common_name = CharField()
email = CharField() email = CharField()
@ -59,7 +57,7 @@ class DistinguishedName(BaseModel):
) )
class Certificate(BaseModel): class Certificate(ORMBaseModel):
owner = ForeignKeyField(User, backref="certs") owner = ForeignKeyField(User, backref="certs")
distinguished_name = ForeignKeyField(DistinguishedName) distinguished_name = ForeignKeyField(DistinguishedName)
expiry = DateTimeField(default=datetime.datetime.now) expiry = DateTimeField(default=datetime.datetime.now)

View file

@ -5,7 +5,7 @@ from fastapi import Depends, FastAPI
from peewee import Database from peewee import Database
from .config import PRODUCTION_MODE from .config import PRODUCTION_MODE
from .db import DB from .db import ORMBaseModel
from .routers import auth, install, user from .routers import auth, install, user
api = FastAPI( api = FastAPI(
@ -32,7 +32,7 @@ api.include_router(user.router)
async def api_startup( async def api_startup(
db: Database = Depends(install.connect_db) db: Database = Depends(install.connect_db)
): ):
DB.initialize(db) ORMBaseModel._meta.database.initialize(db)
app = FastAPI() app = FastAPI()
app.mount("/api", api) app.mount("/api", api)

View file

@ -6,7 +6,8 @@ from fastapi import APIRouter, Depends, HTTPException, status
from peewee import Database from peewee import Database
from ..config import BaseConfig from ..config import BaseConfig
from ..db import DB, Certificate, DistinguishedName, User, UserCapability from ..db import (Certificate, DistinguishedName, ORMBaseModel, User,
UserCapability)
router = APIRouter(prefix="/install") router = APIRouter(prefix="/install")
@ -79,7 +80,7 @@ async def set_config(
if config.jwt.secret is None: if config.jwt.secret is None:
config.jwt.secret = token_hex(32) config.jwt.secret = token_hex(32)
DB.initialize(await connect_db(config)) ORMBaseModel._meta.database.initialize(await connect_db(config))
with open(CONFIG_FILE, "w") as kv: with open(CONFIG_FILE, "w") as kv:
kv.write(config.json(indent=2)) kv.write(config.json(indent=2))