Fixes: 1) Add `# ty: ignore` comments to suppress type errors in multiple files; 2) Correct method argument annotations and definitions to align with type hints; 3) Fix cases of invalid or missing imports and unresolved attributes; Extra: Refactor method definitions to use tuple-based method declarations; replace custom type aliases with `Any`; improve caching utility and error handling logic in utility scripts.
25 lines
889 B
Python
25 lines
889 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) # ty: ignore[invalid-argument-type]
|
|
except (FileNotFoundError, OSError, ValueError):
|
|
return
|
|
|
|
if width is None or height is None:
|
|
return
|
|
if int(width) > max_width or int(height) > max_height:
|
|
raise ValidationError(
|
|
_(
|
|
f"image dimensions should not exceed w{max_width} x h{max_height} pixels"
|
|
)
|
|
)
|