diff options
author | Andrea Lepori <alepori@student.ethz.ch> | 2020-09-04 13:01:54 +0200 |
---|---|---|
committer | Andrea Lepori <alepori@student.ethz.ch> | 2020-09-04 13:01:54 +0200 |
commit | 75ed5c3061cb88453824996fda6218eab7d3c40e (patch) | |
tree | 7abfe968af1bf06a79da612456440c73ebd9ab98 /accounts/views.py | |
parent | add missing field to personal data (diff) | |
download | scout-subs-75ed5c3061cb88453824996fda6218eab7d3c40e.tar.gz scout-subs-75ed5c3061cb88453824996fda6218eab7d3c40e.zip |
allow pdf upload
Diffstat (limited to 'accounts/views.py')
-rw-r--r-- | accounts/views.py | 49 |
1 files changed, 38 insertions, 11 deletions
diff --git a/accounts/views.py b/accounts/views.py index 230e3e1..af7527e 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -15,7 +15,12 @@ import dateparser import os from io import BytesIO from PIL import Image, UnidentifiedImageError - +from pdf2image import convert_from_path, convert_from_bytes +from pdf2image.exceptions import ( + PDFInfoNotInstalledError, + PDFPageCountError, + PDFSyntaxError +) @sensitive_variables("raw_passsword") def signup(request): @@ -156,11 +161,16 @@ def personal(request): name = files[0].name try: # if multiple files concatenate pictures - if len(files) == 1: - im = Image.open(files[0]) - else: - im = Image.open(files.pop(0)) - for f in files: + im = Image.new("RGB", (0, 0), (255, 255, 255)) + for f in files: + if f.name.endswith(".pdf"): + images = convert_from_bytes(f.read()) + for i in images: + dst = Image.new('RGB', (max(im.width, i.width), im.height + i.height), (255, 255, 255)) + dst.paste(im, (0, 0)) + dst.paste(i, (0, im.height)) + im = dst + else: i = Image.open(f) dst = Image.new('RGB', (max(im.width, i.width), im.height + i.height), (255, 255, 255)) dst.paste(im, (0, 0)) @@ -179,6 +189,12 @@ def personal(request): except UnidentifiedImageError: error = True error_text = "Il file non è un immagine valida" + except PDFPageCountError: + error = True + error_text = "Il file non è un pdf valido" + except PDFSyntaxError: + error = True + error_text = "Il file non è un pdf valido" except IOError: error = True error_text = "Il file è un immagine troppo grande" @@ -188,11 +204,16 @@ def personal(request): name = files[0].name try: # if multiple files concatenate pictures - if len(files) == 1: - im = Image.open(files[0]) - else: - im = Image.open(files.pop(0)) - for f in files: + im = Image.new("RGB", (0, 0), (255, 255, 255)) + for f in files: + if f.name.endswith(".pdf"): + images = convert_from_bytes(f.read()) + for i in images: + dst = Image.new('RGB', (max(im.width, i.width), im.height + i.height), (255, 255, 255)) + dst.paste(im, (0, 0)) + dst.paste(i, (0, im.height)) + im = dst + else: i = Image.open(f) dst = Image.new('RGB', (max(im.width, i.width), im.height + i.height), (255, 255, 255)) dst.paste(im, (0, 0)) @@ -211,6 +232,12 @@ def personal(request): except UnidentifiedImageError: error = True error_text = "Il file non è un immagine valida" + except PDFPageCountError: + error = True + error_text = "Il file non è un pdf valido" + except PDFSyntaxError: + error = True + error_text = "Il file non è un pdf valido" except IOError: error = True error_text = "Il file è un immagine troppo grande" |