Repository: mozilla/pontoon

Issue:
Log translation creation action for pretranslations
We currently do not log translation creation actions for pretranslations.

The patch should also include a data migration to create missing objects for existing pretranslations.
Log translation creation action for pretranslations
We currently do not log translation creation actions for pretranslations.

The patch should also include a data migration to create missing objects for existing pretranslations.


Discussion:

Resolving patch:
diff --git a/pontoon/actionlog/migrations/0003_existing_pretranslation_action.py b/pontoon/actionlog/migrations/0003_existing_pretranslation_action.py
new file mode 100644
--- /dev/null
+++ b/pontoon/actionlog/migrations/0003_existing_pretranslation_action.py
@@ -0,0 +1,50 @@
+# Generated by Django 3.2.15 on 2023-01-24 10:40
+
+from django.db import migrations
+
+
+def create_action_logs_for_pretranslations(apps, schema_editor):
+    Translation = apps.get_model("base", "Translation")
+    ActionLog = apps.get_model("actionlog", "ActionLog")
+
+    translations = Translation.objects.filter(
+        user__email__in=["pontoon-sync@example.com", "pontoon-tm@example.com"]
+    )
+
+    actions_to_log = [
+        ActionLog(
+            action_type="translation:created",
+            performed_by=t.user,
+            translation=t,
+            created_at=t.date,
+        )
+        for t in translations
+    ]
+
+    ActionLog.objects.bulk_create(actions_to_log)
+
+
+def delete_action_logs_for_pretranslations(apps, schema_editor):
+    ActionLog = apps.get_model("actionlog", "ActionLog")
+
+    ActionLog.objects.filter(
+        translation__user__email__in=[
+            "pontoon-sync@example.com",
+            "pontoon-tm@example.com",
+        ],
+        action_type="translation:created",
+    ).delete()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("actionlog", "0002_auto_20200703_0709"),
+    ]
+
+    operations = [
+        migrations.RunPython(
+            code=create_action_logs_for_pretranslations,
+            reverse_code=delete_action_logs_for_pretranslations,
+        ),
+    ]
diff --git a/pontoon/pretranslation/tasks.py b/pontoon/pretranslation/tasks.py
--- a/pontoon/pretranslation/tasks.py
+++ b/pontoon/pretranslation/tasks.py
@@ -10,6 +10,7 @@
     Translation,
     ChangedEntityLocale,
 )
+from pontoon.actionlog.models import ActionLog
 from pontoon.pretranslation.pretranslate import (
     get_translations,
     update_changed_instances,
@@ -154,6 +155,18 @@ def pretranslate(self, project_pk, locales=None, entities=None):
 
     translations = Translation.objects.bulk_create(translations)
 
+    # Log creating actions
+    actions_to_log = [
+        ActionLog(
+            action_type=ActionLog.ActionType.TRANSLATION_CREATED,
+            performed_by=t.user,
+            translation=t,
+        )
+        for t in translations
+    ]
+
+    ActionLog.objects.bulk_create(actions_to_log)
+
     # Run checks on all translations
     translation_pks = {translation.pk for translation in translations}
     bulk_run_checks(Translation.objects.for_checks().filter(pk__in=translation_pks))


Test patch: