Changeset f0e64eb in OpenWorkouts-current


Ignore:
Timestamp:
Feb 15, 2019, 11:00:35 AM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
0fa21cd
Parents:
eb20dc8
Message:

(#51) Fixed profile images were too big:

  • Added new dependency (Pillow)
  • Modified the user profile picture view. Now it accepts a GET parameter (size) telling the size of the image we want. Only one value is needed, the code will scale the image appropiately.
  • Modified the dashboard, profile and edit_profile templates to ask for a smaller version (200px) of the user profile picture.

IMPORTANT: Ensure you install Pillow in any existing environments after pulling:

pip install Pillow

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • ow/templates/dashboard.pt

    reb20dc8 rf0e64eb  
    101101          <tal:c tal:condition="getattr(context, 'picture', None)">
    102102            <a href="" tal:attributes="href request.resource_url(context, 'profile')">
    103               <img tal:attributes="src request.resource_path(context, 'picture')"
     103              <img tal:attributes="src request.resource_path(context, 'picture', query={'size': 200})"
    104104                   width="450" />
    105105            </a>
  • ow/templates/edit_profile.pt

    reb20dc8 rf0e64eb  
    4949                      <label for="current_picture" i18n:translate="">
    5050                          Current picture:</label>
    51                       <img id="current_picture" tal:attributes="src request.resource_path(context, 'picture')" width="150">
     51                      <img id="current_picture"
     52                           tal:attributes="src request.resource_path(context, 'picture', query={'size': 200})"
     53                           width="150">
    5254                  </tal:c>
    5355                </div>
  • ow/templates/profile.pt

    reb20dc8 rf0e64eb  
    1818        <div>
    1919          <tal:c tal:condition="getattr(user, 'picture', None)">
    20             <img tal:attributes="src request.resource_path(user, 'picture')"
     20            <img tal:attributes="src request.resource_path(user, 'picture', query={'size': 200})"
    2121                 width="450" />
    2222          </tal:c>
  • ow/tests/views/test_user.py

    reb20dc8 rf0e64eb  
    55from shutil import copyfileobj
    66from unittest.mock import Mock, patch
     7from io import BytesIO
    78
    89import pytest
     
    1516
    1617from webob.multidict import MultiDict
     18
     19from PIL import Image
    1720
    1821from ow.models.root import OpenWorkouts
     
    417420        assert response.status_int == 200
    418421        assert response.content_type == 'image'
     422        # as we did not pass a specific size as a get parameter, the size is
     423        # the same as the original image
     424        original_image = Image.open(image_path)
     425        returned_image = Image.open(BytesIO(response.body))
     426        assert original_image.size == returned_image.size
     427
     428        # now, ask for a smaller image
     429        request.GET['size'] = original_image.size[0] - 20
     430        response = user_views.profile_picture(user, request)
     431        assert isinstance(response, Response)
     432        assert response.status_int == 200
     433        assert response.content_type == 'image'
     434        # now the size of the original image is bigger
     435        returned_image = Image.open(BytesIO(response.body))
     436        assert original_image.size > returned_image.size
     437
     438        # now, ask for a size that is bigger than the original image,
     439        # image will be the same size, as we do not "grow" its size
     440        request.GET['size'] = original_image.size[0] + 1000
     441        response = user_views.profile_picture(user, request)
     442        assert isinstance(response, Response)
     443        assert response.status_int == 200
     444        assert response.content_type == 'image'
     445        # now the size of the original image is bigger
     446        returned_image = Image.open(BytesIO(response.body))
     447        assert original_image.size == returned_image.size
     448
    419449
    420450    def test_edit_profile_get(self, dummy_request, john):
  • ow/views/user.py

    reb20dc8 rf0e64eb  
    33from datetime import datetime, timezone, timedelta
    44from decimal import Decimal
     5from io import BytesIO
    56
    67from pyramid.httpexceptions import HTTPFound, HTTPNotFound
     
    1112from pyramid_simpleform import Form, State
    1213from pytz import common_timezones
     14from PIL import Image
    1315
    1416from ..models.user import User
     
    212214    permission='view')
    213215def profile_picture(context, request):
    214     return Response(
    215         content_type='image',
    216         body_file=context.picture.open())
     216    if context.picture is None:
     217        return HTTPNotFound()
     218
     219    size = request.GET.get('size', 0)
     220    # we will need a tuple, it does not matter if both values are the same,
     221    # Pillow will keep aspect ratio
     222    size = (int(size), int(size))
     223
     224    image = Image.open(context.picture.open())
     225
     226    if size > (0, 0) and size < image.size:
     227        # resize only if they are asking for smaller size, prevent
     228        # someone asking for a "too big" image
     229        image.thumbnail(size)
     230
     231    body_file = BytesIO()
     232    image.save(body_file, format=image.format)
     233    return Response(content_type='image', body=body_file.getvalue())
    217234
    218235
  • setup.py

    reb20dc8 rf0e64eb  
    3131    'pytz',
    3232    'fitparse',
    33     'splinter'
     33    'splinter',
     34    'Pillow'
    3435]
    3536
Note: See TracChangeset for help on using the changeset viewer.