From 98273fc5790b8a6882d558bdafd5e419be226035 Mon Sep 17 00:00:00 2001 From: Radu Mocanu Date: Tue, 26 May 2020 18:44:06 +0300 Subject: [PATCH 1/3] resized ngo avatar on upload --- ro_help/hub/forms.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ro_help/hub/forms.py b/ro_help/hub/forms.py index ad4d94ca..8a3ac229 100644 --- a/ro_help/hub/forms.py +++ b/ro_help/hub/forms.py @@ -1,7 +1,11 @@ +from io import BytesIO + from django import forms from django.utils.translation import ugettext_lazy as _ from django.core.exceptions import ValidationError from django_crispy_bulma.widgets import EmailInput +from django.core.files.uploadedfile import InMemoryUploadedFile +from PIL import Image from hub import models from captcha.fields import ReCaptchaField @@ -79,6 +83,21 @@ class Meta: # "statute": AdminResubmitFileWidget, } + def clean_avatar(self): + avatar = self.cleaned_data["avatar"] + + im = Image.open(avatar) + thumb_io = BytesIO() + + im.thumbnail((200, 200), Image.ANTIALIAS) + im.save(thumb_io, avatar.content_type.split("/")[-1].upper()) + + file = InMemoryUploadedFile( + thumb_io, "avatar", str(avatar), avatar.content_type, thumb_io.getbuffer().nbytes, None, + ) + + return file + class RegisterNGORequestVoteForm(forms.ModelForm): class Meta: From 8eb77083fc4d3df19224e1bdd448d12a8a266a9f Mon Sep 17 00:00:00 2001 From: Radu Mocanu Date: Wed, 27 May 2020 16:17:33 +0300 Subject: [PATCH 2/3] replaced ANTIALIAS with LANCZOS --- ro_help/hub/forms.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ro_help/hub/forms.py b/ro_help/hub/forms.py index 8a3ac229..c550dd60 100644 --- a/ro_help/hub/forms.py +++ b/ro_help/hub/forms.py @@ -89,8 +89,9 @@ def clean_avatar(self): im = Image.open(avatar) thumb_io = BytesIO() - im.thumbnail((200, 200), Image.ANTIALIAS) + im.thumbnail((200, 200), Image.LANCZOS) im.save(thumb_io, avatar.content_type.split("/")[-1].upper()) + self.add_error("name", '{} {}'.format(avatar.content_type, avatar.content_type.split("/")[-1].upper())) file = InMemoryUploadedFile( thumb_io, "avatar", str(avatar), avatar.content_type, thumb_io.getbuffer().nbytes, None, From 0a5d64fce89852b825e720dc64dae917fb8f03d8 Mon Sep 17 00:00:00 2001 From: Radu Mocanu Date: Wed, 27 May 2020 16:19:01 +0300 Subject: [PATCH 3/3] removed error --- ro_help/hub/forms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ro_help/hub/forms.py b/ro_help/hub/forms.py index c550dd60..3b97193c 100644 --- a/ro_help/hub/forms.py +++ b/ro_help/hub/forms.py @@ -91,7 +91,6 @@ def clean_avatar(self): im.thumbnail((200, 200), Image.LANCZOS) im.save(thumb_io, avatar.content_type.split("/")[-1].upper()) - self.add_error("name", '{} {}'.format(avatar.content_type, avatar.content_type.split("/")[-1].upper())) file = InMemoryUploadedFile( thumb_io, "avatar", str(avatar), avatar.content_type, thumb_io.getbuffer().nbytes, None,