PZ, next steps.
[wolnelektury.git] / src / pz / admin.py
1 from django.contrib import admin
2 from django.contrib import messages
3 from django.shortcuts import get_object_or_404, redirect
4 from django.urls import path, reverse
5 from django.utils.safestring import mark_safe
6 from django.utils.translation import gettext_lazy as _
7 from django.utils.timezone import now
8 from fnpdjango.actions import export_as_csv_action
9 from . import bank
10 from . import models
11
12
13 admin.site.register(models.Fundraiser)
14 admin.site.register(models.Campaign)
15
16
17 class BankExportFeedbackLineInline(admin.TabularInline):
18     model = models.BankExportFeedbackLine
19     extra = 0
20
21
22 @admin.register(models.DirectDebit)
23 class DirectDebitAdmin(admin.ModelAdmin):
24     list_display = [
25         'payment_id', 'acquisition_date',
26         'iban_valid',
27         'bank_submission_date',
28         'bank_acceptance_date',
29         'amount', 'first_name', 'last_name',
30     ]
31     date_hierarchy = 'acquisition_date'
32     search_fields = [
33         'payment_id', 'first_name', 'last_name', 'street', 'building', 'town', 'flat',
34         'phone', 'email', 'iban',
35         'notes',
36         'fundraiser_bill',
37     ]
38     list_filter = [
39         'iban_valid',
40         'agree_fundraising',
41         'agree_newsletter',
42         'fundraiser',
43         'campaign',
44         'is_cancelled',
45         'needs_redo',
46         'optout',
47         'amount',
48         'sex',
49         'is_consumer',
50     ]
51     fieldsets = [
52         (None, {
53             "fields": [
54                 ('first_name', 'sex', 'date_of_birth'),
55                 'last_name',
56                 ('street', 'building'),
57                 ('town', 'flat'),
58                 ('postal_code', 'phone'),
59                 'email',
60                 ('iban', 'iban_valid', 'iban_warning'),
61                 'payment_id',
62                 'agree_contact',
63                 'agree_fundraising',
64                 'agree_newsletter',
65                 ('acquisition_date', 'amount'),
66                 'is_consumer',
67                 'fundraiser',
68                 'campaign',
69             ]
70         }),
71         (_('Processing'), {"fields": [
72             ('is_cancelled', 'needs_redo', 'optout'),
73             'submission_date',
74             'fundraiser_commission',
75             'fundraiser_bill',
76             'bank_submission_date',
77             'bank_acceptance_date',
78             'notes',
79             ]
80         })
81     ]
82     readonly_fields = ['agree_contact', 'iban_valid', 'iban_warning']
83     inlines = [BankExportFeedbackLineInline]
84
85     def set_bank_submission(m, r, q):
86         q.update(bank_submission_date=now())
87
88     def create_bank_order(m, request, queryset):
89         bo = models.BankOrder.objects.create()
90         bo.debits.set(queryset)
91         messages.info(request, mark_safe(
92             '<a href="{}">Bank order</a> created.'.format(
93                 reverse('admin:pz_bankorder_change', args=[bo.pk])
94             )
95         ))
96
97
98     actions = [
99         bank.bank_export,
100         set_bank_submission,
101         create_bank_order,
102         export_as_csv_action(),
103     ]
104
105     def agree_contact(self, obj):
106         return _('obligatory')
107     agree_contact.short_description = _('agree contact')
108
109     def get_changeform_initial_data(self, request):
110         return {
111             'payment_id': models.DirectDebit.get_next_payment_id(),
112         }
113
114
115 @admin.register(models.BankExportFeedback)
116 class BankExportFeedbackAdmin(admin.ModelAdmin):
117     inlines = [BankExportFeedbackLineInline]
118
119
120
121 @admin.register(models.BankOrder)
122 class BankOrderAdmin(admin.ModelAdmin):
123     fields = ['payment_date', 'debits', 'sent', 'download']
124     filter_horizontal = ['debits']
125
126     def get_readonly_fields(self, request, obj=None):
127         fields = ['download']
128         if obj is not None and obj.sent:
129             fields += ['debits', 'payment_date']
130         return fields
131
132     def download(self, obj):
133         return mark_safe('<a href="{}">Download</a>'.format(
134             reverse('admin:pz_bankorder_download', args=[obj.pk])
135         ))
136
137     def get_urls(self):
138         urls = super().get_urls()
139         my_urls = [
140             path(
141                 '<int:pk>/download/',
142                 self.admin_site.admin_view(self.download_view),
143                 name='pz_bankorder_download',
144             ),
145         ]
146         return my_urls + urls
147
148     def download_view(self, request, pk):
149         order = get_object_or_404(
150             models.BankOrder, pk=pk)
151         try:
152             return bank.bank_order(order.payment_date, order.debits.all())
153         except Exception as e:
154             messages.error(request, str(e))
155             return redirect('admin:pz_bankorder_change', pk)