Files
romm/backend/utils/assets.py
zurdi ecaa883400 refactor(v2): apply the simplify pass
The folding rule was written three times in two phrasings: as a filter,
as its own inverse inside the auto-open check, and as an arithmetic
difference for the chevron count. A slot band now partitions into
`pinned` and `hidden` once where it is built, so the rule has one home
and the filter stops re-running per render.

Also:
- A band carries its newest asset instead of the id, so ranking no
  longer scans the band to find it again inside a sort comparator.
- `HIDDEN_ASSET_ANNOTATIONS` derives from `AssetAnnotations`, so a third
  annotation field cannot be added without also being masked.
- `normalize_asset_labels` keys the dedupe off a dict, folding each label
  once, and rejects an oversized set as soon as it crosses the cap
  instead of after walking the whole body.
- The clear button's absolute placement moved onto the stage that wants
  it, so the inline variant no longer un-sets it.
- Two duplicated label ternaries became computeds.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016bZ7R3xC73MZu1vTNuatPK
2026-09-23 00:16:32 +02:00

30 lines
1.1 KiB
Python

from fastapi import HTTPException, status
from models.assets import ASSET_LABEL_MAX_LENGTH, ASSET_LABELS_MAX
def normalize_asset_labels(labels: list[str]) -> list[str]:
"""Trim, drop blanks and de-duplicate case-insensitively, keeping order.
Raises:
HTTPException: if a label is too long or there are too many of them.
"""
# Keyed by the folded form, so the first spelling of a label wins.
cleaned: dict[str, str] = {}
for label in labels:
trimmed = label.strip()
if not trimmed:
continue
if len(trimmed) > ASSET_LABEL_MAX_LENGTH:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
detail=f"Labels cannot exceed {ASSET_LABEL_MAX_LENGTH} characters",
)
cleaned.setdefault(trimmed.casefold(), trimmed)
if len(cleaned) > ASSET_LABELS_MAX:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
detail=f"An asset cannot carry more than {ASSET_LABELS_MAX} labels",
)
return list(cleaned.values())