Skip to content

Commit

Permalink
Merge pull request #1246 from rdmorganiser/1244-bug-in-saving-short-t…
Browse files Browse the repository at this point in the history
…itle-when-value-is-too-long-for-page-and-section

Add max length validation to translated fields.
Related issue: #1244
  • Loading branch information
MyPyDavid authored Feb 20, 2025
2 parents 61aa6cb + 46e4c05 commit 1417153
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
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).']}

0 comments on commit 1417153

Please sign in to comment.