Fixes: 1) Add exception handling for invalid image file dimensions in `validators.py`; Extra: 1) Update settings categories to include new system options; 2) Improve code clarity in `backup_task` and `validators.py`.
19 lines
775 B
Python
19 lines
775 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:
|
|
try:
|
|
width, height = get_image_dimensions(image.file) # type: ignore [arg-type]
|
|
except (FileNotFoundError, OSError, ValueError):
|
|
return
|
|
|
|
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"))
|