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

Fix bulk_update() on I18n_JSON fields for homogenous values #10618 #10620

Merged
merged 3 commits into from
Mar 25, 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
14 changes: 14 additions & 0 deletions arches/app/models/fields/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils.translation import gettext_lazy as _
from django.db.migrations.serializer import BaseSerializer, Serializer
from django.db.models import JSONField
from django.db.models.functions.comparison import Cast
from django.db.models.sql.compiler import SQLInsertCompiler
from django.db.models.sql.where import NothingNode
from django.utils.translation import get_language
Expand Down Expand Up @@ -237,6 +238,17 @@ def __init__(self, value=None, lang=None, use_nulls=False, attname=None):
def _parse(self, value, lang, use_nulls):
ret = {}

if isinstance(value, Cast):
# Django 4.2 regression: bulk_update() sends Cast expressions
# https://code.djangoproject.com/ticket/35167
values = set(case.result.value for case in value.source_expressions[0].cases)
value = list(values)[0]
if len(values) > 1:
# Prevent silent data loss.
raise NotImplementedError(
"Heterogenous values provided to I18n_JSON field bulk_update():\n"
f"{tuple(str(v) for v in values)}"
)
if isinstance(value, str):
try:
ret = json.loads(value)
Expand All @@ -248,6 +260,8 @@ def _parse(self, value, lang, use_nulls):
ret = value.raw_value
elif isinstance(value, dict):
ret = value
else:
raise TypeError(value)
self.raw_value = ret

if "i18n_properties" in self.raw_value:
Expand Down
55 changes: 54 additions & 1 deletion tests/localization/field_tests.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import json

from arches.app.models.models import DDataType
from arches.app.models.fields.i18n import I18n_String, I18n_TextField, I18n_JSON, I18n_JSONField
from tests.base_test import ArchesTestCase
from django.contrib.gis.db import models
from django.utils import translation
from django.db import connection

# these tests can be run from the command line via
# python manage.py test tests/localization/field_tests.py --pattern="*.py" --settings="tests.test_settings"
# python manage.py test tests.localization.field_tests --settings="tests.test_settings"


class Customi18nTextFieldTests(ArchesTestCase):
Expand Down Expand Up @@ -371,3 +373,54 @@ def test_i18nJSONField_can_handle_different_initial_states(self):
m.save()
m = self.LocalizationTestJsonModel.objects.get(pk=3)
self.assertEqual(m.config.raw_value, expected_output_json)


class I18nJSONFieldBulkUpdateTests(ArchesTestCase):
def test_bulk_update_node_config_homogenous_value(self):
new_config = I18n_JSON({
"en": "some",
"zh": "json",
})
for_bulk_update = []
for dt in DDataType.objects.all()[:3]:
dt.defaultconfig = new_config
for_bulk_update.append(dt)

DDataType.objects.bulk_update(for_bulk_update, fields=["defaultconfig"])

for i, obj in enumerate(for_bulk_update):
with self.subTest(obj_index=i):
obj.refresh_from_db()
self.assertEqual(str(obj.defaultconfig), str(new_config))

def test_bulk_update_heterogenous_values(self):
new_configs = [
I18n_JSON({
"en": "some",
"zh": "json",
}),
I18n_JSON({}),
None,
]
for_bulk_update = []
for i, dt in enumerate(DDataType.objects.all()[:3]):
dt.defaultconfig = new_configs[i]
for_bulk_update.append(dt)

with self.assertRaises(NotImplementedError):
DDataType.objects.bulk_update(for_bulk_update, fields=["defaultconfig"])

# If the above starts failing, it's likely the underlying Django
# regression was fixed.
# https://code.djangoproject.com/ticket/35167

# In that case, remove the with statement, de-indent the bulk_update,
# and comment the following code back in:

# for i, obj in enumerate(for_bulk_update):
# new_config_as_string = str(new_configs[i])
# with self.subTest(new_config=new_config_as_string):
# obj.refresh_from_db()
# self.assertEqual(str(obj.defaultconfig), new_config_as_string)

# Also consider removing the code at the top of I18n_JSON._parse()