Skip to content

Commit

Permalink
Merge pull request #27 from trojsten/avatar-square
Browse files Browse the repository at this point in the history
Force square avatars
  • Loading branch information
gardenerik authored Sep 10, 2023
2 parents 593142e + 9bb4c87 commit adba6b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions trojstenid/users/forms/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from io import BytesIO

from django import forms
from django.core.exceptions import ValidationError
from django.core.files.images import get_image_dimensions
from PIL import Image
from PIL.Image import Resampling

from trojstenid.users.models import User

Expand All @@ -7,3 +13,18 @@ class ProfileForm(forms.ModelForm):
class Meta:
model = User
fields = ["first_name", "last_name", "avatar_file"]

def clean_avatar_file(self):
avatar = self.cleaned_data.get("avatar_file")
if avatar:
w, h = get_image_dimensions(avatar)
if w != h:
raise ValidationError("Profilová fotka musí byť štvorec.")
if w < 200:
raise ValidationError("Profilová fotka musí mať aspoň 200x200px.")
if w > 1000:
img = Image.open(avatar).convert("RGB")
img.thumbnail((1000, 1000), Resampling.BILINEAR)
avatar.file = BytesIO()
img.save(avatar.file, "jpeg")
return avatar
3 changes: 3 additions & 0 deletions trojstenid/users/templates/settings/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ <h2 class="text-xl font-bold text-gray-900">Osobné údaje</h2>
<label class="flex items-center mt-2 text-sm leading-6 text-gray-900">
<input type="checkbox" class="input-checkbox mr-3" name="{{ form.avatar_file.html_name }}-clear"> Vymazať a použiť predvolenú fotku
</label>
{% if form.avatar_file.errors %}
<span class="mt-2 text-sm text-red-600">{{ form.avatar_file.errors|first }}</span>
{% endif %}
</div>
</div>
</div>
Expand Down

0 comments on commit adba6b1

Please sign in to comment.