update layercake
This commit is contained in:
@@ -5,8 +5,10 @@ import boto3
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from mypy_boto3_dynamodb.client import DynamoDBClient
|
||||
from mypy_boto3_s3 import S3Client
|
||||
else:
|
||||
DynamoDBClient = object
|
||||
S3Client = object
|
||||
|
||||
|
||||
def get_dynamodb_client() -> DynamoDBClient:
|
||||
@@ -18,4 +20,5 @@ def get_dynamodb_client() -> DynamoDBClient:
|
||||
return boto3.client('dynamodb', endpoint_url=f'http://{host}:8000')
|
||||
|
||||
|
||||
s3_client: S3Client = boto3.client('s3')
|
||||
dynamodb_client: DynamoDBClient = get_dynamodb_client()
|
||||
|
||||
@@ -3,3 +3,5 @@ import os
|
||||
USER_TABLE: str = os.getenv('USER_TABLE') # type: ignore
|
||||
ENROLLMENT_TABLE: str = os.getenv('ENROLLMENT_TABLE') # type: ignore
|
||||
COURSE_TABLE: str = os.getenv('COURSE_TABLE') # type: ignore
|
||||
|
||||
BUCKET_NAME: str = os.getenv('BUCKET_NAME') # type: ignore
|
||||
|
||||
33
api.saladeaula.digital/app/form_data.py
Normal file
33
api.saladeaula.digital/app/form_data.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from io import BytesIO
|
||||
from typing import Any
|
||||
|
||||
from python_multipart import parse_form
|
||||
|
||||
|
||||
def parse(
|
||||
headers: dict[str, Any],
|
||||
body: BytesIO,
|
||||
) -> dict[str, Any]:
|
||||
ret = {}
|
||||
|
||||
def on_field(field):
|
||||
field_name = field.field_name.decode().split('.')
|
||||
|
||||
if len(field_name) > 1:
|
||||
key, sub = field_name
|
||||
|
||||
if key not in ret:
|
||||
ret[key] = {}
|
||||
|
||||
ret[key][sub] = field.value
|
||||
else:
|
||||
key, *_ = field_name
|
||||
ret[key] = field.value
|
||||
|
||||
def on_file(file):
|
||||
file.file_object.seek(0)
|
||||
ret[file.field_name.decode()] = file.file_object.read(-1)
|
||||
|
||||
parse_form(headers, body, on_field=on_field, on_file=on_file)
|
||||
|
||||
return ret
|
||||
@@ -13,8 +13,8 @@ from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||
from pydantic import UUID4, BaseModel
|
||||
|
||||
from api_gateway import JSONResponse
|
||||
from boto3clients import dynamodb_client
|
||||
from config import COURSE_TABLE
|
||||
from boto3clients import dynamodb_client, s3_client
|
||||
from config import BUCKET_NAME, COURSE_TABLE
|
||||
from form_data import parse
|
||||
|
||||
logger = Logger(__name__)
|
||||
@@ -32,21 +32,18 @@ def get_course(course_id: str):
|
||||
|
||||
class Cert(BaseModel):
|
||||
exp_interval: int | None = None
|
||||
rawfile: bytes | None = None
|
||||
s3_uri: str | None = None
|
||||
|
||||
def model_dump(self, **kwargs) -> dict[str, Any]:
|
||||
return super().model_dump(
|
||||
exclude={'rawfile'},
|
||||
exclude_none=True,
|
||||
**kwargs,
|
||||
)
|
||||
return super().model_dump(exclude_none=True, **kwargs)
|
||||
|
||||
|
||||
class Course(BaseModel):
|
||||
id: UUID4
|
||||
name: str
|
||||
access_period: int
|
||||
cert: Cert | None = None
|
||||
cert: Cert
|
||||
rawfile: bytes | None = None
|
||||
|
||||
|
||||
@router.put('/<course_id>')
|
||||
@@ -58,10 +55,21 @@ def edit_course(course_id: str):
|
||||
|
||||
body = BytesIO(event.decoded_body.encode())
|
||||
course = Course.model_validate(
|
||||
{'id': course_id} | parse(event.headers, body),
|
||||
{'id': course_id, 'cert': {}} | parse(event.headers, body),
|
||||
)
|
||||
now_ = now()
|
||||
|
||||
if course.rawfile:
|
||||
object_key = f'certs/{course_id}.html'
|
||||
course.cert.s3_uri = f's3://{BUCKET_NAME}/{object_key}'
|
||||
|
||||
s3_client.put_object(
|
||||
Bucket=BUCKET_NAME,
|
||||
Key=object_key,
|
||||
Body=course.rawfile,
|
||||
ContentType='text/html',
|
||||
)
|
||||
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.update(
|
||||
key=KeyPair(str(course.id), '0'),
|
||||
@@ -72,7 +80,7 @@ def edit_course(course_id: str):
|
||||
},
|
||||
expr_attr_values={
|
||||
':name': course.name,
|
||||
':cert': course.cert.model_dump() if course.cert else None,
|
||||
':cert': course.cert.model_dump(),
|
||||
':access_period': course.access_period,
|
||||
':updated_at': now_,
|
||||
},
|
||||
|
||||
@@ -12,7 +12,6 @@ dev = [
|
||||
"jsonlines>=4.0.0",
|
||||
"pytest>=8.3.4",
|
||||
"pytest-cov>=6.0.0",
|
||||
"python-multipart>=0.0.20",
|
||||
"requests-toolbelt>=1.0.0",
|
||||
"ruff>=0.9.1",
|
||||
"sqlite-utils>=3.38",
|
||||
|
||||
@@ -8,6 +8,9 @@ Parameters:
|
||||
EnrollmentTable:
|
||||
Type: String
|
||||
Default: betaeducacao-prod-enrollments
|
||||
BucketName:
|
||||
Type: String
|
||||
Default: saladeaula.digital
|
||||
|
||||
Globals:
|
||||
Function:
|
||||
@@ -17,7 +20,7 @@ Globals:
|
||||
Architectures:
|
||||
- x86_64
|
||||
Layers:
|
||||
- !Sub arn:aws:lambda:sa-east-1:336641857101:layer:layercake:96
|
||||
- !Sub arn:aws:lambda:sa-east-1:336641857101:layer:layercake:97
|
||||
Environment:
|
||||
Variables:
|
||||
TZ: America/Sao_Paulo
|
||||
@@ -27,6 +30,7 @@ Globals:
|
||||
DYNAMODB_PARTITION_KEY: id
|
||||
COURSE_TABLE: !Ref CourseTable
|
||||
ENROLLMENT_TABLE: !Ref EnrollmentTable
|
||||
BUCKET_NAME: !Ref BucketName
|
||||
|
||||
Resources:
|
||||
HttpLog:
|
||||
@@ -42,7 +46,7 @@ Resources:
|
||||
AllowMethods: [GET, POST, PUT, DELETE, PATCH, OPTIONS]
|
||||
AllowHeaders: [Content-Type, X-Requested-With, Authorization]
|
||||
AllowCredentials: false
|
||||
MaxAge: 600
|
||||
MaxAge: 600 # 10 minutes
|
||||
Auth:
|
||||
DefaultAuthorizer: OAuth2Authorizer
|
||||
Authorizers:
|
||||
@@ -66,6 +70,8 @@ Resources:
|
||||
TableName: !Ref CourseTable
|
||||
- DynamoDBCrudPolicy:
|
||||
TableName: !Ref EnrollmentTable
|
||||
- S3WritePolicy:
|
||||
BucketName: !Ref BucketName
|
||||
Events:
|
||||
Preflight:
|
||||
Type: HttpApi
|
||||
|
||||
@@ -17,6 +17,7 @@ SK = 'sk'
|
||||
def pytest_configure():
|
||||
os.environ['TZ'] = 'America/Sao_Paulo'
|
||||
os.environ['COURSE_TABLE'] = PYTEST_TABLE_NAME
|
||||
os.environ['BUCKET_NAME'] = 'saladeaula.digital'
|
||||
os.environ['DYNAMODB_PARTITION_KEY'] = PK
|
||||
os.environ['DYNAMODB_SORT_KEY'] = SK
|
||||
|
||||
|
||||
@@ -37,8 +37,7 @@ def test_edit_course(
|
||||
'given_cert': 'true',
|
||||
'name': 'pytest updated from test',
|
||||
'access_period': '365',
|
||||
'cert.exp_interval': '360',
|
||||
'cert.rawfile': f,
|
||||
'rawfile': ('sample.html', f, 'text/html'),
|
||||
}
|
||||
)
|
||||
r = app.lambda_handler(
|
||||
@@ -59,4 +58,7 @@ def test_edit_course(
|
||||
key={'id': course_id, 'sk': '0'},
|
||||
)
|
||||
|
||||
print(r)
|
||||
assert (
|
||||
r['cert']['s3_uri']
|
||||
== 's3://saladeaula.digital/certs/2a8963fc-4694-4fe2-953a-316d1b10f1f5.html'
|
||||
)
|
||||
|
||||
6
api.saladeaula.digital/uv.lock
generated
6
api.saladeaula.digital/uv.lock
generated
@@ -25,7 +25,6 @@ dev = [
|
||||
{ name = "jsonlines" },
|
||||
{ name = "pytest" },
|
||||
{ name = "pytest-cov" },
|
||||
{ name = "python-multipart" },
|
||||
{ name = "requests-toolbelt" },
|
||||
{ name = "ruff" },
|
||||
{ name = "sqlite-utils" },
|
||||
@@ -41,7 +40,6 @@ dev = [
|
||||
{ name = "jsonlines", specifier = ">=4.0.0" },
|
||||
{ name = "pytest", specifier = ">=8.3.4" },
|
||||
{ name = "pytest-cov", specifier = ">=6.0.0" },
|
||||
{ name = "python-multipart", specifier = ">=0.0.20" },
|
||||
{ name = "requests-toolbelt", specifier = ">=1.0.0" },
|
||||
{ name = "ruff", specifier = ">=0.9.1" },
|
||||
{ name = "sqlite-utils", specifier = ">=3.38" },
|
||||
@@ -594,7 +592,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "layercake"
|
||||
version = "0.9.14"
|
||||
version = "0.10.0"
|
||||
source = { directory = "../layercake" }
|
||||
dependencies = [
|
||||
{ name = "arnparse" },
|
||||
@@ -611,6 +609,7 @@ dependencies = [
|
||||
{ name = "pycpfcnpj" },
|
||||
{ name = "pydantic", extra = ["email"] },
|
||||
{ name = "pydantic-extra-types" },
|
||||
{ name = "python-multipart" },
|
||||
{ name = "pytz" },
|
||||
{ name = "requests" },
|
||||
{ name = "smart-open", extra = ["s3"] },
|
||||
@@ -634,6 +633,7 @@ requires-dist = [
|
||||
{ name = "pycpfcnpj", specifier = ">=1.8" },
|
||||
{ name = "pydantic", extras = ["email"], specifier = ">=2.10.6" },
|
||||
{ name = "pydantic-extra-types", specifier = ">=2.10.3" },
|
||||
{ name = "python-multipart", specifier = ">=0.0.20" },
|
||||
{ name = "pytz", specifier = ">=2025.1" },
|
||||
{ name = "requests", specifier = ">=2.32.3" },
|
||||
{ name = "smart-open", extras = ["s3"], specifier = ">=7.1.0" },
|
||||
|
||||
Reference in New Issue
Block a user