Full Working static multi tenant
This commit is contained in:
@@ -1,47 +1,42 @@
|
||||
from beanie import PydanticObjectId
|
||||
from fastapi import HTTPException, Depends
|
||||
|
||||
from ..core.routes import get_crud_router
|
||||
from ..core.routes import get_crud_router, get_logged_tenant_db_cursor
|
||||
from ..user.manager import get_current_user
|
||||
|
||||
from .models import ContractDraft, ContractDraftStatus
|
||||
from .models import ContractDraft, ContractDraftStatus, ContractDraftFilters
|
||||
from .schemas import ContractDraftCreate, ContractDraftRead, ContractDraftUpdate
|
||||
|
||||
draft_router = get_crud_router(ContractDraft, ContractDraftCreate, ContractDraftRead, ContractDraftUpdate)
|
||||
draft_router = get_crud_router(ContractDraft, ContractDraftCreate, ContractDraftRead, ContractDraftUpdate, ContractDraftFilters)
|
||||
|
||||
del(draft_router.routes[0])
|
||||
del(draft_router.routes[2])
|
||||
del(draft_router.routes[3]) #update route
|
||||
del(draft_router.routes[1]) #post route
|
||||
|
||||
|
||||
@draft_router.post("/", response_description="Contract Draft added to the database")
|
||||
async def create(item: ContractDraftCreate, user=Depends(get_current_user)) -> dict:
|
||||
await item.validate_foreign_key()
|
||||
o = await ContractDraft(**item.dict()).create()
|
||||
await o.check_is_ready()
|
||||
async def create(schema: ContractDraftCreate, db=Depends(get_logged_tenant_db_cursor)) -> ContractDraftRead:
|
||||
await schema.validate_foreign_key(db)
|
||||
record = await ContractDraft.create(db, schema)
|
||||
await record.check_is_ready(db)
|
||||
|
||||
return {"message": "Contract Draft added successfully", "id": o.id}
|
||||
return ContractDraftRead.from_model(record)
|
||||
|
||||
|
||||
@draft_router.put("/{id}", response_description="Contract Draft record updated")
|
||||
async def update(id: PydanticObjectId, req: ContractDraftUpdate, user=Depends(get_current_user)) -> ContractDraftRead:
|
||||
req = {k: v for k, v in req.dict().items() if v is not None}
|
||||
update_query = {"$set": {
|
||||
field: value for field, value in req.items()
|
||||
}}
|
||||
|
||||
item = await ContractDraft.get(id)
|
||||
if not item:
|
||||
@draft_router.put("/{record_id}", response_description="Contract Draft record updated")
|
||||
async def update(record_id: PydanticObjectId, schema: ContractDraftUpdate, db=Depends(get_logged_tenant_db_cursor)) -> ContractDraftRead:
|
||||
record = await ContractDraft.get(db, record_id)
|
||||
if not record:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Contract Draft record not found!"
|
||||
)
|
||||
if item.status == ContractDraftStatus.published:
|
||||
if record.status == ContractDraftStatus.published:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Contract Draft has already been published"
|
||||
)
|
||||
|
||||
await item.update(update_query)
|
||||
await item.check_is_ready()
|
||||
record = await ContractDraft.update(db, record, schema)
|
||||
await record.check_is_ready(db)
|
||||
|
||||
return ContractDraftRead(**item.dict())
|
||||
return ContractDraftRead.from_model(record)
|
||||
|
||||
Reference in New Issue
Block a user