Skip to content

Commit

Permalink
make ImageSizeValidator serializeable and add types
Browse files Browse the repository at this point in the history
  • Loading branch information
sainak committed Sep 20, 2024
1 parent 9291c88 commit 4efa55b
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions care/utils/models/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jsonschema
from django.core import validators
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import UploadedFile
from django.core.validators import RegexValidator
from django.utils.deconstruct import deconstructible
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -199,7 +200,7 @@ def __eq__(self, __value: object) -> bool: # pragma: no cover


class ImageSizeValidator:
message = {
message: dict[str, str] = {
"min_width": _(
"Image width is less than the minimum allowed width of %(min_width)s pixels."
),
Expand All @@ -225,14 +226,14 @@ class ImageSizeValidator:

def __init__(
self,
min_width: int = None,
max_width: int = None,
min_height: int = None,
max_height: int = None,
aspect_ratio: float = None,
min_size: int = None,
max_size: int = None,
):
min_width: int | None = None,
max_width: int | None = None,
min_height: int | None = None,
max_height: int | None = None,
aspect_ratio: float | None = None,
min_size: int | None = None,
max_size: int | None = None,
) -> None:
self.min_width = min_width
self.max_width = max_width
self.min_height = min_height
Expand All @@ -241,12 +242,12 @@ def __init__(
self.min_size = min_size
self.max_size = max_size

def __call__(self, value):
def __call__(self, value: UploadedFile) -> None:
with Image.open(value.file) as image:
width, height = image.size
size = value.size
size: int = value.size

errors = []
errors: list[str] = []

if self.min_width and width < self.min_width:
errors.append(self.message["min_width"] % {"min_width": self.min_width})
Expand Down Expand Up @@ -287,6 +288,22 @@ def __call__(self, value):

value.seek(0)

def __eq__(self, other: object) -> bool:
if not isinstance(other, ImageSizeValidator):
return False
return all(
getattr(self, attr) == getattr(other, attr)
for attr in [
"min_width",
"max_width",
"min_height",
"max_height",
"aspect_ratio",
"min_size",
"max_size",
]
)


cover_image_validator = ImageSizeValidator(
min_width=400,
Expand Down

0 comments on commit 4efa55b

Please sign in to comment.