[WIP] Download contract
This commit is contained in:
@@ -81,6 +81,7 @@ async def create_contract(
|
||||
total_price = '{:10.2f}'.format(recurrent_price + compute_occasional_prices(occasionals))
|
||||
cheques = list(map(lambda x: {"name": x.name, "value": x.value}, new_contract.cheques))
|
||||
# TODO: send contract to referer
|
||||
|
||||
try:
|
||||
pdf_bytes = generate_html_contract(
|
||||
new_contract,
|
||||
@@ -92,6 +93,7 @@ async def create_contract(
|
||||
)
|
||||
pdf_file = io.BytesIO(pdf_bytes)
|
||||
contract_id = f'{new_contract.firstname}_{new_contract.lastname}_{new_contract.form.productor.type}_{new_contract.form.season}'
|
||||
service.add_contract_file(session, id, pdf_bytes)
|
||||
except:
|
||||
raise HTTPException(status_code=400, detail=PDFerrorOccured)
|
||||
return StreamingResponse(
|
||||
@@ -109,6 +111,23 @@ def get_contracts(
|
||||
):
|
||||
return service.get_all(session, forms)
|
||||
|
||||
@router.get('/{id}/file')
|
||||
def get_contract_file(
|
||||
id: int,
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
contract = service.get_one(session, id)
|
||||
print(contract.file)
|
||||
if contract is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
return StreamingResponse(
|
||||
contract.file,
|
||||
media_type='application/pdf',
|
||||
headers={
|
||||
'Content-Disposition': f'attachement; filename=contract_{contract.id}.pdf'
|
||||
}
|
||||
)
|
||||
|
||||
@router.get('/{id}', response_model=models.ContractPublic)
|
||||
def get_contract(id: int, session: Session = Depends(get_session)):
|
||||
result = service.get_one(session, id)
|
||||
|
||||
@@ -39,6 +39,17 @@ def create_one(session: Session, contract: models.ContractCreate) -> models.Cont
|
||||
session.refresh(new_contract)
|
||||
return new_contract
|
||||
|
||||
def add_contract_file(session: Session, id: int, file: bytes):
|
||||
statement = select(models.Contract).where(models.Contract.id == id)
|
||||
result = session.exec(statement)
|
||||
contract = result.first()
|
||||
|
||||
contract.file = file
|
||||
session.add(contract)
|
||||
session.commit()
|
||||
session.refresh(contract)
|
||||
return contract
|
||||
|
||||
def update_one(session: Session, id: int, contract: models.ContractUpdate) -> models.ContractPublic:
|
||||
statement = select(models.Contract).where(models.Contract.id == id)
|
||||
result = session.exec(statement)
|
||||
|
||||
Reference in New Issue
Block a user