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

OP-2065: Change resolution of photo to 1024px #151

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion insuree/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import uuid
from importlib import import_module
from os import path
from io import BytesIO
from PIL import Image

from core.apps import CoreConfig
from django.db.models import Q
Expand All @@ -17,6 +19,7 @@
from django.core.exceptions import ValidationError
from core.models import filter_validity, resolved_id_reference


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -182,6 +185,22 @@ def reset_family_before_update(family):
family.json_ext = None



def _resize_image(base64_str, max_size=(1024, 1024)):
image_data = base64.b64decode(base64_str)

image = Image.open(BytesIO(image_data))

if image.size[0] > max_size[0] or image.size[1] > max_size[1]:
image.thumbnail(max_size) # Maintain aspect ratio

buffer = BytesIO()
image.save(buffer, format=image.format)

resized_base64_str = base64.b64encode(buffer.getvalue()).decode('utf-8')

return resized_base64_str

def handle_insuree_photo(user, now, insuree, data):
existing_insuree_photo = insuree.photo
insuree_photo = None
Expand All @@ -191,6 +210,7 @@ def handle_insuree_photo(user, now, insuree, data):
data['validity_from'] = now
data['insuree_id'] = insuree.id
photo_bin = data.get('photo', None)
photo_bin = _resize_image(photo_bin) if photo_bin else None
# no photo changes
if (
'uuid' in data and existing_insuree_photo and
Expand Down Expand Up @@ -329,7 +349,8 @@ def create_or_update(self, data, create_only=False):
status = data.get('status', InsureeStatus.ACTIVE)
if status not in [choice[0] for choice in InsureeStatus.choices]:
raise ValidationError(_("mutation.insuree.wrong_status"))
if InsureeConfig.is_insuree_photo_required and photo_data is None:
# If photo is set it cannot be deleted
if InsureeConfig.is_insuree_photo_required and photo_data is None and 'uuid' not in data:
dragos-dobre marked this conversation as resolved.
Show resolved Hide resolved
raise ValidationError(_("mutation.insuree.no_required_photo"))
insuree = None
if "uuid" in data:
Expand Down
Loading