From 74858130ef6c6ebc49ead1670d431a565397fe0f Mon Sep 17 00:00:00 2001 From: David Date: Tue, 28 May 2019 14:27:51 +0800 Subject: [PATCH] add exceptions --- utils/views.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/utils/views.py b/utils/views.py index 36843e5..0fa2eb9 100644 --- a/utils/views.py +++ b/utils/views.py @@ -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")