schon/core/validators.py

16 lines
682 B
Python

from django.core.exceptions import ValidationError
from django.core.files.images import ImageFile, get_image_dimensions
from django.utils.translation import gettext_lazy as _
def validate_category_image_dimensions(
image: ImageFile, max_width: int | None = None, max_height: int | None = None
) -> None:
max_width = max_width or 7680
max_height = max_height or 4320
if image:
width, height = get_image_dimensions(image.file) # type: ignore [arg-type]
if int(width) > max_width or int(height) > max_height: # type: ignore [arg-type]
raise ValidationError(_(f"image dimensions should not exceed w{max_width} x h{max_height} pixels"))