Adding contract creation
This commit is contained in:
@@ -1,9 +1,76 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from fastapi import Depends, HTTPException
|
||||
|
||||
from ..core.routes import get_crud_router
|
||||
from .routes_draft import draft_router
|
||||
from .print import print_router
|
||||
|
||||
contract_router = APIRouter()
|
||||
from .models import Contract, ContractDraft
|
||||
from .schemas import ContractCreate, ContractRead, ContractUpdate
|
||||
|
||||
from ..entity.models import Entity
|
||||
from ..template.models import ProvisionTemplate
|
||||
|
||||
contract_router = get_crud_router(Contract, ContractCreate, ContractRead, ContractUpdate)
|
||||
del(contract_router.routes[0])
|
||||
del(contract_router.routes[2])
|
||||
del(contract_router.routes[2])
|
||||
|
||||
contract_router.include_router(draft_router, prefix="/draft", tags=["draft"], )
|
||||
contract_router.include_router(print_router, prefix="/print", tags=["print"], )
|
||||
|
||||
|
||||
def can_create_contract():
|
||||
class User:
|
||||
entity_id = '63d127bcf355de8e65a193e1'
|
||||
return User()
|
||||
|
||||
|
||||
@contract_router.post("/", response_description="Contract Successfully created")
|
||||
async def create(item: ContractCreate, user=Depends(can_create_contract)) -> dict:
|
||||
await item.validate_foreign_key()
|
||||
|
||||
draft = await ContractDraft.get(item.draft_id)
|
||||
|
||||
for v in draft.variables:
|
||||
if not v.key or not v.value:
|
||||
raise HTTPException(status_code=400, detail="Variable {} is invalid".format(v))
|
||||
|
||||
contract_dict = item.dict()
|
||||
del(contract_dict['draft_id'])
|
||||
|
||||
contract_dict['lawyer'] = await Entity.get(user.entity_id)
|
||||
|
||||
contract_dict['name'] = draft.name
|
||||
contract_dict['title'] = draft.title
|
||||
parties = []
|
||||
for p in draft.parties:
|
||||
parties.append({
|
||||
'entity': await Entity.get(p.entity_id),
|
||||
'part': p.part,
|
||||
'representative': await Entity.get(p.representative_id) if p.representative_id else None
|
||||
})
|
||||
|
||||
contract_dict['parties'] = parties
|
||||
|
||||
provisions = []
|
||||
for p in draft.provisions:
|
||||
p = p.provision
|
||||
provision = await ProvisionTemplate.get(p.provision_template_id) if p.type == 'template' \
|
||||
else p
|
||||
|
||||
provisions.append({
|
||||
'title': replace_variables_in_value(draft.variables, provision.title),
|
||||
'body': replace_variables_in_value(draft.variables, provision.body)
|
||||
})
|
||||
|
||||
contract_dict['provisions'] = provisions
|
||||
|
||||
o = await Contract(**contract_dict).create()
|
||||
return {"message": "Contract Successfully created", "id": o.id}
|
||||
|
||||
|
||||
def replace_variables_in_value(variables, value: str):
|
||||
for v in variables:
|
||||
value = value.replace('%{}%'.format(v.key), v.value)
|
||||
return value
|
||||
|
||||
Reference in New Issue
Block a user