Skip to content

Commit fdfd581

Browse files
jacobtylerwallschiatt
authored andcommitted
Fix i18n text/json field serialization in Django admin #10597
Follow-up to 9334ae1.
1 parent eeda1b0 commit fdfd581

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

arches/app/utils/betterJSONSerializer.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ def __init__(self, **options):
5353
self.utf_encode = False
5454

5555
def encode(self, obj):
56-
return self.serializeToPython(obj, **self._options)
56+
if isinstance(obj, (I18n_JSON, I18n_String)):
57+
# Ensure raw JSON strings are re-encoded ("'" -> "\"")
58+
# and non-active language values are preserved.
59+
return DjangoJSONEncoder().encode(obj.raw_value)
60+
return self.serialize(obj, **self._options)
5761

5862
def serializeToPython(self, obj, **options):
5963
# allow users to override any kwargs passed into the __init__ method
@@ -71,7 +75,7 @@ def serializeToPython(self, obj, **options):
7175

7276
def serialize(self, obj, **options):
7377
obj = self.serializeToPython(obj, **options)
74-
# prevent raw strings from begin re-encoded
78+
# prevent raw strings from being re-encoded
7579
# this is especially important when doing bulk operations in elasticsearch
7680
if isinstance(obj, str):
7781
return obj

releases/7.6.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Arches 7.6.0 Release Notes
4343
- 10911 Styling fix in resource model manage menu
4444
- 10726 Upgrade openpyxl package to 3.1.2 and fixes ETL modules
4545
- 9191 Adds unlocalized string datatype
46+
- 10597 Fix internationalized string/json field entry problems in the Django admin
4647
- 10787 Search Export: data exportable as "system values" (e.g. concept valueids) instead of "display values" (e.g. string preflabel)
4748
- 10121 Prevent language code duplication during concept import; avoid creating Language objects other than English in new projects
4849
- 10575 Add pre-commit hooks for formatting
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.test import SimpleTestCase
2+
from django.utils.translation import activate
3+
4+
from arches.app.models.fields.i18n import I18n_JSON, I18n_String
5+
from arches.app.utils.betterJSONSerializer import JSONSerializer
6+
7+
# these tests can be run from the command line via
8+
# python manage.py test tests.utils.test_betterJSONSerializer --settings="tests.test_settings"
9+
10+
11+
class TestBetterJSONSerializer(SimpleTestCase):
12+
def test_encode_i18n_json(self):
13+
serializer = JSONSerializer()
14+
json = I18n_JSON("{'trueLabel': {'en': 'Yes'}")
15+
encoded = serializer.encode(json)
16+
self.assertEqual(
17+
encoded, "\"{'trueLabel': {'en': 'Yes'}\"", "JSON must be double-quoted"
18+
)
19+
20+
def test_encode_i18n_string(self):
21+
string = I18n_String()
22+
string["de"] = "German label"
23+
string["en"] = "English label"
24+
25+
serializer = JSONSerializer()
26+
encoded = serializer.encode(string)
27+
self.assertEqual(encoded, '{"en": "English label", "de": "German label"}')

0 commit comments

Comments
 (0)