Skip to content

Commit 8a5b067

Browse files
committed
implement CSV export of SUSHI credentials from django admin
1 parent 6d9b450 commit 8a5b067

File tree

6 files changed

+413
-231
lines changed

6 files changed

+413
-231
lines changed

apps/sushi/admin.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,56 @@
11
from django.contrib import admin, messages
22
from 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
46
from reversion.admin import VersionAdmin
57

68
from logs.logic.attempt_import import reprocess_attempt
79
from logs.models import ImportBatch
810
from . 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):

config/settings/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
'error_report',
5656
'cachalot',
5757
'django_prometheus',
58+
'import_export',
5859
]
5960

6061
MIDDLEWARE = [

0 commit comments

Comments
 (0)