Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize NGO logos on upload #375

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions ro_help/hub/forms.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.LANCZOS)
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:
Expand Down