update layercake

This commit is contained in:
2025-10-06 23:16:42 -03:00
parent 942c038744
commit 161b75db8d
13 changed files with 95 additions and 24 deletions

View File

@@ -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()

View File

@@ -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

View 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

View File

@@ -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_,
},