Skip to content

Commit

Permalink
validation logo image
Browse files Browse the repository at this point in the history
  • Loading branch information
BelousSofiya committed Dec 9, 2023
1 parent 4986949 commit 947e705
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion images/tests/test_banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]")
self.company_dnipro = ProfileStartupFactory.create(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
)

Expand Down
8 changes: 8 additions & 0 deletions validation/validate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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).")

0 comments on commit 947e705

Please sign in to comment.