Skip to content

Commit

Permalink
Max length validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mgax committed Mar 13, 2024
1 parent b0243d3 commit 7cdf140
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/wagtail_ai/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def clean_prompt(self):

class DescribeImageApiForm(ApiForm):
image_id = forms.CharField()
maxlength = forms.IntegerField(required=False)
maxlength = forms.IntegerField(required=False, min_value=0, max_value=4096)


class DescribeImageForm(BaseImageForm):
Expand Down
4 changes: 2 additions & 2 deletions src/wagtail_ai/static_src/image_description/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import wandIcon from '../wand_icon.svg';

document.addEventListener('wagtail-ai:image-form', (event) => {
const input = event.target as HTMLInputElement;
const imageId = input.dataset['wagtailaiImageId'];
const imageId = input.dataset.wagtailaiImageId;
if (!imageId) {
throw new Error('The attribute data-wagtailai-image-id is missing.');
}
Expand All @@ -27,7 +27,7 @@ document.addEventListener('wagtail-ai:image-form', (event) => {

const button = document.createElement('button');
button.type = 'button';
button.title = input.dataset['wagtailaiButtonTitle'] || '';
button.title = input.dataset.wagtailaiButtonTitle || '';
button.classList.add('button', 'wagtailai-button');
button.innerHTML = wandIcon;
flexWrapper.appendChild(button);
Expand Down
43 changes: 43 additions & 0 deletions tests/views/test_describe_image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from http import HTTPStatus
from typing import cast
from unittest.mock import ANY, Mock, call

Expand Down Expand Up @@ -133,3 +134,45 @@ def test_custom_rendition_filter(admin_client, settings):
assert response.json() == {
"message": "This is an echo backend: images/example.max-100x100.jpg"
}


@pytest.mark.parametrize(
"maxlength,expected_status,error_message",
[
(100, HTTPStatus.OK, None),
(
-1,
HTTPStatus.BAD_REQUEST,
"Ensure this value is greater than or equal to 0.",
),
(
10000,
HTTPStatus.BAD_REQUEST,
"Ensure this value is less than or equal to 4096.",
),
],
)
def test_maxlength_validation(
admin_client, settings, maxlength, expected_status, error_message
):
settings.WAGTAIL_AI = {
"BACKENDS": {
"echo": {
"CLASS": "wagtail_ai.ai.echo.EchoBackend",
"CONFIG": {
"MODEL_ID": "echo",
"TOKEN_LIMIT": 123123,
},
},
},
"IMAGE_DESCRIPTION_BACKEND": "echo",
}

image = cast(Image, ImageFactory())
response = admin_client.post(
reverse("wagtail_ai:describe_image"),
data={"image_id": image.pk, "maxlength": maxlength},
)
assert response.status_code == expected_status
if error_message is not None:
assert response.json() == {"error": error_message}

0 comments on commit 7cdf140

Please sign in to comment.