From 947e705de8eccf61b88d206e283fe469ec9810c0 Mon Sep 17 00:00:00 2001 From: Sofia Bilous Date: Sat, 9 Dec 2023 19:37:02 +0200 Subject: [PATCH] validation logo image --- images/tests/test_banner.py | 2 +- .../0005_profile_logo_image_alter_profile_banner_image.py | 4 ++-- profiles/models.py | 3 ++- validation/validate_image.py | 8 ++++++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/images/tests/test_banner.py b/images/tests/test_banner.py index bd44dad7e..5f312d202 100644 --- a/images/tests/test_banner.py +++ b/images/tests/test_banner.py @@ -28,7 +28,7 @@ def _generate_image(ext, size=(100, 100)): def setUp(self) -> None: self.right_image = self._generate_image("jpeg", (100, 100)) - self.wrong_image = self._generate_image("png", (7000, 7000)) + self.wrong_image = self._generate_image("png", (8000, 10000)) self.user = UserFactory(email="test1@test.com") self.company_dnipro = ProfileStartupFactory.create( diff --git a/profiles/migrations/0005_profile_logo_image_alter_profile_banner_image.py b/profiles/migrations/0005_profile_logo_image_alter_profile_banner_image.py index 52ec7a1f2..7af28c8ea 100644 --- a/profiles/migrations/0005_profile_logo_image_alter_profile_banner_image.py +++ b/profiles/migrations/0005_profile_logo_image_alter_profile_banner_image.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.4 on 2023-12-09 13:17 +# Generated by Django 4.2.4 on 2023-12-09 15:58 from django.db import migrations, models import validation.validate_image @@ -14,7 +14,7 @@ class Migration(migrations.Migration): migrations.AddField( model_name='profile', name='logo_image', - field=models.ImageField(null=True, upload_to='logos', validators=[validation.validate_image.validate_image_format, validation.validate_image.validate_image_size]), + field=models.ImageField(null=True, upload_to='logos', validators=[validation.validate_image.validate_image_format, validation.validate_image.validate_logo_size]), ), migrations.AlterField( model_name='profile', diff --git a/profiles/models.py b/profiles/models.py index b572b6807..0ce125639 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -6,6 +6,7 @@ from validation.validate_foundation_year import validate_foundation_year_range from validation.validate_image import ( validate_image_size, + validate_logo_size, validate_image_format, ) from validation.validate_phone_number import ( @@ -94,7 +95,7 @@ class Profile(models.Model): logo_image = models.ImageField( upload_to="logos", - validators=[validate_image_format, validate_image_size], + validators=[validate_image_format, validate_logo_size], null=True, ) diff --git a/validation/validate_image.py b/validation/validate_image.py index a3cbf7996..e6cf59f8d 100644 --- a/validation/validate_image.py +++ b/validation/validate_image.py @@ -3,6 +3,8 @@ import sys MAX_ALLOWED_BANNER_IMAGE_SIZE = 50 * 1024 +MAX_ALLOWED_LOGO_IMAGE_SIZE = 10 * 1024 +#ToDo: discuss problem with MB-sized pictures with mentors, experts and BAs def validate_image_format(image: Image): @@ -19,3 +21,9 @@ def validate_image_size(image_file): max_size = sys.getsizeof(image_file.file) if max_size > MAX_ALLOWED_BANNER_IMAGE_SIZE: raise ValidationError("Image size exceeds the maximum allowed (50MB).") + + +def validate_logo_size(image_file): + max_size = sys.getsizeof(image_file.file) + if max_size > MAX_ALLOWED_LOGO_IMAGE_SIZE: + raise ValidationError("Image size exceeds the maximum allowed (10MB).")