Changeset e75910d in flexograder for builder/media_items.py
- Timestamp:
- 11/22/25 17:46:21 (4 months ago)
- Branches:
- fake-data, main, master
- Children:
- 858a4dc
- Parents:
- 0792ddd
- File:
-
- 1 edited
-
builder/media_items.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
builder/media_items.py
r0792ddd re75910d 5 5 from datetime import datetime 6 6 from dataclasses import dataclass, field 7 from flexoentity import FlexOID, EntityType, EntityState, FlexoEntity 7 from flexoentity import FlexOID, EntityType, EntityState, FlexoEntity, logger 8 8 9 9 @dataclass … … 16 16 caption: str = "" 17 17 author: str = "" 18 created_at: datetime = field(default_factory=datetime.utcnow)19 18 ExportPrefix = "media/" 20 19 … … 36 35 return "file" 37 36 37 @property 38 def type(self) -> str: 39 # logical type used for JSON, defaults to mtype 40 return self.mtype 41 38 42 @classmethod 39 43 def default(cls): … … 43 47 super().__post_init__() 44 48 49 @property 45 50 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}" 52 52 53 53 def to_dict(self): … … 57 57 "title": self.title, 58 58 "caption": self.caption, 59 "type": self. mtype,59 "type": self.type, 60 60 }) 61 61 return base … … 63 63 @classmethod 64 64 def from_dict(cls, data): 65 flexo_id = FlexOID(data["flexo_id"]) if "flexo_id" in data else None 66 65 67 obj = cls( 68 flexo_id=flexo_id, 66 69 src=data.get("src", ""), 67 70 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, 69 77 ) 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 73 79 return obj 74 80 75 81 def to_html(self): 76 82 raise NotImplementedError … … 122 128 return f'<a href="{prefix + self.src}" download>{self.label}</a>' 123 129 124 125 130 @dataclass 126 131 class NullMediaItem(MediaItem): 127 132 133 def __post_init__(self): 134 # Null objects bypass FlexOEntity lifecycle: no identity 135 self.flexo_id = None 136 self.fingerprint = None 137 128 138 @classmethod 129 139 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) 132 141 133 def __post_init__(self):134 super().__post_init__()142 def to_dict(self): 143 return {} 135 144 136 145 @property … … 141 150 return prefix 142 151 143 def to_dict(self): 144 return {} 152 def media_factory(m: dict) -> MediaItem: 145 153 146 # FIXME: We should really check if all attributes are initialized correctly147 148 def media_factory(m: dict) -> MediaItem:149 154 mtype = m.get("type", "").lower() 150 src = m.get("src", "")151 domain = m.get("domain", "GEN")152 label = m.get("label", None)153 155 154 156 cls = { … … 156 158 "audio": AudioItem, 157 159 "video": VideoItem, 158 "download": DownloadItem 160 "download": DownloadItem, 159 161 }.get(mtype, NullMediaItem) 160 162 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.
