Changeset e75910d in flexograder for builder/media_items.py


Ignore:
Timestamp:
11/22/25 17:46:21 (4 months ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
fake-data, main, master
Children:
858a4dc
Parents:
0792ddd
Message:

all tests green, new json format and domain handling added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • builder/media_items.py

    r0792ddd re75910d  
    55from datetime import datetime
    66from dataclasses import dataclass, field
    7 from flexoentity import FlexOID, EntityType, EntityState, FlexoEntity
     7from flexoentity import FlexOID, EntityType, EntityState, FlexoEntity, logger
    88
    99@dataclass
     
    1616    caption: str = ""
    1717    author: str = ""
    18     created_at: datetime = field(default_factory=datetime.utcnow)
    1918    ExportPrefix = "media/"
    2019
     
    3635        return "file"
    3736
     37    @property
     38    def type(self) -> str:
     39        # logical type used for JSON, defaults to mtype
     40        return self.mtype
     41
    3842    @classmethod
    3943    def default(cls):
     
    4347        super().__post_init__()
    4448
     49    @property
    4550    def text_seed(self) -> str:
    46         try:
    47             content_hash = hashlib.blake2s(Path(self.src).read_bytes()).hexdigest()[:12]
    48         except FileNotFoundError:
    49             content_hash = "MISSING"
    50 
    51         return f"{self.src}|{content_hash}|{self.title}|{self.caption}|{self.mtype}"
     51        return f"{self.src}|{self.title}|{self.caption}|{self.mtype}"
    5252
    5353    def to_dict(self):
     
    5757            "title": self.title,
    5858            "caption": self.caption,
    59             "type": self.mtype,
     59            "type": self.type,
    6060        })
    6161        return base
     
    6363    @classmethod
    6464    def from_dict(cls, data):
     65        flexo_id = FlexOID(data["flexo_id"]) if "flexo_id" in data else None
     66
    6567        obj = cls(
     68            flexo_id=flexo_id,
    6669            src=data.get("src", ""),
    6770            title=data.get("title", ""),
    68             caption=data.get("caption", "")
     71            caption=data.get("caption", ""),
     72            author=data.get("author", ""),
     73            fingerprint=data.get("fingerprint"),
     74            state=EntityState(data["state"]) if "state" in data else EntityState(flexo_id.state_code),
     75            version=data.get("version"),
     76            _in_factory=True,
    6977        )
    70         obj.flexo_id = FlexOID.from_string(data["flexo_id"]) if "flexo_id" in data else None
    71         obj.state = data.get("state", "DRAFT")
    72         obj.version = data.get("version", 1)
     78
    7379        return obj
    74    
     80    
    7581    def to_html(self):
    7682        raise NotImplementedError
     
    122128        return f'<a href="{prefix + self.src}" download>{self.label}</a>'
    123129
    124 
    125130@dataclass
    126131class NullMediaItem(MediaItem):
    127132
     133    def __post_init__(self):
     134        # Null objects bypass FlexOEntity lifecycle: no identity
     135        self.flexo_id = None
     136        self.fingerprint = None
     137
    128138    @classmethod
    129139    def default(cls):
    130         """Return a canonical default NullMediaItem instance."""
    131         return cls.with_domain_id(domain_id="NULL_MEDIA")
     140        return cls(_in_factory=True)
    132141
    133     def __post_init__(self):
    134         super().__post_init__()
     142    def to_dict(self):
     143        return {}
    135144
    136145    @property
     
    141150        return prefix
    142151
    143     def to_dict(self):
    144         return {}
     152def media_factory(m: dict) -> MediaItem:
    145153
    146 # FIXME: We should really check if all attributes are initialized correctly
    147 
    148 def media_factory(m: dict) -> MediaItem:
    149154    mtype = m.get("type", "").lower()
    150     src = m.get("src", "")
    151     domain = m.get("domain", "GEN")
    152     label = m.get("label", None)
    153155
    154156    cls = {
     
    156158        "audio": AudioItem,
    157159        "video": VideoItem,
    158         "download": DownloadItem
     160        "download": DownloadItem,
    159161    }.get(mtype, NullMediaItem)
    160162
    161     m = cls.with_domain_id(domain, src=src) if cls is not NullMediaItem else NullMediaItem.default()
    162     return m
     163    if cls is NullMediaItem:
     164        return NullMediaItem.default()
     165
     166    # Prefer from_dict if a FlexOID is provided
     167    if "flexo_id" in m:
     168        return cls.from_dict(m)
     169
     170    # Otherwise, generate a new entity
     171    domain_id = m.get("domain_id", "GEN")
     172    return cls.with_domain_id(domain_id, src=m.get("src", ""))
Note: See TracChangeset for help on using the changeset viewer.