73 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
海报内容数据模型
"""
from dataclasses import dataclass, field
from typing import List, Optional
from PIL import Image
@dataclass
class PosterContent:
"""
海报内容模型
Attributes:
title: 主标题 (必填)
subtitle: 副标题
price: 价格 ( "¥199")
price_suffix: 价格后缀 ( "/人", "/晚")
tags: 标签列表 ( ["周末游", "亲子"])
highlights: 亮点标签 ( ["环境好", "出片"])
features: 特色列表 ( ["海景房", "含早餐"])
details: 详情列表 ( ["距地铁5分钟", "可带宠物"])
label: 角标/徽章 ( "精选推荐")
emoji: emoji符号 ( "🍰")
image: 背景图片 (PIL Image对象)
"""
title: str
subtitle: str = ""
price: str = ""
price_suffix: str = ""
tags: List[str] = field(default_factory=list)
highlights: List[str] = field(default_factory=list)
features: List[str] = field(default_factory=list)
details: List[str] = field(default_factory=list)
label: str = ""
emoji: str = ""
image: Optional[Image.Image] = None
@classmethod
def from_dict(cls, data: dict) -> 'PosterContent':
"""从字典创建内容对象"""
return cls(
title=data.get('title', ''),
subtitle=data.get('subtitle', ''),
price=data.get('price', ''),
price_suffix=data.get('price_suffix', ''),
tags=data.get('tags', []),
highlights=data.get('highlights', []),
features=data.get('features', []),
details=data.get('details', []),
label=data.get('label', ''),
emoji=data.get('emoji', ''),
image=data.get('image'),
)
def to_dict(self) -> dict:
"""转换为字典 (不含image)"""
return {
'title': self.title,
'subtitle': self.subtitle,
'price': self.price,
'price_suffix': self.price_suffix,
'tags': self.tags,
'highlights': self.highlights,
'features': self.features,
'details': self.details,
'label': self.label,
'emoji': self.emoji,
}