add download sample
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from http import HTTPStatus
|
||||
from io import BytesIO
|
||||
from typing import Any
|
||||
from typing import Annotated, Any
|
||||
|
||||
import requests
|
||||
from aws_lambda_powertools import Logger
|
||||
from aws_lambda_powertools.event_handler.api_gateway import Router
|
||||
from aws_lambda_powertools.event_handler.api_gateway import Response, Router
|
||||
from aws_lambda_powertools.event_handler.exceptions import (
|
||||
BadRequestError,
|
||||
NotFoundError,
|
||||
)
|
||||
from aws_lambda_powertools.event_handler.openapi.params import Body
|
||||
from layercake.dateutils import now
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||
from pydantic import UUID4, BaseModel
|
||||
|
||||
from api_gateway import JSONResponse
|
||||
from boto3clients import dynamodb_client, s3_client
|
||||
from config import BUCKET_NAME, COURSE_TABLE
|
||||
from config import BUCKET_NAME, COURSE_TABLE, PAPERFORGE_API
|
||||
from form_data import parse
|
||||
|
||||
logger = Logger(__name__)
|
||||
@@ -89,3 +93,55 @@ def edit_course(course_id: str):
|
||||
)
|
||||
|
||||
return JSONResponse(HTTPStatus.NO_CONTENT)
|
||||
|
||||
|
||||
@router.post('/<course_id>/sample')
|
||||
def sample(course_id: str, s3_uri: Annotated[str, Body(embed=True)]):
|
||||
now_ = now()
|
||||
# Send template URI and data to Paperforge API to generate a PDF
|
||||
r = requests.post(
|
||||
PAPERFORGE_API,
|
||||
data=json.dumps(
|
||||
{
|
||||
'template_uri': s3_uri,
|
||||
'args': {
|
||||
'name': 'Juscelino Kubitschek',
|
||||
'cpf': '***.810.132-**',
|
||||
'score': 100,
|
||||
'started_at': now_.strftime('%d/%m/%Y'),
|
||||
'completed_at': now_.strftime('%d/%m/%Y'),
|
||||
'today': _datefmt(now_),
|
||||
'year': now_.strftime('%Y'),
|
||||
'expires_at': now_.strftime('%d/%m/%Y'),
|
||||
},
|
||||
},
|
||||
),
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
||||
return Response(
|
||||
body=r.content,
|
||||
content_type='application/pdf',
|
||||
status_code=HTTPStatus.OK,
|
||||
headers={
|
||||
'Content-Disposition': f'attachment; filename="{course_id}_sample.pdf"',
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _datefmt(dt: datetime) -> str:
|
||||
months = [
|
||||
'Janeiro',
|
||||
'Fevereiro',
|
||||
'Março',
|
||||
'Abril',
|
||||
'Maio',
|
||||
'Junho',
|
||||
'Julho',
|
||||
'Agosto',
|
||||
'Setembro',
|
||||
'Outubro',
|
||||
'Novembro',
|
||||
'Dezembro',
|
||||
]
|
||||
return f'{dt.day:02d} de {months[dt.month - 1]} de {dt.year}'
|
||||
|
||||
Reference in New Issue
Block a user