1 from django.contrib import admin
2 from django.contrib.admin.filters import FieldListFilter
3 from django.contrib import messages
4 from django.db.models import Q
5 from django.shortcuts import get_object_or_404, redirect
6 from django.urls import path, reverse
7 from django.utils.safestring import mark_safe
8 from django.utils.translation import gettext_lazy as _
9 from django.utils.timezone import now
10 from fnpdjango.actions import export_as_csv_action
15 admin.site.register(models.Fundraiser)
16 admin.site.register(models.Campaign)
19 # Backport from Django 3.1.
20 class EmptyFieldListFilter(FieldListFilter):
21 def __init__(self, field, request, params, model, model_admin, field_path):
22 self.lookup_kwarg = '%s__isempty' % field_path
23 self.lookup_val = params.get(self.lookup_kwarg)
24 super().__init__(field, request, params, model, model_admin, field_path)
26 def queryset(self, request, queryset):
27 if self.lookup_kwarg not in self.used_parameters:
29 if self.lookup_val not in ('0', '1'):
30 raise IncorrectLookupParameters
32 lookup_condition = Q(**{'%s__isnull' % self.field_path: True})
33 if self.lookup_val == '1':
34 return queryset.filter(lookup_condition)
35 return queryset.exclude(lookup_condition)
37 def expected_parameters(self):
38 return [self.lookup_kwarg]
40 def choices(self, changelist):
41 for lookup, title in (
44 ('0', _('Not empty')),
47 'selected': self.lookup_val == lookup,
48 'query_string': changelist.get_query_string({self.lookup_kwarg: lookup}),
53 class BankExportFeedbackLineInline(admin.TabularInline):
54 model = models.BankExportFeedbackLine
58 @admin.register(models.DirectDebit)
59 class DirectDebitAdmin(admin.ModelAdmin):
61 'payment_id', 'acquisition_date',
63 'bank_submission_date',
64 'bank_acceptance_date',
65 'amount', 'first_name', 'last_name',
67 date_hierarchy = 'acquisition_date'
69 'payment_id', 'first_name', 'last_name', 'street', 'building', 'town', 'flat',
70 'phone', 'email', 'iban',
80 ('cancelled_at', EmptyFieldListFilter),
90 ('first_name', 'sex', 'date_of_birth'),
92 ('street', 'building'),
94 ('postal_code', 'phone'),
96 ('iban', 'iban_valid', 'iban_warning'),
101 ('acquisition_date', 'amount'),
107 (_('Processing'), {"fields": [
108 ('cancelled_at', 'needs_redo', 'optout'),
110 'fundraiser_commission',
112 'bank_submission_date',
113 'bank_acceptance_date',
118 readonly_fields = ['agree_contact', 'iban_valid', 'iban_warning']
119 inlines = [BankExportFeedbackLineInline]
121 def set_bank_submission(m, r, q):
122 q.update(bank_submission_date=now())
124 def create_bank_order(m, request, queryset):
125 bo = models.BankOrder.objects.create()
126 bo.debits.set(queryset)
127 messages.info(request, mark_safe(
128 '<a href="{}">Bank order</a> created.'.format(
129 reverse('admin:pz_bankorder_change', args=[bo.pk])
138 export_as_csv_action(),
141 def agree_contact(self, obj):
142 return _('obligatory')
143 agree_contact.short_description = _('agree contact')
145 def get_changeform_initial_data(self, request):
147 'payment_id': models.DirectDebit.get_next_payment_id(),
151 @admin.register(models.BankExportFeedback)
152 class BankExportFeedbackAdmin(admin.ModelAdmin):
153 inlines = [BankExportFeedbackLineInline]
157 @admin.register(models.BankOrder)
158 class BankOrderAdmin(admin.ModelAdmin):
159 fields = ['payment_date', 'debits', 'sent', 'download']
160 filter_horizontal = ['debits']
162 def get_readonly_fields(self, request, obj=None):
163 fields = ['download']
164 if obj is not None and obj.sent:
165 fields += ['debits', 'payment_date']
168 def download(self, obj):
169 if obj is not None and obj.pk:
170 return mark_safe('<a href="{}">Download</a>'.format(
171 reverse('admin:pz_bankorder_download', args=[obj.pk])
177 urls = super().get_urls()
180 '<int:pk>/download/',
181 self.admin_site.admin_view(self.download_view),
182 name='pz_bankorder_download',
185 return my_urls + urls
187 def download_view(self, request, pk):
188 order = get_object_or_404(
189 models.BankOrder, pk=pk)
191 return bank.bank_order(
196 except Exception as e:
197 messages.error(request, str(e))
198 return redirect('admin:pz_bankorder_change', pk)