Skip to content

Commit 8520115

Browse files
author
dorin.musteata
committed
Changelog:
* Auto update model i18n field , delete / update languages
1 parent 2731612 commit 8520115

File tree

7 files changed

+40
-23
lines changed

7 files changed

+40
-23
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class Blog(models.Model): # noqa
8585

8686
# DRF Localize model specific constant, which is used to store translations (json)
8787
LOCALIZE_FIELD = 'i18n'
88+
89+
# DRF Localize model specific constant, which is used to auto-update or remove translations for languages
90+
LOCALIZE_AUTO_UPDATE = False # not required , defaults to False
8891

8992
# Your custom model fields
9093
title = models.CharField(max_length=254, null=True)

drf_localize/core/__init__.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
from django.db.models.base import ModelBase
88
from contextlib import suppress
99
from zipfile import ZipFile
10-
from django.db.models import (
11-
F,
12-
Func,
13-
Value,
14-
JSONField
15-
)
1610

1711
# Import your package here.
1812

@@ -451,28 +445,48 @@ def _signal(self, model=None):
451445

452446
translate, field, auto_update = self._model_set(model=model)
453447

454-
if not auto_update:
448+
if not all([translate, field, auto_update]):
455449
return self
456450

457451
languages = self.get_languages()
452+
objects = []
453+
queryset = model.objects.all()
454+
455+
for element in queryset:
456+
i18n = getattr(element, field, None)
457+
458+
if not i18n:
459+
continue
458460

459-
x = model.objects.values_list(field, 'id')
460-
for i18n, _id in x:
461461
keys = list(i18n.keys())
462-
difference = list(set(languages).difference(keys))
462+
difference_to_update = list(set(languages).difference(keys))
463+
difference_to_delete = list(set(keys).difference(languages))
463464

464-
if not difference:
465+
if not difference_to_update and not difference_to_delete:
465466
continue
466467

467-
payload = {}
468-
for language in difference:
469-
payload.update({language: {}})
468+
for language in difference_to_delete:
469+
i18n.pop(language, None)
470470

471-
for translate_field in translate:
472-
payload[language].update({translate_field: ''})
471+
for language in difference_to_update:
472+
i18n.update({language: {}})
473473

474-
i18n.update(**payload)
475-
model.objects.filter(id=_id).update(**{field: i18n})
474+
for translate_field in translate:
475+
i18n[language].update({translate_field: ''})
476+
477+
# Set updatable field
478+
setattr(element, field, i18n)
479+
objects.append(element)
480+
481+
# Bulk update model objects
482+
if length := len(objects):
483+
model.objects.bulk_update(
484+
objs=objects,
485+
fields=[
486+
field,
487+
],
488+
batch_size=length,
489+
)
476490

477491

478492
# Export localize instance

drf_localize/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __init__(self, **kwargs):
3232
self.context = kwargs.pop('context', None)
3333
self.localize_namespace = kwargs.pop('namespace', False)
3434
self.localize_translate, self.localize_field, self.localize_auto_update = localize._model_set(model=self.localize_model) # noqa
35-
localize._signal(model=self.localize_model)
35+
localize._signal(model=self.localize_model) # noqa
3636
super(I18N, self).__init__(**kwargs)
3737

3838
def to_representation(self, instance):

drf_localize_test/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@
8484
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
8585

8686
DRF_LOCALIZE = {
87-
'LANGUAGES': ['en', 'ru', 'ro', 'pt', 'bg'],
87+
'LANGUAGES': ['en', 'ro', 'bg'],
8888
}

drf_localize_test/test/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Blog(models.Model): # noqa
1212
LOCALIZE_FIELD = 'i18n'
1313

1414
# DRF Localize model specific constant, which is used to auto-update or remove translations for new languages
15-
LOCALIZE_AUTO_UPDATE = True
15+
LOCALIZE_AUTO_UPDATE = False
1616

1717
# Your custom model fields
1818
title = models.CharField(max_length=254, null=True)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "drf-localize"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
description = "Package to provide localization experiences for mobile and api applications."
55
authors = ["Dorin Musteața"]
66
readme = "README.md"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name="drf-localize",
9-
version="0.1.4",
9+
version="0.1.5",
1010
author="Dorin Musteața",
1111
description="Package to provide localization experiences for mobile and api applications.",
1212
long_description=README,

0 commit comments

Comments
 (0)