-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathadmin.py
94 lines (69 loc) · 2.57 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from django.contrib import admin
from django.http import HttpResponse
from django.template import loader
from django.views import View
from .models import Campaign, Fundraiser, Donation, Donor
class CampaignAdmin(admin.ModelAdmin):
pass
# Register your models here.
admin.site.register(Campaign)
admin.site.register(Fundraiser)
@admin.register(Donation)
class DonationAdmin(admin.ModelAdmin):
list_display = (
'name', 'date', 'amount', 'payment_status',
'email', 'fundraiser', 'country'
)
search_fields = ('name', 'email')
list_filter = ('payment_status', 'country')
@admin.register(Donor)
class DonorAdmin(admin.ModelAdmin):
"""
Report that shows all the donations, grouped by email and name
so that a single person donating to multiple fundraisers will show
up once. Can be shown as HTML or exported as CSV
"""
change_list_template = 'admin/donation_report.html'
list_filter = ('fundraiser__campaign_id',)
def changelist_view(self, request, extra_content=None):
# call parent class
response = super().changelist_view(
request,
)
# if the user has chosen a campaign using the filter, pass it
campaign_id = request.GET.get('fundraiser__campaign_id__id__exact', '')
response.context_data['campaign_id'] = campaign_id
# get the query string for the Donor proxy model
try:
donations = response.context_data['cl'].queryset
except (AttributeError, KeyError):
return response
# sort by number of donations - for some reason removing this
# stops the grouping ??
donations = donations.order_by('-amount')
response.context_data['summary'] = list(
donations
)
return response
class DonorCsv(View):
"""
CSV export version of the Donor admin report, to be used from the admin
section.
"""
csv_template_name = 'admin/donation_report.csv'
def get(self, request, campaign_id):
# get donations for a single campaign
donations = Donor.objects.filter(fundraiser__campaign_id=campaign_id)
context = {
'donations': donations,
}
# set up the csv content type
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = \
'attachment; filename="donations.csv"'
t = loader.get_template(self.csv_template_name)
context = {
'donations': donations,
}
response.write(t.render(context))
return response