Improve photo upload error handling

This commit is contained in:
Tanner Collin 2022-01-21 04:36:58 +00:00
parent 08ab4b2924
commit 9e14fde11e

View File

@ -11,7 +11,7 @@ from rest_framework.views import exception_handler
from dateutil import relativedelta from dateutil import relativedelta
from uuid import uuid4 from uuid import uuid4
from PIL import Image, ImageDraw, ImageFont, ImageOps, JpegImagePlugin from PIL import Image, ImageDraw, ImageFont, ImageOps, JpegImagePlugin
#JpegImagePlugin._getmp = lambda x: None JpegImagePlugin._getmp = lambda x: None
from bleach.sanitizer import Cleaner from bleach.sanitizer import Cleaner
from PyPDF2 import PdfFileWriter, PdfFileReader from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas from reportlab.pdfgen import canvas
@ -180,7 +180,7 @@ def process_image_upload(upload, crop):
try: try:
pic = Image.open(upload) pic = Image.open(upload)
except OSError: except OSError:
raise serializers.ValidationError(dict(photo='Invalid image file.')) raise serializers.ValidationError(dict(non_field_errors='Invalid image file.'))
logging.info('Detected format: %s', pic.format) logging.info('Detected format: %s', pic.format)
@ -189,7 +189,7 @@ def process_image_upload(upload, crop):
elif pic.format == 'JPEG': elif pic.format == 'JPEG':
ext = '.jpg' ext = '.jpg'
else: else:
raise serializers.ValidationError(dict(photo='Image must be a jpg or png.')) raise serializers.ValidationError(dict(non_field_errors='Image must be a jpg or png.'))
pic = ImageOps.exif_transpose(pic) pic = ImageOps.exif_transpose(pic)