: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(data-deletion): add admin action to duplicate deletion requests (#67142)
Paweł Szczur5c127e835739
diff --git a/posthog/admin/admins/data_deletion_request_admin.py b/posthog/admin/admins/data_deletion_request_admin.pyindex 538a6f1fd4e2..b4e6a4234842 100644--- a/posthog/admin/admins/data_deletion_request_admin.py+++ b/posthog/admin/admins/data_deletion_request_admin.py@@ -415,6 +415,7 @@ def fetch_deletion_stats(obj: DataDeletionRequest, *, user_id: int | None = None@admin.register(DataDeletionRequest)class DataDeletionRequestAdmin(admin.ModelAdmin):form = DataDeletionRequestForm+ actions = ["duplicate_requests"]list_display = ("id","team_id",@@ -528,6 +529,34 @@ class DataDeletionRequestAdmin(admin.ModelAdmin):),)+ @admin.action(permissions=["add"], description="Duplicate selected requests (as new drafts)")+ def duplicate_requests(self, request, queryset):+ """Copy each selected request into a fresh draft, noting it's a copy of the original."""+ created = 0+ for original in queryset:+ original_url = request.build_absolute_uri(+ reverse("admin:posthog_datadeletionrequest_change", args=[original.pk])+ )+ copy_note = f"Copy of data deletion request {original.pk} ({original_url})."+ notes = f"{copy_note}\n\n{original.notes}" if original.notes else copy_note+ # Build a fresh draft from CRITERIA_FIELDS (the single source of truth) so a new+ # criteria field is copied automatically. Everything operational — stats, approval,+ # execution tracking — falls back to model defaults (DRAFT, no stats, not approved).+ criteria = {field: getattr(original, field) for field in CRITERIA_FIELDS}+ # Shallow-copy mutable list fields so the duplicate never aliases the original's lists.+ criteria = {k: list(v) if isinstance(v, list) else v for k, v in criteria.items()}+ DataDeletionRequest.objects.create(+ **criteria,+ team_id=original.team_id,+ requires_approval=original.requires_approval,+ notes=notes,+ status=RequestStatus.DRAFT,+ created_by=request.user,+ created_by_staff=request.user.is_staff,+ )+ created += 1+ messages.success(request, f"Duplicated {created} request(s) as new draft(s).")+@admin.display(description="Count query (ready to paste)")def rendered_count_query(self, obj: DataDeletionRequest) -> str:"""Show the fully-substituted ClickHouse COUNT query operators can copy/paste."""diff --git a/posthog/admin/test_data_deletion_request_admin.py b/posthog/admin/test_data_deletion_request_admin.pyindex 79cee3e444d7..c4adab66f10a 100644--- a/posthog/admin/test_data_deletion_request_admin.py+++ b/posthog/admin/test_data_deletion_request_admin.py@@ -810,3 +810,60 @@ class TestDataDeletionRequestAdminVerify(BaseTest):self._call_verify(request)request.refresh_from_db()self.assertEqual(request.status, RequestStatus.QUEUED)+++@override_settings(STORAGES={"staticfiles": {"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"}})+class TestDataDeletionRequestAdminDuplicate(BaseTest):+ def setUp(self):+ super().setUp()+ self.user.is_staff = True+ self.user.save()+ self.factory = RequestFactory()+ self.admin = DataDeletionRequestAdmin(DataDeletionRequest, AdminSite())++ def _call_duplicate(self, queryset):+ http_request = self.factory.post("/admin/posthog/datadeletionrequest/")+ http_request.user = self.user+ _attach_messages(http_request)+ with patch("posthog.admin.admins.data_deletion_request_admin.reverse", side_effect=_fake_reverse):+ self.admin.duplicate_requests(http_request, queryset)++ @parameterized.expand(+ [+ ("with_notes", "please be careful"),+ ("without_notes", ""),+ ]+ )+ @freeze_time("2026-01-15")+ def test_duplicate_copies_criteria_into_a_fresh_draft_with_link_note(self, _name, original_notes):+ original = DataDeletionRequest.objects.create(+ team_id=self.team.id,+ request_type=RequestType.EVENT_REMOVAL,+ events=["$pageview"],+ start_time=datetime.now() - timedelta(days=7),+ end_time=datetime.now(),+ notes=original_notes,+ status=RequestStatus.COMPLETED,+ count=42,+ approved=True,+ approved_at=datetime.now(),+ attempt_count=3,+ )++ self._call_duplicate(DataDeletionRequest.objects.filter(pk=original.pk))++ copy = DataDeletionRequest.objects.exclude(pk=original.pk).get()+ self.assertEqual(copy.team_id, original.team_id)+ self.assertEqual(copy.events, ["$pageview"])+ self.assertEqual(copy.status, RequestStatus.DRAFT)+ self.assertIsNone(copy.count)+ self.assertFalse(copy.approved)+ self.assertEqual(copy.attempt_count, 0)+ self.assertEqual(copy.created_by, self.user)+ self.assertIn(str(original.pk), copy.notes)+ self.assertIn("Copy of data deletion request", copy.notes)+ if original_notes:+ self.assertIn(original_notes, copy.notes)+ else:+ # No original notes — the copy note stands alone, no trailing separator.+ self.assertFalse(copy.notes.endswith("\n"))