11from django .contrib import admin , messages
22from django .db .transaction import atomic
3-
3+ from import_export .admin import ExportActionMixin
4+ from import_export .fields import Field
5+ from import_export .resources import ModelResource
46from reversion .admin import VersionAdmin
57
68from logs .logic .attempt_import import reprocess_attempt
79from logs .models import ImportBatch
810from . import models
911
1012
13+ class SushiCredentialsResource (ModelResource ):
14+ """
15+ This is used by django-import-export to facilitate CSV export in Django admin
16+ """
17+
18+ platform__name = Field (attribute = 'platform__name' , column_name = 'platform' )
19+ organization__name = Field (attribute = 'organization__name' , column_name = 'organization' )
20+ active_counter_reports = Field (
21+ attribute = 'active_counter_reports' , column_name = 'counter_reports'
22+ )
23+
24+ class Meta :
25+ model = models .SushiCredentials
26+ fields = (
27+ 'id' ,
28+ 'organization__name' ,
29+ 'platform__name' ,
30+ 'url' ,
31+ 'counter_version' ,
32+ 'requestor_id' ,
33+ 'customer_id' ,
34+ 'http_username' ,
35+ 'http_password' ,
36+ 'api_key' ,
37+ 'extra_params' ,
38+ 'active_counter_reports' ,
39+ )
40+ export_order = fields
41+
42+ def dehydrate_active_counter_reports (self , credentials : models .SushiCredentials ):
43+ return ', ' .join ([cr .code for cr in credentials .active_counter_reports .all ()])
44+
45+ def dehydrate_platform__name (self , credentials : models .SushiCredentials ):
46+ return credentials .platform .name or credentials .platform .short_name
47+
48+ def dehydrate_organization__name (self , credentials : models .SushiCredentials ):
49+ return credentials .organization .name or credentials .organization .short_name
50+
51+
1152@admin .register (models .SushiCredentials )
12- class SushiCredentialsAdmin (VersionAdmin ):
53+ class SushiCredentialsAdmin (ExportActionMixin , VersionAdmin ):
1354
1455 list_display = [
1556 'organization' ,
@@ -22,6 +63,7 @@ class SushiCredentialsAdmin(VersionAdmin):
2263 'when_can_access' ,
2364 ]
2465 list_filter = ['enabled' , 'counter_version' , 'organization' , 'platform' ]
66+ resource_class = SushiCredentialsResource
2567
2668 @classmethod
2769 def organization_internal_id (cls , obj : models .SushiCredentials ):
0 commit comments