ariculratherapy #24
|
|
@ -370,6 +370,39 @@ class ContentService:
|
|||
for item, hydrated_data in zip(items, hydrated_payloads, strict=True):
|
||||
item.data = hydrated_data
|
||||
|
||||
# Inherit media from referenced content items when direct media is absent
|
||||
items_needing_media: list[
|
||||
tuple[ModuleTemplateContentDetailItem, list[tuple[UUID, str | None]]]
|
||||
] = []
|
||||
all_ref_ids: set[UUID] = set()
|
||||
for item in items:
|
||||
if not item.media:
|
||||
refs: list[tuple[UUID, str | None]] = []
|
||||
self._extract_referenced_content_items(item.data, refs)
|
||||
if refs:
|
||||
items_needing_media.append((item, refs))
|
||||
all_ref_ids.update(cid for cid, _ in refs)
|
||||
|
||||
if items_needing_media and all_ref_ids:
|
||||
ref_media_by_content = (
|
||||
await self.repository.get_content_media_for_content_ids(
|
||||
session=self.session,
|
||||
content_ids=list(all_ref_ids),
|
||||
)
|
||||
)
|
||||
for item, refs in items_needing_media:
|
||||
inherited: list[ContentMediaResponse] = []
|
||||
for cid, label in refs:
|
||||
for row in ref_media_by_content.get(cid, []):
|
||||
row_copy = dict(row)
|
||||
if label:
|
||||
row_copy["caption"] = label
|
||||
row_copy["alt_text"] = label
|
||||
row_copy["media_role"] = "gallery"
|
||||
row_copy["sort_order"] = len(inherited)
|
||||
inherited.append(self._media_response(row_copy))
|
||||
item.media = inherited
|
||||
|
||||
return ModuleTemplateContentDetailResponse(
|
||||
module=ModuleSummaryResponse.model_validate(module),
|
||||
template=self._template_summary_response(template),
|
||||
|
|
@ -581,6 +614,30 @@ class ContentService:
|
|||
content_id=content_id,
|
||||
)
|
||||
|
||||
if not media_rows:
|
||||
referenced_items: list[tuple[UUID, str | None]] = []
|
||||
self._extract_referenced_content_items(dynamic_data, referenced_items)
|
||||
if referenced_items:
|
||||
ref_ids = [cid for cid, _ in referenced_items]
|
||||
media_by_content = (
|
||||
await self.repository.get_content_media_for_content_ids(
|
||||
session=self.session,
|
||||
content_ids=ref_ids,
|
||||
)
|
||||
)
|
||||
inherited_rows: list[dict[str, Any]] = []
|
||||
for cid, label in referenced_items:
|
||||
rows = media_by_content.get(cid, [])
|
||||
for r in rows:
|
||||
row_copy = dict(r)
|
||||
if label:
|
||||
row_copy["caption"] = label
|
||||
row_copy["alt_text"] = label
|
||||
row_copy["media_role"] = "gallery"
|
||||
row_copy["sort_order"] = len(inherited_rows)
|
||||
inherited_rows.append(row_copy)
|
||||
media_rows = inherited_rows
|
||||
|
||||
response = ContentDetailResponse(
|
||||
module=ModuleSummaryResponse.model_validate(module),
|
||||
content=ContentItemResponse.model_validate(
|
||||
|
|
@ -1155,7 +1212,7 @@ class ContentService:
|
|||
)
|
||||
if (
|
||||
derived_module_code is not None
|
||||
and derived_module_code != module.code
|
||||
and derived_module_code.lower() != module.code.lower()
|
||||
):
|
||||
# Prevent a caller from using a template from M0002 against M0001
|
||||
# even if content associations have been accidentally corrupted.
|
||||
|
|
@ -1274,6 +1331,43 @@ class ContentService:
|
|||
for payload in payloads
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def _extract_referenced_content_items(
|
||||
cls,
|
||||
value: Any,
|
||||
output: list[tuple[UUID, str | None]],
|
||||
seen_ids: set[UUID] | None = None,
|
||||
) -> None:
|
||||
"""Recursively collect (content_id, label) tuples from all content_ref objects in order."""
|
||||
if seen_ids is None:
|
||||
seen_ids = set()
|
||||
|
||||
if isinstance(value, list):
|
||||
for item in value:
|
||||
cls._extract_referenced_content_items(item, output, seen_ids)
|
||||
return
|
||||
|
||||
if not isinstance(value, dict):
|
||||
return
|
||||
|
||||
if value.get("type") == "content_ref":
|
||||
target = value.get("target")
|
||||
raw_id = target.get("content_id") if isinstance(target, dict) else None
|
||||
if raw_id:
|
||||
try:
|
||||
cid = UUID(str(raw_id))
|
||||
if cid not in seen_ids:
|
||||
seen_ids.add(cid)
|
||||
label = value.get("label") or (
|
||||
target.get("source_key") if isinstance(target, dict) else None
|
||||
)
|
||||
output.append((cid, str(label) if label else None))
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
for child in value.values():
|
||||
cls._extract_referenced_content_items(child, output, seen_ids)
|
||||
|
||||
@classmethod
|
||||
def _collect_content_reference_lookups(
|
||||
cls,
|
||||
|
|
|
|||
Loading…
Reference in New Issue