baodan/api/insurance/models/poster_template_model.py

62 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""海报模板模型AI 生图方案)。"""
from sqlalchemy import Column, String, Text, SmallInteger, BigInteger, TIMESTAMP, func
from insurance.db.compat import db
class PosterTemplate(db.Model):
"""海报模板表AI 生图方案)。"""
__tablename__ = "poster_templates"
id = Column(BigInteger, primary_key=True, autoincrement=True)
name = Column(String(100), nullable=False, comment="模板名称")
scenario_tag = Column(String(50), nullable=True, comment="场景标签")
style_description = Column(Text, nullable=False, comment="AI 生图风格描述 prompt 片段")
color_scheme = Column(Text, nullable=True, comment="配色方案 JSON")
reference_image = Column(String(500), nullable=True, comment="参考图地址")
preview_image = Column(String(500), nullable=True, comment="预览图地址")
layout_key = Column(String(50), default="legacy", comment="确定性画布布局标识")
supported_modes = Column(Text, default='["single","long"]', comment="兼容输出模式 JSON")
supported_formats = Column(Text, default='["single_2_3","single_9_16","long_1242_auto"]', comment="兼容格式 JSON")
status = Column(SmallInteger, default=1, comment="1=启用, 0=停用")
created_at = Column(TIMESTAMP, server_default=func.now())
updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())
def to_dict(self):
import json
def _json_list(value, fallback):
try:
parsed = json.loads(value) if value else fallback
return parsed if isinstance(parsed, list) else fallback
except (json.JSONDecodeError, TypeError):
return fallback
color_scheme = None
if self.color_scheme:
try:
color_scheme = json.loads(self.color_scheme)
except (json.JSONDecodeError, TypeError):
color_scheme = None
return {
"id": self.id,
"name": self.name,
"scenarioTag": self.scenario_tag,
"styleDescription": self.style_description,
"colorScheme": color_scheme,
"referenceImage": self.reference_image,
"previewImage": self.preview_image,
"layoutKey": self.layout_key or "legacy",
"supportedModes": _json_list(self.supported_modes, ["single", "long"]),
"supportedFormats": _json_list(
self.supported_formats,
["single_2_3", "single_9_16", "long_1242_auto"],
),
"status": self.status,
"createdAt": self.created_at.isoformat() if self.created_at else None,
"updatedAt": self.updated_at.isoformat() if self.updated_at else None,
}
def supports(self, output_mode: str, format_id: str) -> bool:
data = self.to_dict()
return output_mode in data["supportedModes"] and format_id in data["supportedFormats"]