107 lines
3.6 KiB
Python
107 lines
3.6 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Literal
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
|
|
|
|
|
SUPPORTED_EXTENSIONS = {".webp", ".png", ".jpg", ".jpeg"}
|
|
SUPPORTED_MIME_TYPES = {"image/webp", "image/png", "image/jpeg"}
|
|
|
|
|
|
class MediaTarget(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
module_code: str = Field(pattern=r"^m\d{4}$")
|
|
template_code: str = Field(pattern=r"^m\d{4}_ct\d{4}$")
|
|
source_code: str = Field(min_length=1)
|
|
|
|
@field_validator("module_code", "template_code", mode="before")
|
|
@classmethod
|
|
def normalize_codes(cls, value: str) -> str:
|
|
if not isinstance(value, str):
|
|
return value
|
|
return value.strip().lower()
|
|
|
|
@field_validator("source_code")
|
|
@classmethod
|
|
def strip_source_code(cls, value: str) -> str:
|
|
value = value.strip()
|
|
if not value:
|
|
raise ValueError("source_code must not be empty")
|
|
return value
|
|
|
|
|
|
class MediaEntry(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
file: str = Field(min_length=1)
|
|
target: MediaTarget
|
|
media_role: Literal["primary", "gallery"] = "gallery"
|
|
usage_type: str = Field(min_length=1)
|
|
sort_order: int = Field(default=1, ge=1)
|
|
alt_text: str | None = None
|
|
caption: str | None = None
|
|
metadata: dict[str, Any] = Field(default_factory=dict)
|
|
|
|
@field_validator("usage_type")
|
|
@classmethod
|
|
def normalize_usage_type(cls, value: str) -> str:
|
|
value = value.strip().lower().replace("-", "_").replace(" ", "_")
|
|
if not value:
|
|
raise ValueError("usage_type must not be empty")
|
|
return value
|
|
|
|
|
|
class MediaManifest(BaseModel):
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
version: Literal["1.0"] = "1.0"
|
|
module_code: str = Field(pattern=r"^m\d{4}$")
|
|
media: list[MediaEntry] = Field(default_factory=list)
|
|
|
|
@field_validator("module_code", mode="before")
|
|
@classmethod
|
|
def normalize_module_code(cls, value: str) -> str:
|
|
if not isinstance(value, str):
|
|
return value
|
|
return value.strip().lower()
|
|
|
|
@model_validator(mode="after")
|
|
def validate_module_consistency(self) -> "MediaManifest":
|
|
for index, entry in enumerate(self.media):
|
|
if entry.target.module_code != self.module_code:
|
|
raise ValueError(
|
|
f"media[{index}] target.module_code={entry.target.module_code!r} "
|
|
f"does not match manifest module_code={self.module_code!r}"
|
|
)
|
|
|
|
primary_targets: dict[tuple[str, str], int] = {}
|
|
duplicate_keys: set[tuple[str, str, str, int]] = set()
|
|
|
|
for index, entry in enumerate(self.media):
|
|
target_key = (entry.target.template_code, entry.target.source_code)
|
|
|
|
if entry.media_role == "primary":
|
|
if target_key in primary_targets:
|
|
raise ValueError(
|
|
"Only one primary media entry is allowed per content item: "
|
|
f"{target_key}; entries {primary_targets[target_key]} and {index}"
|
|
)
|
|
primary_targets[target_key] = index
|
|
|
|
identity = (
|
|
entry.target.template_code,
|
|
entry.target.source_code,
|
|
entry.usage_type,
|
|
entry.sort_order,
|
|
)
|
|
if identity in duplicate_keys:
|
|
raise ValueError(
|
|
"Duplicate media association identity in manifest: "
|
|
f"{identity}"
|
|
)
|
|
duplicate_keys.add(identity)
|
|
|
|
return self
|