Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disable aspect ratio validation for images #2487

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion care/utils/models/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
errors: Iterable[jsonschema.ValidationError],
container: list[ValidationError],
):
for error in errors:
if error.context:
return self._extract_errors(error.context, container)

message = str(error).replace("\n\n", ": ").replace("\n", "")
container.append(ValidationError(message))

Check failure on line 50 in care/utils/models/validators.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (RET503)

care/utils/models/validators.py:45:9: RET503 Missing explicit `return` at the end of function able to return non-`None` value


@deconstructible
Expand Down Expand Up @@ -88,23 +88,23 @@
landline_number_regex = r"^\+91[2-9]\d{7,9}$"
support_number_regex = r"^(1800|1860)\d{6,7}$"

regex_map = {
"indian_mobile": indian_mobile_number_regex,
"international_mobile": international_mobile_number_regex,
"mobile": rf"{indian_mobile_number_regex}|{international_mobile_number_regex}",
"landline": landline_number_regex,
"support": support_number_regex,
}

Check failure on line 97 in care/utils/models/validators.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (RUF012)

care/utils/models/validators.py:91:17: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`

def __init__(self, types: Iterable[str], *args, **kwargs):
if not isinstance(types, Iterable) or isinstance(types, str) or len(types) == 0:
raise ValueError("The `types` argument must be a non-empty iterable.")

Check failure on line 101 in care/utils/models/validators.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (EM101)

care/utils/models/validators.py:101:30: EM101 Exception must not use a string literal, assign to variable first

self.types = types
self.message = f"Invalid phone number. Must be one of the following types: {', '.join(self.types)}. Received: %(value)s"
self.code = "invalid_phone_number"

self.regex = r"|".join([self.regex_map[type] for type in self.types])

Check failure on line 107 in care/utils/models/validators.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (A001)

care/utils/models/validators.py:107:58: A001 Variable `type` is shadowing a Python builtin
super().__init__(*args, **kwargs)

def __eq__(self, other):
Expand All @@ -127,7 +127,7 @@
min_amount: int | float,
max_amount: int | float,
units: Iterable[str],
allow_floats: bool = True,

Check failure on line 130 in care/utils/models/validators.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (FBT001)

care/utils/models/validators.py:130:9: FBT001 Boolean-typed positional argument in function definition
precision: int = 2,
):
self.min_amount = min_amount
Expand All @@ -140,7 +140,7 @@
isinstance(min_amount, float) or isinstance(max_amount, float)
):
raise ValueError(
"If floats are not allowed, min_amount and max_amount must be integers"

Check failure on line 143 in care/utils/models/validators.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (EM101)

care/utils/models/validators.py:143:17: EM101 Exception must not use a string literal, assign to variable first
)

def __call__(self, value: str):
Expand All @@ -148,20 +148,20 @@
amount, unit = value.split(" ", maxsplit=1)
if unit not in self.allowed_units:
raise ValidationError(
f"Unit must be one of {', '.join(self.allowed_units)}"

Check failure on line 151 in care/utils/models/validators.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (EM102)

care/utils/models/validators.py:151:21: EM102 Exception must not use an f-string literal, assign to variable first
)

amount_number: int | float = float(amount)
if amount_number.is_integer():
amount_number = int(amount_number)
elif not self.allow_floats:
raise ValidationError("Input amount must be an integer")

Check failure on line 158 in care/utils/models/validators.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (EM101)

care/utils/models/validators.py:158:39: EM101 Exception must not use a string literal, assign to variable first
elif len(str(amount_number).split(".")[1]) > self.precision:
raise ValidationError("Input amount must have at most 4 decimal places")

Check failure on line 160 in care/utils/models/validators.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (EM101)

care/utils/models/validators.py:160:39: EM101 Exception must not use a string literal, assign to variable first

if len(amount) != len(str(amount_number)):
raise ValidationError(
f"Input amount must be a valid number without leading{' or trailing ' if self.allow_floats else ' '}zeroes"

Check failure on line 164 in care/utils/models/validators.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

Ruff (EM102)

care/utils/models/validators.py:164:21: EM102 Exception must not use an f-string literal, assign to variable first
)

if self.min_amount > amount_number or amount_number > self.max_amount:
Expand Down Expand Up @@ -248,6 +248,9 @@
self.aspect_ratio_str = ", ".join(
f"{ratio.numerator}:{ratio.denominator}" for ratio in self.aspect_ratio
)
else:
self.aspect_ratio = None
self.aspect_ratio_str = None

def __call__(self, value: UploadedFile) -> None:
with Image.open(value.file) as image:
Expand Down Expand Up @@ -326,7 +329,6 @@
min_height=400,
max_width=1024,
max_height=1024,
aspect_ratio=[1 / 1],
min_size=1024, # 1 KB
max_size=1024 * 1024 * 2, # 2 MB
)
Expand Down
12 changes: 0 additions & 12 deletions care/utils/tests/test_image_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,3 @@ def test_invalid_image_too_large(self):
"Image size is greater than the maximum allowed size of 2 MB.",
],
)

def test_invalid_image_aspect_ratio(self):
image = Image.new("RGB", (400, 800))
file = io.BytesIO()
image.save(file, format="JPEG")
test_file = UploadedFile(file, "test.jpg", "image/jpeg", 2048)
with self.assertRaises(ValidationError) as cm:
self.cover_image_validator(test_file)
self.assertEqual(
cm.exception.messages,
["Image aspect ratio must be one of the following: 1:1."],
)
Loading