Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(surveys): copy translations when duplicating to another project (#82779)

Phillc1885c3efe00
diff --git a/products/surveys/backend/api/survey.py b/products/surveys/backend/api/survey.py
index 74697c20134f..7da5808d95f7 100644
--- a/products/surveys/backend/api/survey.py
+++ b/products/surveys/backend/api/survey.py
@@ -3192,8 +3192,11 @@ class SurveyViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.
"name": f"{source_survey.name} (duplicated at {duplicate_timestamp})",
"description": source_survey.description,
"type": source_survey.type,
+ # Per-question translations are stripped here and restored verbatim after
+ # validation (see below), for the same reason as the survey-level translations.
"questions": [
- {k: v for k, v in q.items() if k != "id"} for q in (source_survey.questions or [])
+ {k: v for k, v in q.items() if k not in ("id", "translations")}
+ for q in (source_survey.questions or [])
],
"appearance": source_survey.appearance,
"conditions": cleaned_conditions,
@@ -3226,8 +3229,23 @@ class SurveyViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.
new_survey = Survey(
team=team,
created_by=user,
+ # Copied outside the serializer so a duplicate is never stricter than its source:
+ # these values are already sanitized, and surveys with grandfathered language keys
+ # (which only validate against an existing instance) would otherwise fail to copy.
+ base_language=source_survey.base_language,
+ translations=source_survey.translations,
**serializer.validated_data,
)
+
+ # Restore per-question translations verbatim. The create serializer grandfathers legacy
+ # language keys only against an existing instance, so re-validating them here would 400
+ # a copy the source already saved. Order matches: validate_questions keeps input order.
+ source_questions = source_survey.questions or []
+ for index, question in enumerate(new_survey.questions or []):
+ source_translations = source_questions[index].get("translations")
+ if source_translations is not None:
+ question["translations"] = source_translations
+
surveys_to_create.append(new_survey)
for survey in surveys_to_create:
diff --git a/products/surveys/backend/api/test/test_survey.py b/products/surveys/backend/api/test/test_survey.py
index 282e7e4c8f75..6918c7df420b 100644
--- a/products/surveys/backend/api/test/test_survey.py
+++ b/products/surveys/backend/api/test/test_survey.py
@@ -6521,6 +6521,86 @@ class TestSurveyBulkDuplication(APIBaseTest):
# Generic condition fields SHOULD be copied
assert duplicated.conditions.get("url") == "https://example.com"
+ @parameterized.expand(
+ [
+ (
+ "standard_keys",
+ {"fr": {"name": "Sondage"}, "es-MX": {"name": "Encuesta"}},
+ {"fr": {"question": "Qu'en pensez-vous?"}},
+ ),
+ # Legacy keys predate language-code validation and only survive edits via grandfathering, which
+ # keys off an existing instance. Duplication is a create, so routing these through the serializer
+ # would reject "english" as an invalid code and 400 the whole batch.
+ (
+ "legacy_keys",
+ {"english": {"name": "Survey"}},
+ {"english": {"question": "What do you think?"}},
+ ),
+ ]
+ )
+ def test_bulk_duplicate_copies_translations(
+ self, _name: str, survey_translations: dict, question_translations: dict
+ ) -> None:
+ translated_survey = Survey.objects.create(
+ team=self.team,
+ name="Translated Survey",
+ type="popover",
+ questions=[
+ {"type": "open", "question": "What do you think?", "translations": question_translations},
+ {"type": "open", "question": "Any other feedback?"},
+ ],
+ base_language="en-GB",
+ translations=survey_translations,
+ created_by=self.user,
+ )
+
+ response = self.client.post(
+ f"/api/projects/{self.team.project_id}/surveys/{translated_survey.id}/duplicate_to_projects/",
+ data={"target_team_ids": [self.team2.id]},
+ format="json",
+ )
+
+ assert response.status_code == status.HTTP_201_CREATED
+
+ duplicated = Survey.objects.get(team=self.team2)
+ assert duplicated.base_language == "en-GB"
+ assert duplicated.translations == survey_translations
+ assert duplicated.questions is not None
+ # Per-question translations are restored onto the right question, and questions without any are untouched.
+ assert duplicated.questions[0]["translations"] == question_translations
+ assert "translations" not in duplicated.questions[1]
+
+ def test_bulk_duplicate_copies_question_translation_matching_default_base_language(self) -> None:
+ # A non-English base language with an "en" question translation is valid at the source, but the create
+ # serializer resolves base_language to the default "en" and would reject "en" as colliding with it.
+ translated_survey = Survey.objects.create(
+ team=self.team,
+ name="French Survey",
+ type="popover",
+ questions=[
+ {
+ "type": "open",
+ "question": "Qu'en pensez-vous?",
+ "translations": {"en": {"question": "What do you think?"}},
+ }
+ ],
+ base_language="fr",
+ created_by=self.user,
+ )
+
+ response = self.client.post(
+ f"/api/projects/{self.team.project_id}/surveys/{translated_survey.id}/duplicate_to_projects/",
+ data={"target_team_ids": [self.team2.id]},
+ format="json",
+ )
+
+ assert response.status_code == status.HTTP_201_CREATED
+
+ duplicated = Survey.objects.get(team=self.team2)
+ assert duplicated.base_language == "fr"
+ assert duplicated.questions is not None
+ assert duplicated.questions[0]["translations"] == {"en": {"question": "What do you think?"}}
+
def test_bulk_duplicate_transaction_rollback_on_error(self):
"""Test that all duplications are rolled back if one fails"""
# Create a survey with the same name in team2 to cause a conflict