Changeset 7d9089b in flexograder


Ignore:
Timestamp:
01/19/26 16:55:02 (2 months ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
fake-data, main, master
Children:
5739768
Parents:
e92c42d
Message:

fixes for media items, they now serialize as full FlexoEntities

Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • flexograder/core_entities/media_items.py

    re92c42d r7d9089b  
    11import mimetypes
    2 import hashlib
    3 from pathlib import Path
    42
    5 from datetime import datetime
    6 from dataclasses import dataclass, field
    7 from flexoentity import FlexOID, EntityType, EntityState, FlexoEntity, logger
     3from dataclasses import dataclass
     4from abc import ABC, abstractmethod
     5from flexoentity import EntityType, FlexoEntity, logger
    86
    97@dataclass
    10 class MediaItem(FlexoEntity):
     8class MediaItem(FlexoEntity, ABC):
    119
    1210    ENTITY_TYPE = EntityType.MEDIA
     
    2119    # Derived property
    2220    # ──────────────────────────────────────────────────────────────
     21
    2322    @property
     23    @abstractmethod
    2424    def mtype(self) -> str:
     25        """Semantic media type (image, audio, video, download)."""
     26        raise NotImplementedError
     27   
     28    @property
     29    def mime_category(self) -> str:
    2530        """Infer type from src path using MIME detection."""
    2631        mime, _ = mimetypes.guess_type(self.src)
     
    4651        return f"{self.src}|{self.title}|{self.caption}|{self.mtype}"
    4752
    48     def to_dict(self):
    49         base = super().to_dict()  # includes OID, state, version, etc.
    50         base.update({
     53    def _serialize_content(self) -> dict:
     54        content = super()._serialize_content() or {}
     55        content.update({
     56            "type": self.mtype,
    5157            "src": self.src,
    5258            "title": self.title,
    5359            "caption": self.caption,
    54             "type": self.mtype,
     60            "author": self.author
    5561        })
    56         return base
     62        return content
    5763
    58     @classmethod
    59     def from_dict(cls, data):
    60         flexo_id = FlexOID(data["flexo_id"]) if "flexo_id" in data else None
    61 
    62         obj = cls(
    63             flexo_id=flexo_id,
    64             src=data.get("src", ""),
    65             title=data.get("title", ""),
    66             caption=data.get("caption", ""),
    67             author=data.get("author", ""),
    68             fingerprint=data.get("fingerprint"),
    69             state=EntityState(data["state"]) if "state" in data else EntityState(flexo_id.state_code),
    70             version=data.get("version"),
    71             _in_factory=True,
    72         )
    73 
    74         return obj
     64    def _deserialize_content(self, content: dict):
     65        super()._deserialize_content(content)
     66        self.src = content.get("src", "")
     67        self.title = content.get("title", "")
     68        self.caption = content.get("caption", "")
     69        self.author = content.get("author", "")
    7570
    7671    def to_html(self):
     
    113108
    114109    @property
    115     def type(self):
     110    def mtype(self):
    116111        return "download"
    117112
Note: See TracChangeset for help on using the changeset viewer.