Skip to content

Commit

Permalink
Add patch to fix leaking translations
Browse files Browse the repository at this point in the history
Currently, when a translatable fields value is set via the admin
intrface in a specific lungage (e.g. title_de) , that value leaks into
the translated field of the language of the session (e.h. title_en).

This patch tries to fix this.
  • Loading branch information
donald committed Jan 15, 2020
1 parent 1d2462f commit d18a2ae
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions patches/modeltranslations-Don-t-leak-into-current-language.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
From 8fa955600d5653af7ee0c552fb18005a0dd9e043 Mon Sep 17 00:00:00 2001
From: Donald Buczek <buczek@molgen.mpg.de>
Date: Mon, 13 Jan 2020 16:43:34 +0100
Subject: [PATCH] modeltranslations: Don't leak into current language

Disable the implicit setting of the translation field from
the translated field ( Rule 2 of [1] ) if we come from
clean_fields. See [2]

[1] https://django-modeltranslation.readthedocs.io/en/latest/usage.html#rules-for-translated-field-access ) if we come from clean_fields
[2] https://github.com/deschler/django-modeltranslation/issues/382
---
lib/python3.7/site-packages/modeltranslation/fields.py | 2 +-
lib/python3.7/site-packages/modeltranslation/translator.py | 6 +++++-
2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/lib/python3.7/site-packages/modeltranslation/fields.py b/lib/python3.7/site-packages/modeltranslation/fields.py
index d3c5a42..2ff0d83 100644
--- a/lib/python3.7/site-packages/modeltranslation/fields.py
+++ b/lib/python3.7/site-packages/modeltranslation/fields.py
@@ -314,7 +314,7 @@ class TranslationFieldDescriptor(object):
instance.__dict__[self.field.name] = value
if isinstance(self.field, fields.related.ForeignKey):
instance.__dict__[self.field.get_attname()] = None if value is None else value.pk
- if getattr(instance, '_mt_init', False):
+ if getattr(instance, '_mt_init', False) or getattr(instance, '_mt_disable', False):
# When assignment takes place in model instance constructor, don't set value.
# This is essential for only/defer to work, but I think it's sensible anyway.
return
diff --git a/lib/python3.7/site-packages/modeltranslation/translator.py b/lib/python3.7/site-packages/modeltranslation/translator.py
index 901f701..d58dd55 100644
--- a/lib/python3.7/site-packages/modeltranslation/translator.py
+++ b/lib/python3.7/site-packages/modeltranslation/translator.py
@@ -267,7 +267,11 @@ def patch_clean_fields(model):
if orig_field_name in exclude:
field.save_form_data(self, value, check=False)
delattr(self, '_mt_form_pending_clear')
- old_clean_fields(self, exclude)
+ setattr(self, '_mt_disable', True)
+ try:
+ old_clean_fields(self, exclude)
+ finally:
+ setattr(self, '_mt_disable', False)
model.clean_fields = new_clean_fields


--
2.24.1

0 comments on commit d18a2ae

Please sign in to comment.