Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 23419a2

Browse files
committed
Export plans and tiers to CSV
1 parent 714ba37 commit 23419a2

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

codecov_auth/admin.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
import csv
12
import logging
23
from datetime import timedelta
34
from typing import Optional, Sequence
4-
55
import django.forms as forms
66
from django.conf import settings
77
from django.contrib import admin, messages
88
from django.contrib.admin.models import LogEntry
99
from django.db.models import OuterRef, Subquery
1010
from django.db.models.fields import BLANK_CHOICE_DASH
1111
from django.forms import CheckboxInput, Select, Textarea
12-
from django.http import HttpRequest
12+
from django.http import HttpRequest, HttpResponse
1313
from django.shortcuts import redirect, render
1414
from django.utils import timezone
1515
from django.utils.html import format_html
@@ -734,9 +734,25 @@ class PlansInline(admin.TabularInline):
734734
Plan._meta.get_field("benefits"): {"widget": Textarea(attrs={"rows": 3})},
735735
}
736736

737+
def export_to_csv(modeladmin, request, queryset):
738+
model = queryset.model
739+
response = HttpResponse(content_type="text/csv")
740+
response["Content-Disposition"] = f'attachment; filename="{model._meta.model_name}s.csv"'
741+
writer = csv.writer(response)
742+
743+
writer.writerow([field.name for field in model._meta.fields])
744+
745+
for obj in queryset:
746+
writer.writerow([getattr(obj, field.name) for field in model._meta.fields])
747+
748+
return response
749+
750+
export_to_csv.short_description = "Export selected items to CSV"
751+
737752

738753
@admin.register(Tier)
739754
class TierAdmin(admin.ModelAdmin):
755+
actions = [export_to_csv]
740756
list_display = (
741757
"tier_name",
742758
"bundle_analysis",
@@ -791,6 +807,8 @@ def clean_monthly_uploads_limit(self) -> int | None:
791807
@admin.register(Plan)
792808
class PlanAdmin(admin.ModelAdmin):
793809
form = PlanAdminForm
810+
actions = [export_to_csv]
811+
794812
list_display = (
795813
"name",
796814
"marketing_name",

0 commit comments

Comments
 (0)