Skip to content

Commit 487fccc

Browse files
Merge pull request #2694 from IFRCGo/fix/dref-activities-translation
Lang/Fix translation cache duplicate creation issue
2 parents bd9d795 + ae55ba9 commit 487fccc

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

assets

dref/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Meta:
6565
fields = "__all__"
6666

6767

68-
class ProposedActionActivitySerializer(serializers.ModelSerializer):
68+
class ProposedActionActivitySerializer(ModelSerializer):
6969
id = serializers.IntegerField(required=False)
7070
sector = serializers.PrimaryKeyRelatedField(queryset=Sector.objects.all(), required=True)
7171

dref/tasks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
PlannedIntervention,
1818
PlannedInterventionIndicators,
1919
ProposedAction,
20+
ProposedActionActivities,
2021
RiskSecurity,
2122
SourceInformation,
2223
)
@@ -50,6 +51,7 @@ def send_dref_email(dref_id, users_emails, new_or_updated=""):
5051
PlannedIntervention,
5152
RiskSecurity,
5253
ProposedAction,
54+
ProposedActionActivities,
5355
PlannedInterventionIndicators,
5456
SourceInformation,
5557
]

lang/translation.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,22 @@ def translate_text(self, text, dest_language, source_language=None, table_field=
182182

183183
# Cache the translation if original text was short enough
184184
if use_cache:
185-
TranslationCache.objects.create(
185+
obj, created = TranslationCache.objects.get_or_create(
186186
text=text,
187187
text_hash=text_hash,
188188
source_language=source_language or "", # source_language can be "detected"
189189
dest_language=dest_language,
190-
translated_text=translated,
191-
table_field=table_field or "",
192-
last_used=timezone.now(),
190+
defaults={
191+
"translated_text": translated,
192+
"table_field": table_field or "",
193+
"last_used": timezone.now(),
194+
},
193195
)
196+
if not created:
197+
TranslationCache.objects.filter(pk=obj.pk).update(
198+
last_used=timezone.now(),
199+
num_calls=F("num_calls") + 1,
200+
)
194201
return translated + textTail
195202

196203

0 commit comments

Comments
 (0)