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

Add max length validation to translated fields #1246

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
9 changes: 8 additions & 1 deletion rdmo/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.contrib.auth.models import Group
from django.contrib.sites.models import Site
from django.core.validators import MaxLengthValidator
from django.db.models import Max

from rest_framework import serializers
Expand Down Expand Up @@ -58,11 +59,17 @@ def __init__(self, *args, **kwargs):
field_name = f'{field}_{lang_field}'
model_field = meta.model._meta.get_field(field_name)

validators = []
if hasattr(model_field, 'max_length') and model_field.max_length is not None:
validators.append(MaxLengthValidator(model_field.max_length))

self.fields[f'{field}_{lang_code}'] = serializers.CharField(
source=field_name,
required=not model_field.blank,
allow_null=model_field.null,
allow_blank=model_field.blank)
allow_blank=model_field.blank,
validators=validators
)


class ThroughModelSerializerMixin:
Expand Down
16 changes: 16 additions & 0 deletions rdmo/questions/tests/test_viewset_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,19 @@ def test_detail_export_full(db, client):
assert 'http://example.com/terms/domain/conditions' in uris
assert 'http://example.com/terms/options/one_two_three' in uris
assert 'http://example.com/terms/options/one_two_three/one' in uris

def test_update_page_field_validation_short_title(db, client):
username = password = 'editor'
client.login(username=username, password=password)
instance = Page.objects.first()
very_long_title = 'short_title, ' * 10

url = reverse(urlnames['detail'], args=[instance.pk])
data = {
'uri_prefix': instance.uri_prefix,
'uri_path': instance.uri_path,
'short_title_en': very_long_title,
}
response = client.put(url, data, content_type='application/json')
assert response.status_code == 400
assert response.json() == {'short_title_en': ['Ensure this value has at most 32 characters (it has 129).']}
17 changes: 17 additions & 0 deletions rdmo/questions/tests/test_viewset_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,20 @@ def test_detail_export_full(db, client):
assert 'http://example.com/terms/domain/conditions' in uris
assert 'http://example.com/terms/options/one_two_three' in uris
assert 'http://example.com/terms/options/one_two_three/one' in uris


def test_update_section_field_validation_short_title(db: object, client: object) -> None:
username = password = 'editor'
client.login(username=username, password=password)
instance = Section.objects.first()
very_long_title = 'short_title, ' * 10

url = reverse(urlnames['detail'], args=[instance.pk])
data = {
'uri_prefix': instance.uri_prefix,
'uri_path': instance.uri_path,
'short_title_de': very_long_title,
}
response = client.put(url, data, content_type='application/json')
assert response.status_code == 400
assert response.json() == {'short_title_de': ['Ensure this value has at most 32 characters (it has 129).']}
Loading