add route to download html template
This commit is contained in:
@@ -3,9 +3,9 @@ from datetime import datetime
|
|||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import Annotated, Any
|
from typing import Annotated, Any
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import requests
|
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.api_gateway import Response, Router
|
||||||
from aws_lambda_powertools.event_handler.exceptions import (
|
from aws_lambda_powertools.event_handler.exceptions import (
|
||||||
BadRequestError,
|
BadRequestError,
|
||||||
@@ -21,7 +21,6 @@ from boto3clients import dynamodb_client, s3_client
|
|||||||
from config import BUCKET_NAME, COURSE_TABLE, PAPERFORGE_API
|
from config import BUCKET_NAME, COURSE_TABLE, PAPERFORGE_API
|
||||||
from form_data import parse
|
from form_data import parse
|
||||||
|
|
||||||
logger = Logger(__name__)
|
|
||||||
router = Router()
|
router = Router()
|
||||||
dyn = DynamoDBPersistenceLayer(COURSE_TABLE, dynamodb_client)
|
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:
|
def _datefmt(dt: datetime) -> str:
|
||||||
months = [
|
months = [
|
||||||
'Janeiro',
|
'Janeiro',
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
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 Router
|
||||||
from aws_lambda_powertools.event_handler.exceptions import (
|
from aws_lambda_powertools.event_handler.exceptions import (
|
||||||
NotFoundError,
|
NotFoundError,
|
||||||
@@ -8,7 +7,6 @@ from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
|||||||
from boto3clients import dynamodb_client
|
from boto3clients import dynamodb_client
|
||||||
from config import ORDER_TABLE
|
from config import ORDER_TABLE
|
||||||
|
|
||||||
logger = Logger(__name__)
|
|
||||||
router = Router()
|
router = Router()
|
||||||
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
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 Router
|
||||||
from aws_lambda_powertools.event_handler.exceptions import (
|
from aws_lambda_powertools.event_handler.exceptions import (
|
||||||
NotFoundError,
|
NotFoundError,
|
||||||
@@ -13,7 +12,6 @@ from .orgs import router as orgs
|
|||||||
|
|
||||||
__all__ = ['emails', 'orgs']
|
__all__ = ['emails', 'orgs']
|
||||||
|
|
||||||
logger = Logger(__name__)
|
|
||||||
router = Router()
|
router = Router()
|
||||||
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
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 Router
|
||||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||||
|
|
||||||
from boto3clients import dynamodb_client
|
from boto3clients import dynamodb_client
|
||||||
from config import USER_TABLE
|
from config import USER_TABLE
|
||||||
|
|
||||||
logger = Logger(__name__)
|
|
||||||
router = Router()
|
router = Router()
|
||||||
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
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 Router
|
||||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||||
|
|
||||||
from boto3clients import dynamodb_client
|
from boto3clients import dynamodb_client
|
||||||
from config import USER_TABLE
|
from config import USER_TABLE
|
||||||
|
|
||||||
logger = Logger(__name__)
|
|
||||||
router = Router()
|
router = Router()
|
||||||
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||||
|
|
||||||
@@ -13,9 +11,11 @@ dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
|||||||
@router.get('/<user_id>/orgs')
|
@router.get('/<user_id>/orgs')
|
||||||
def get_orgs(user_id: str):
|
def get_orgs(user_id: str):
|
||||||
start_key = router.current_event.get_query_string_value('start_key', None)
|
start_key = router.current_event.get_query_string_value('start_key', None)
|
||||||
|
limit = int(router.current_event.get_query_string_value('limit', '25'))
|
||||||
|
|
||||||
return dyn.collection.query(
|
return dyn.collection.query(
|
||||||
# Post-migration (users): rename `orgs` to `ORG`
|
# Post-migration (users): rename `orgs` to `ORG`
|
||||||
key=KeyPair(user_id, 'orgs#'),
|
key=KeyPair(user_id, 'orgs#'),
|
||||||
start_key=start_key,
|
start_key=start_key,
|
||||||
|
limit=limit,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -82,3 +82,23 @@ def test_sample(
|
|||||||
lambda_context,
|
lambda_context,
|
||||||
)
|
)
|
||||||
assert r['statusCode'] == HTTPStatus.OK
|
assert r['statusCode'] == HTTPStatus.OK
|
||||||
|
|
||||||
|
|
||||||
|
def test_template(
|
||||||
|
app,
|
||||||
|
seeds,
|
||||||
|
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||||
|
http_api_proxy: HttpApiProxy,
|
||||||
|
lambda_context: LambdaContext,
|
||||||
|
):
|
||||||
|
r = app.lambda_handler(
|
||||||
|
http_api_proxy(
|
||||||
|
raw_path='/courses/2a8963fc-4694-4fe2-953a-316d1b10f1f5/template',
|
||||||
|
method=HTTPMethod.POST,
|
||||||
|
body={
|
||||||
|
's3_uri': 's3://saladeaula.digital/certs/samples/cipa-grau-de-risco-1.html',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
lambda_context,
|
||||||
|
)
|
||||||
|
assert r['statusCode'] == HTTPStatus.OK
|
||||||
|
|||||||
Reference in New Issue
Block a user