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
13 admin.site.register(models.Fundraiser)
14 admin.site.register(models.Campaign)
17 class BankExportFeedbackLineInline(admin.TabularInline):
18 model = models.BankExportFeedbackLine
22 @admin.register(models.DirectDebit)
23 class DirectDebitAdmin(admin.ModelAdmin):
25 'payment_id', 'acquisition_date',
27 'bank_submission_date',
28 'bank_acceptance_date',
29 'amount', 'first_name', 'last_name',
31 date_hierarchy = 'acquisition_date'
33 'payment_id', 'first_name', 'last_name', 'street', 'building', 'town', 'flat',
34 'phone', 'email', 'iban',
54 ('first_name', 'sex', 'date_of_birth'),
56 ('street', 'building'),
58 ('postal_code', 'phone'),
60 ('iban', 'iban_valid', 'iban_warning'),
65 ('acquisition_date', 'amount'),
71 (_('Processing'), {"fields": [
72 ('is_cancelled', 'needs_redo', 'optout'),
74 'fundraiser_commission',
76 'bank_submission_date',
77 'bank_acceptance_date',
82 readonly_fields = ['agree_contact', 'iban_valid', 'iban_warning']
83 inlines = [BankExportFeedbackLineInline]
85 def set_bank_submission(m, r, q):
86 q.update(bank_submission_date=now())
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])
102 export_as_csv_action(),
105 def agree_contact(self, obj):
106 return _('obligatory')
107 agree_contact.short_description = _('agree contact')
109 def get_changeform_initial_data(self, request):
111 'payment_id': models.DirectDebit.get_next_payment_id(),
115 @admin.register(models.BankExportFeedback)
116 class BankExportFeedbackAdmin(admin.ModelAdmin):
117 inlines = [BankExportFeedbackLineInline]
121 @admin.register(models.BankOrder)
122 class BankOrderAdmin(admin.ModelAdmin):
123 fields = ['payment_date', 'debits', 'sent', 'download']
124 filter_horizontal = ['debits']
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']
132 def download(self, obj):
133 return mark_safe('<a href="{}">Download</a>'.format(
134 reverse('admin:pz_bankorder_download', args=[obj.pk])
138 urls = super().get_urls()
141 '<int:pk>/download/',
142 self.admin_site.admin_view(self.download_view),
143 name='pz_bankorder_download',
146 return my_urls + urls
148 def download_view(self, request, pk):
149 order = get_object_or_404(
150 models.BankOrder, pk=pk)
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)