经典 → 古典宫廷(王子/皇后/国王/小丑) 现代 → 现代人物(小孩/女青年/男青年/小丑鱼) 卡通 → 现代人物 + 暖色调 + 圆边框 复古 → 简笔符号 + 深色边框 + 米色背景 后端: - CardTemplate 新增 theme_id(绑预设主题)+ design_override(背景/边框/字体等覆盖) - 新增 apply_template_to_project():把 LibraryAsset 复制到项目素材 + 写 design - 创建项目时支持传 template_id,自动套用整套预设 - 模板列表 API 附加 library 预览(4 张图缩略) 前端 Home.vue: - 4 套模板卡片每张带 4 张缩略图(来自 library 预览) - 点模板一键创建项目 + 跳转到编辑器 - '新建空白项目' 保留为独立按钮 init_system 同步:4 套模板配置 + 应用到示例项目
59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
from rest_framework.decorators import api_view
|
|
from rest_framework.response import Response
|
|
from rest_framework import status
|
|
from .models import CardTemplate
|
|
from apps.projects.models import LibraryAsset
|
|
|
|
|
|
def _template_to_dict(t):
|
|
return {
|
|
'id': t.id,
|
|
'name': t.name,
|
|
'description': t.description,
|
|
'preview_image': t.preview_image.url if t.preview_image else None,
|
|
'theme_id': t.theme_id,
|
|
'colors': {
|
|
'spade': t.color_spade,
|
|
'heart': t.color_heart,
|
|
'club': t.color_club,
|
|
'diamond': t.color_diamond,
|
|
'background': t.color_background,
|
|
},
|
|
'design_override': t.design_override or {},
|
|
}
|
|
|
|
|
|
def _template_with_preview(t):
|
|
"""附加 library 中 theme_id 对应的素材预览(前端可以画一组小图)"""
|
|
out = _template_to_dict(t)
|
|
libs = LibraryAsset.objects.filter(theme_id=t.theme_id)
|
|
out['library'] = [
|
|
{
|
|
'id': lib.id,
|
|
'role': lib.role,
|
|
'role_name': lib.role_name,
|
|
'label': lib.label,
|
|
'file_url': f'/media/{lib.file_path}',
|
|
}
|
|
for lib in libs
|
|
]
|
|
return out
|
|
|
|
|
|
@api_view(['GET'])
|
|
def template_list(request):
|
|
"""获取所有模板列表(含每个模板的 theme 预览)"""
|
|
templates = CardTemplate.objects.all()
|
|
data = [_template_with_preview(t) for t in templates]
|
|
return Response(data)
|
|
|
|
|
|
@api_view(['GET'])
|
|
def template_detail(request, pk):
|
|
"""获取模板详情"""
|
|
try:
|
|
template = CardTemplate.objects.get(pk=pk)
|
|
except CardTemplate.DoesNotExist:
|
|
return Response({'error': 'Template not found'}, status=status.HTTP_404_NOT_FOUND)
|
|
return Response(_template_with_preview(template))
|