92 lines
3.1 KiB
Python
92 lines
3.1 KiB
Python
from rest_framework.decorators import api_view
|
|
from rest_framework.response import Response
|
|
from rest_framework import status
|
|
from django.http import HttpResponse
|
|
from django.conf import settings
|
|
from ..projects.models import Project
|
|
from .utils import generate_card_png
|
|
import zipfile
|
|
import io
|
|
import os
|
|
|
|
|
|
def _all_card_keys(project):
|
|
"""生成所有 54 张牌的 key 列表"""
|
|
keys = []
|
|
for suit in ['spade', 'heart', 'club', 'diamond']:
|
|
for rank in ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']:
|
|
keys.append(f"{suit}-{rank}")
|
|
keys.append('joker-big')
|
|
keys.append('joker-small')
|
|
if project.export_include_back:
|
|
keys.append('back')
|
|
return keys
|
|
|
|
|
|
@api_view(['POST'])
|
|
def export_project(request, pk):
|
|
"""批量导出整副牌为 ZIP"""
|
|
try:
|
|
project = Project.objects.get(pk=pk)
|
|
except Project.DoesNotExist:
|
|
return Response({'error': 'Project not found'}, status=status.HTTP_404_NOT_FOUND)
|
|
|
|
resolution = request.data.get('resolution', 'standard')
|
|
cards_filter = request.data.get('cards', 'all')
|
|
|
|
if cards_filter == 'all':
|
|
cards = _all_card_keys(project)
|
|
else:
|
|
cards = cards_filter if isinstance(cards_filter, list) else [cards_filter]
|
|
|
|
zip_buffer = io.BytesIO()
|
|
failed = []
|
|
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
|
for card_key in cards:
|
|
try:
|
|
png = generate_card_png(project, card_key, resolution)
|
|
img_buffer = io.BytesIO()
|
|
png.save(img_buffer, format='PNG')
|
|
img_buffer.seek(0)
|
|
zip_file.writestr(f"{card_key}.png", img_buffer.getvalue())
|
|
except Exception as e:
|
|
failed.append({'card': card_key, 'error': str(e)})
|
|
continue
|
|
|
|
zip_buffer.seek(0)
|
|
|
|
export_dir = os.path.join(settings.MEDIA_ROOT, 'export', str(project.id))
|
|
os.makedirs(export_dir, exist_ok=True)
|
|
zip_path = os.path.join(export_dir, 'cards.zip')
|
|
with open(zip_path, 'wb') as f:
|
|
f.write(zip_buffer.getvalue())
|
|
|
|
download_url = f"{settings.MEDIA_URL}export/{project.id}/cards.zip"
|
|
return Response({
|
|
'download_url': download_url,
|
|
'card_count': len(cards),
|
|
'failed': failed,
|
|
})
|
|
|
|
|
|
@api_view(['GET'])
|
|
def export_single_card(request, pk, card_key):
|
|
"""导出单张牌 PNG"""
|
|
try:
|
|
project = Project.objects.get(pk=pk)
|
|
except Project.DoesNotExist:
|
|
return Response({'error': 'Project not found'}, status=status.HTTP_404_NOT_FOUND)
|
|
|
|
resolution = request.query_params.get('resolution', 'standard')
|
|
|
|
try:
|
|
png = generate_card_png(project, card_key, resolution)
|
|
img_buffer = io.BytesIO()
|
|
png.save(img_buffer, format='PNG')
|
|
img_buffer.seek(0)
|
|
response = HttpResponse(img_buffer, content_type='image/png')
|
|
response['Content-Disposition'] = f'attachment; filename="{card_key}.png"'
|
|
return response
|
|
except Exception as e:
|
|
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|