Merge pull request #558 from mouse-reeve/image-crop

Moves avatar crop into function and adds test
This commit is contained in:
Mouse Reeve 2021-01-26 08:24:52 -08:00 committed by GitHub
commit 12b95c6cd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 19 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -1,5 +1,9 @@
''' test for app action functionality ''' ''' test for app action functionality '''
import pathlib
from unittest.mock import patch from unittest.mock import patch
from PIL import Image
from django.core.files.base import ContentFile
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
@ -97,3 +101,15 @@ class UserViews(TestCase):
with patch('bookwyrm.broadcast.broadcast_task.delay'): with patch('bookwyrm.broadcast.broadcast_task.delay'):
view(request) view(request)
self.assertEqual(self.local_user.name, 'New Name') self.assertEqual(self.local_user.name, 'New Name')
def test_crop_avatar(self):
''' reduce that image size '''
image_file = pathlib.Path(__file__).parent.joinpath(
'../../static/images/no_cover.jpg')
image = Image.open(image_file)
result = views.user.crop_avatar(image)
self.assertIsInstance(result, ContentFile)
image_result = Image.open(result)
self.assertEqual(image_result.size, (120, 120))

View file

@ -159,6 +159,20 @@ class EditUser(View):
if 'avatar' in form.files: if 'avatar' in form.files:
# crop and resize avatar upload # crop and resize avatar upload
image = Image.open(form.files['avatar']) image = Image.open(form.files['avatar'])
image = crop_avatar(image)
# set the name to a hash
extension = form.files['avatar'].name.split('.')[-1]
filename = '%s.%s' % (uuid4(), extension)
user.avatar.save(filename, image)
user.save()
broadcast(user, user.to_update_activity(user))
return redirect(user.local_path)
def crop_avatar(image):
''' reduce the size and make an avatar square '''
target_size = 120 target_size = 120
width, height = image.size width, height = image.size
thumbnail_scale = height / (width / target_size) if height > width \ thumbnail_scale = height / (width / target_size) if height > width \
@ -176,13 +190,4 @@ class EditUser(View):
)) ))
output = BytesIO() output = BytesIO()
cropped.save(output, format=image.format) cropped.save(output, format=image.format)
ContentFile(output.getvalue()) return ContentFile(output.getvalue())
# set the name to a hash
extension = form.files['avatar'].name.split('.')[-1]
filename = '%s.%s' % (uuid4(), extension)
user.avatar.save(filename, ContentFile(output.getvalue()))
user.save()
broadcast(user, user.to_update_activity(user))
return redirect(user.local_path)