add exceptions

This commit is contained in:
David 2019-05-28 14:27:51 +08:00
parent bed03c9234
commit 74858130ef
1 changed files with 20 additions and 14 deletions

View File

@ -10,6 +10,7 @@ from django.http.response import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework import permissions
from rest_framework import views
from rest_framework.exceptions import APIException
from rest_framework.response import Response
from booking.models import Room
@ -40,7 +41,10 @@ class QRCodeAPI(views.APIView):
data = {}
for key in request.query_params:
data[key] = request.query_params[key]
stream = create_qrcode(data)
try:
stream = create_qrcode(data)
except Exception as e:
raise APIException(e)
return HttpResponse(stream.getvalue(), content_type="image/png")
@ -52,20 +56,22 @@ class DownloadAllQRCodeByRoomAPI(views.APIView):
room = get_object_or_404(Room, id=room_id)
seat_qrcode_stream_list = []
seat_name_list = []
for seat in room.seat_set.all():
data = {
'type': 'seat',
'name': seat.name,
'id': seat.id
}
seat_qrcode_stream_list.append(create_qrcode(data))
seat_name_list.append(seat.name)
zip = BytesIO()
with zipfile.ZipFile(zip, 'w', zipfile.ZIP_DEFLATED) as zf:
for index, file in enumerate(seat_qrcode_stream_list):
zf.writestr(f'座位-{seat_name_list[index]}.png', file.getvalue())
try:
for seat in room.seat_set.all():
data = {
'type': 'seat',
'name': seat.name,
'id': seat.id
}
seat_qrcode_stream_list.append(create_qrcode(data))
seat_name_list.append(seat.name)
zip = BytesIO()
with zipfile.ZipFile(zip, 'w', zipfile.ZIP_DEFLATED) as zf:
for index, file in enumerate(seat_qrcode_stream_list):
zf.writestr(f'座位-{seat_name_list[index]}.png', file.getvalue())
except Exception as e:
raise APIException(e)
return HttpResponse(zip.getvalue(), content_type="application/zip")