from rest_framework.decorators import api_view from rest_framework.response import Response from rest_framework import status from .models import CardTemplate @api_view(['GET']) def template_list(request): """获取所有模板列表""" templates = CardTemplate.objects.all() data = [] for template in templates: data.append({ 'id': template.id, 'name': template.name, 'description': template.description, 'preview_image': template.preview_image.url if template.preview_image else None, 'colors': { 'spade': template.color_spade, 'heart': template.color_heart, 'club': template.color_club, 'diamond': template.color_diamond, 'background': template.color_background, }, }) 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) data = { 'id': template.id, 'name': template.name, 'description': template.description, 'preview_image': template.preview_image.url if template.preview_image else None, 'colors': { 'spade': template.color_spade, 'heart': template.color_heart, 'club': template.color_club, 'diamond': template.color_diamond, 'background': template.color_background, }, 'default_assets': template.default_assets, 'suit_symbols': { ss.suit_name: { 'svg_path': ss.svg_path, 'color': ss.color, } for ss in template.suit_symbols.all() } } return Response(data)