1 from django.contrib import admin
2 from django.contrib.admin.filters import FieldListFilter, SimpleListFilter
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.timezone import now
9 from fnpdjango.actions import export_as_csv_action
14 admin.site.register(models.Fundraiser)
15 admin.site.register(models.Campaign)
18 # Backport from Django 3.1.
19 class EmptyFieldListFilter(FieldListFilter):
20 with_empty_str = False
22 def __init__(self, field, request, params, model, model_admin, field_path):
23 self.lookup_kwarg = '%s__isempty' % field_path
24 self.lookup_val = params.get(self.lookup_kwarg)
25 super().__init__(field, request, params, model, model_admin, field_path)
27 def queryset(self, request, queryset):
28 if self.lookup_kwarg not in self.used_parameters:
30 if self.lookup_val not in ('0', '1'):
31 raise IncorrectLookupParameters
33 lookup_condition = Q(**{'%s__isnull' % self.field_path: True})
34 if self.with_empty_str:
35 lookup_condition |= Q(**{self.field_path: ''})
36 if self.lookup_val == '1':
37 return queryset.filter(lookup_condition)
38 return queryset.exclude(lookup_condition)
40 def expected_parameters(self):
41 return [self.lookup_kwarg]
43 def choices(self, changelist):
44 for lookup, title in (
50 'selected': self.lookup_val == lookup,
51 'query_string': changelist.get_query_string({self.lookup_kwarg: lookup}),
56 class PayedListFilter(SimpleListFilter):
58 parameter_name = 'payed'
59 def lookups(self, request, model_admin):
65 def queryset(self, request, queryset):
66 if self.value() == 'yes':
67 return queryset.filter(payment__is_dd=True, payment__realised=True).distinct()
68 if self.value() == 'no':
69 return queryset.exclude(payment__is_dd=True, payment__realised=True).distinct()
73 class BankExportFeedbackLineInline(admin.TabularInline):
74 model = models.BankExportFeedbackLine
77 class BankPaymentInline(admin.TabularInline):
78 model = models.Payment
81 @admin.register(models.DirectDebit)
82 class DirectDebitAdmin(admin.ModelAdmin):
86 'payment_id', 'acquisition_date',
89 'bank_submission_date',
90 'bank_acceptance_date',
91 'amount', 'first_name', 'last_name',
93 date_hierarchy = 'acquisition_date'
95 'payment_id', 'first_name', 'last_name', 'street', 'building', 'town', 'flat',
96 'phone', 'email', 'iban',
106 ('cancelled_at', EmptyFieldListFilter),
113 ('fundraiser_commission', EmptyFieldListFilter),
114 ('fundraiser_bonus', EmptyFieldListFilter),
121 ('first_name', 'sex', 'date_of_birth'),
123 ('street', 'building'),
125 ('postal_code', 'phone'),
127 ('iban', 'iban_valid', 'iban_warning'),
128 ('payment_id', 'latest_status'),
132 ('acquisition_date', 'amount'),
138 ('Przetwarzanie', {"fields": [
139 ('cancelled_at', 'needs_redo', 'optout'),
141 'fundraiser_commission',
144 'bank_submission_date',
145 'bank_acceptance_date',
150 readonly_fields = ['agree_contact', 'iban_valid', 'iban_warning', 'latest_status']
151 inlines = [BankExportFeedbackLineInline, BankPaymentInline]
153 def set_bank_submission(m, r, q):
154 q.update(bank_submission_date=now())
156 def create_bank_order(m, request, queryset):
157 bo = models.BankOrder.objects.create()
158 bo.debits.set(queryset)
159 messages.info(request, mark_safe(
160 '<a href="{}">Bank order</a> created.'.format(
161 reverse('admin:pz_bankorder_change', args=[bo.pk])
170 export_as_csv_action(),
173 def agree_contact(self, obj):
175 agree_contact.short_description = 'zgoda na kontakt'
177 def get_changeform_initial_data(self, request):
179 'payment_id': models.DirectDebit.get_next_payment_id(),
183 @admin.register(models.BankExportFeedback)
184 class BankExportFeedbackAdmin(admin.ModelAdmin):
185 inlines = [BankExportFeedbackLineInline, BankPaymentInline]
189 @admin.register(models.BankOrder)
190 class BankOrderAdmin(admin.ModelAdmin):
191 fields = ['payment_date', 'debits', 'sent', 'download']
192 filter_horizontal = ['debits']
194 def get_readonly_fields(self, request, obj=None):
195 fields = ['download']
196 if obj is not None and obj.sent:
197 fields += ['debits', 'payment_date']
200 def download(self, obj):
201 if obj is not None and obj.pk:
202 return mark_safe('<a href="{}">Download</a>'.format(
203 reverse('admin:pz_bankorder_download', args=[obj.pk])
209 urls = super().get_urls()
212 '<int:pk>/download/',
213 self.admin_site.admin_view(self.download_view),
214 name='pz_bankorder_download',
217 return my_urls + urls
219 def download_view(self, request, pk):
220 order = get_object_or_404(
221 models.BankOrder, pk=pk)
223 return bank.bank_order(
228 except Exception as e:
229 messages.error(request, str(e))
230 return redirect('admin:pz_bankorder_change', pk)