add route to download html template

This commit is contained in:
2025-10-25 22:14:18 -03:00
parent 29ecb87e45
commit f284b64c60
6 changed files with 42 additions and 10 deletions

View File

@@ -3,9 +3,9 @@ from datetime import datetime
from http import HTTPStatus
from io import BytesIO
from typing import Annotated, Any
from urllib.parse import urlparse
import requests
from aws_lambda_powertools import Logger
from aws_lambda_powertools.event_handler.api_gateway import Response, Router
from aws_lambda_powertools.event_handler.exceptions import (
BadRequestError,
@@ -21,7 +21,6 @@ from boto3clients import dynamodb_client, s3_client
from config import BUCKET_NAME, COURSE_TABLE, PAPERFORGE_API
from form_data import parse
logger = Logger(__name__)
router = Router()
dyn = DynamoDBPersistenceLayer(COURSE_TABLE, dynamodb_client)
@@ -134,6 +133,25 @@ def sample(course_id: str, s3_uri: Annotated[str, Body(embed=True)]):
)
@router.post('/<course_id>/template')
def template(course_id: str, s3_uri: Annotated[str, Body(embed=True)]):
parsed = urlparse(s3_uri)
bucket = parsed.netloc
object_key = parsed.path.lstrip('/')
r = s3_client.get_object(Bucket=bucket, Key=object_key)
return Response(
body=r['Body'].read(),
content_type='text/html',
status_code=HTTPStatus.OK,
headers={
'Content-Disposition': f'attachment; filename="{course_id}.html"',
},
)
def _datefmt(dt: datetime) -> str:
months = [
'Janeiro',