1
1
from django .contrib import admin
2
2
from core .admin import ExportCSVMixin
3
+ import csv
4
+ from django .http import HttpResponse
3
5
4
6
from participant .models import *
5
7
@@ -16,18 +18,51 @@ def get_national_code(self, obj):
16
18
17
19
18
20
@admin .register (Participation )
19
- class ParticipationAdmin (admin .ModelAdmin , ExportCSVMixin ):
21
+ class ParticipationAdmin (admin .ModelAdmin ):
20
22
list_display = ('participant' , 'participant_email' , 'plan' )
21
23
list_filter = ('plan' , 'plan__event__order' ,)
22
24
autocomplete_fields = ('participant' , 'info' ,)
23
25
search_fields = ('participant__user__email' , 'participant__info__national_code' ,)
24
- actions = ["export_as_csv " ]
26
+ actions = ["export_data " ]
25
27
26
28
def participant_email (self , obj ):
27
29
return obj .participant .user .email if obj .participant and obj .participant .user else ""
28
30
29
31
participant_email .short_description = "Email"
30
32
33
+ def export_data (self , request , queryset ):
34
+ meta = self .model ._meta
35
+ response = HttpResponse (content_type = 'text/csv' )
36
+ response ['Content-Disposition' ] = 'attachment; filename=participation_data.csv'
37
+ writer = csv .writer (response )
38
+
39
+ headers = [
40
+ 'Participation Id' ,
41
+ 'Email' ,
42
+ 'First Name' ,
43
+ 'Last Name' ,
44
+ 'National Code' ,
45
+ 'Phone Number' ,
46
+ 'Plan'
47
+ ]
48
+ writer .writerow (headers )
49
+
50
+ for obj in queryset :
51
+ row = [
52
+ obj .id if obj .id else '' ,
53
+ obj .participant .user .email if obj .participant and obj .participant .user else '' ,
54
+ obj .participant .info .first_name if obj .participant and obj .participant .info else '' ,
55
+ obj .participant .info .last_name if obj .participant and obj .participant .info else '' ,
56
+ obj .participant .info .national_code if obj .participant and obj .participant .info else '' ,
57
+ obj .participant .info .phone_number if obj .participant and obj .participant .info else '' ,
58
+ str (obj .plan ) if obj .plan else ''
59
+ ]
60
+ writer .writerow (row )
61
+
62
+ return response
63
+
64
+ export_data .short_description = "Export Registration Data As CSV"
65
+
31
66
32
67
@admin .register (ParticipantInfo )
33
68
class ParticipantInfoAdmin (admin .ModelAdmin ):
0 commit comments