Switching to the registry paradigm
This commit is contained in:
@@ -4,31 +4,48 @@ from hub.auth import get_current_user
|
||||
from firm.db import get_db_client
|
||||
from firm.current_firm import CurrentFirmModel
|
||||
|
||||
async def get_tenant_db_cursor(instance: str, firm: str, db_client=Depends(get_db_client)):
|
||||
db_cursor = db_client[f"tenant_{instance}_{firm}"]
|
||||
current_firm = await CurrentFirmModel.get(db_cursor)
|
||||
if current_firm is None:
|
||||
raise HTTPException(status_code=405, detail=f"Firm needs to be instantiated first")
|
||||
db_cursor.firm = current_firm
|
||||
return db_cursor
|
||||
class Registry:
|
||||
user = None
|
||||
|
||||
def get_logged_tenant_db_cursor(db_cursor=Depends(get_tenant_db_cursor), user=Depends(get_current_user)):
|
||||
for firm in user.firms:
|
||||
if firm == db_cursor.firm:
|
||||
db_cursor.user = user
|
||||
return db_cursor
|
||||
def __init__(self, db_client, instance, firm):
|
||||
self.db = db_client[f"tenant_{instance}_{firm}"]
|
||||
self.instance = instance
|
||||
self.firm = firm
|
||||
|
||||
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
||||
self.current_firm = CurrentFirmModel.get(self.db)
|
||||
|
||||
async def get_uninitialized_tenant_db_cursor(instance: str, firm: str, db_client=Depends(get_db_client), user=Depends(get_current_user)):
|
||||
db_cursor = db_client[f"tenant_{instance}_{firm}"]
|
||||
current_firm = await CurrentFirmModel.get(db_cursor)
|
||||
if current_firm is not None:
|
||||
def set_user(self, user):
|
||||
for firm in user.firms:
|
||||
if firm.instance == self.instance and firm.firm == firm:
|
||||
self.user = user
|
||||
self.db.user = user
|
||||
return
|
||||
|
||||
raise PermissionError
|
||||
|
||||
async def get_tenant_registry(instance: str, firm: str, db_client=Depends(get_db_client)) -> Registry:
|
||||
registry = Registry(db_client, instance, firm)
|
||||
if await registry.current_firm is None:
|
||||
raise HTTPException(status_code=405, detail=f"Firm needs to be initialized first")
|
||||
|
||||
return registry
|
||||
|
||||
def get_authed_tenant_registry(registry=Depends(get_tenant_registry), user=Depends(get_current_user)) -> Registry:
|
||||
try:
|
||||
registry.set_user(user)
|
||||
except PermissionError:
|
||||
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
||||
|
||||
return registry
|
||||
|
||||
async def get_uninitialized_registry(instance: str, firm: str, db_client=Depends(get_db_client), user=Depends(get_current_user)) -> Registry:
|
||||
registry = Registry(db_client, instance, firm)
|
||||
if await registry.current_firm is not None:
|
||||
HTTPException(status_code=409, detail="Firm configuration already exists")
|
||||
|
||||
for firm in user.firms:
|
||||
if firm == db_cursor.firm:
|
||||
db_cursor.user = user
|
||||
return db_cursor
|
||||
try:
|
||||
registry.set_user(user)
|
||||
except PermissionError:
|
||||
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
||||
|
||||
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
||||
return registry
|
||||
|
||||
@@ -5,7 +5,7 @@ from fastapi_filter import FilterDepends
|
||||
from fastapi_pagination import Page, add_pagination
|
||||
from fastapi_pagination.ext.motor import paginate
|
||||
|
||||
from firm.core.depends import get_logged_tenant_db_cursor
|
||||
from firm.core.depends import get_authed_tenant_registry
|
||||
from firm.core.models import CrudDocument
|
||||
from firm.core.schemas import Writer, Reader
|
||||
|
||||
@@ -14,18 +14,18 @@ def get_crud_router(model: CrudDocument, model_create: Writer, model_read: Reade
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/", response_model=Page[model_read], response_description=f"{model_name} records retrieved")
|
||||
async def read_list(filters: model_filter=FilterDepends(model_filter), db=Depends(get_logged_tenant_db_cursor)) -> Page[model_read]:
|
||||
return await paginate(**model.find(db, filters))
|
||||
async def read_list(filters: model_filter=FilterDepends(model_filter), reg=Depends(get_authed_tenant_registry)) -> Page[model_read]:
|
||||
return await paginate(**model.find(reg.db, filters))
|
||||
|
||||
@router.post("/", response_description=f"{model_name} added to the database")
|
||||
async def create(schema: model_create, db=Depends(get_logged_tenant_db_cursor)) -> model_read:
|
||||
await schema.validate_foreign_key(db)
|
||||
record = await model.create(db, schema)
|
||||
async def create(schema: model_create, reg=Depends(get_authed_tenant_registry)) -> model_read:
|
||||
await schema.validate_foreign_key(reg.db)
|
||||
record = await model.create(reg.db, schema)
|
||||
return model_read.validate_model(record)
|
||||
|
||||
@router.get("/{record_id}", response_description=f"{model_name} record retrieved")
|
||||
async def read_one(record_id: PydanticObjectId, db=Depends(get_logged_tenant_db_cursor)) -> model_read:
|
||||
record = await model.get(db, record_id)
|
||||
async def read_one(record_id: PydanticObjectId, reg=Depends(get_authed_tenant_registry)) -> model_read:
|
||||
record = await model.get(reg.db, record_id)
|
||||
if not record:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
@@ -35,27 +35,27 @@ def get_crud_router(model: CrudDocument, model_create: Writer, model_read: Reade
|
||||
return model_read.from_model(record)
|
||||
|
||||
@router.put("/{record_id}", response_description=f"{model_name} record updated")
|
||||
async def update(record_id: PydanticObjectId, schema: model_update, db=Depends(get_logged_tenant_db_cursor)) -> model_read:
|
||||
record = await model.get(db, record_id)
|
||||
async def update(record_id: PydanticObjectId, schema: model_update, reg=Depends(get_authed_tenant_registry)) -> model_read:
|
||||
record = await model.get(reg.db, record_id)
|
||||
if not record:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"{model_name} record not found!"
|
||||
)
|
||||
|
||||
record = await model.update(db, record, schema)
|
||||
record = await model.update(reg.db, record, schema)
|
||||
return model_read.from_model(record)
|
||||
|
||||
@router.delete("/{record_id}", response_description=f"{model_name} record deleted from the database")
|
||||
async def delete(record_id: PydanticObjectId, db=Depends(get_logged_tenant_db_cursor)) -> dict:
|
||||
record = await model.get(db, record_id)
|
||||
async def delete(record_id: PydanticObjectId, reg=Depends(get_authed_tenant_registry)) -> dict:
|
||||
record = await model.get(reg.db, record_id)
|
||||
if not record:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"{model_name} record not found!"
|
||||
)
|
||||
|
||||
await model.delete(db, record)
|
||||
await model.delete(reg.db, record)
|
||||
return {
|
||||
"message": f"{model_name} deleted successfully"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user