: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(billing): Add button to sync organization information to billingin org admin (#58093)
Marce Coll72ccdcd3ad94
diff --git a/posthog/admin/admins/organization_admin.py b/posthog/admin/admins/organization_admin.pyindex 398c4d97f7a9..90d939522928 100644--- a/posthog/admin/admins/organization_admin.py+++ b/posthog/admin/admins/organization_admin.py@@ -151,6 +151,7 @@ class OrganizationAdmin(admin.ModelAdmin):"limited_products_display","customer_trust_scores","bulk_delete_data_display",+ "sync_to_billing_display","is_active","is_not_active_reason","is_hipaa",@@ -175,6 +176,7 @@ class OrganizationAdmin(admin.ModelAdmin):"limited_products_display","customer_trust_scores","bulk_delete_data_display",+ "sync_to_billing_display",]search_fields = ("name", "members__email", "team__api_token")list_display = (@@ -311,6 +313,40 @@ class OrganizationAdmin(admin.ModelAdmin):url = reverse("admin:organization_model_counts", args=[organization.pk])return format_html('<a class="button" href="{}">View counts</a>', url)+ @admin.display(description="Sync to billing")+ def sync_to_billing_display(self, organization: Organization):+ if not organization.pk:+ return "-"+ request = getattr(self, "_current_request", None)+ # nosemgrep: python.django.security.audit.avoid-mark-safe.avoid-mark-safe (admin-only, renders trusted template)+ return mark_safe(+ render_to_string(+ "admin/organization/sync_to_billing.html",+ {"organization_id": organization.pk},+ request=request,+ )+ )++ def sync_to_billing_view(self, request, organization_id):+ from posthog.tasks.sync_billing import sync_members_to_billing++ try:+ organization = Organization.objects.get(id=organization_id)+ except Organization.DoesNotExist:+ messages.error(request, f"Organization with id {organization_id} not found.")+ return redirect(reverse("admin:posthog_organization_changelist"))++ if request.method == "POST":+ try:+ sync_members_to_billing.delay(organization.id)+ messages.success(+ request, f"Queued sync to billing for organization {organization.name} ({organization.id})."+ )+ except Exception as e:+ messages.error(request, f"Error queuing sync to billing: {e}")++ return redirect(reverse("admin:posthog_organization_change", args=[organization_id]))+def limit_product_view(self, request, organization_id):from ee.billing.quota_limiting import QuotaResource@@ -371,6 +407,11 @@ class OrganizationAdmin(admin.ModelAdmin):self.admin_site.admin_view(self.model_counts_view),name="organization_model_counts",),+ path(+ "<path:organization_id>/sync-to-billing/",+ self.admin_site.admin_view(self.sync_to_billing_view),+ name="organization_sync_to_billing",+ ),]return custom_urls + urlsdiff --git a/posthog/templates/admin/organization/sync_to_billing.html b/posthog/templates/admin/organization/sync_to_billing.htmlnew file mode 100644index 000000000000..4b3bc0fa1668--- /dev/null+++ b/posthog/templates/admin/organization/sync_to_billing.html@@ -0,0 +1,7 @@+<form method="post" action="{% url 'admin:organization_sync_to_billing' organization_id %}" style="display: inline-block;">+ {% csrf_token %}+ <button type="submit" class="button">Sync to billing</button>+</form>+<p style="color: #666; margin-top: 8px;">+ Queues a Celery task that syncs this organization's members to the billing service (equivalent to <code>./manage.py sync_to_billing --action=organization_users --organization-ids=<id> --async=1</code>).+</p>