from django.contrib import admin
+from django.contrib.admin.filters import FieldListFilter
from django.contrib import messages
+from django.db.models import Q
from django.shortcuts import get_object_or_404, redirect
from django.urls import path, reverse
from django.utils.safestring import mark_safe
admin.site.register(models.Campaign)
+# Backport from Django 3.1.
+class EmptyFieldListFilter(FieldListFilter):
+ def __init__(self, field, request, params, model, model_admin, field_path):
+ self.lookup_kwarg = '%s__isempty' % field_path
+ self.lookup_val = params.get(self.lookup_kwarg)
+ super().__init__(field, request, params, model, model_admin, field_path)
+
+ def queryset(self, request, queryset):
+ if self.lookup_kwarg not in self.used_parameters:
+ return queryset
+ if self.lookup_val not in ('0', '1'):
+ raise IncorrectLookupParameters
+
+ lookup_condition = Q(**{'%s__isnull' % self.field_path: True})
+ if self.lookup_val == '1':
+ return queryset.filter(lookup_condition)
+ return queryset.exclude(lookup_condition)
+
+ def expected_parameters(self):
+ return [self.lookup_kwarg]
+
+ def choices(self, changelist):
+ for lookup, title in (
+ (None, _('All')),
+ ('1', _('Empty')),
+ ('0', _('Not empty')),
+ ):
+ yield {
+ 'selected': self.lookup_val == lookup,
+ 'query_string': changelist.get_query_string({self.lookup_kwarg: lookup}),
+ 'display': title,
+ }
+
+
class BankExportFeedbackLineInline(admin.TabularInline):
model = models.BankExportFeedbackLine
extra = 0
'agree_newsletter',
'fundraiser',
'campaign',
- 'is_cancelled',
+ ('cancelled_at', EmptyFieldListFilter),
'needs_redo',
'optout',
'amount',
]
}),
(_('Processing'), {"fields": [
- ('is_cancelled', 'needs_redo', 'optout'),
+ ('cancelled_at', 'needs_redo', 'optout'),
'submission_date',
'fundraiser_commission',
'fundraiser_bill',
order = get_object_or_404(
models.BankOrder, pk=pk)
try:
- return bank.bank_order(order.payment_date, order.debits.all())
+ return bank.bank_order(
+ order.payment_date,
+ order.sent,
+ order.debits.all()
+ )
except Exception as e:
messages.error(request, str(e))
return redirect('admin:pz_bankorder_change', pk)
yield payment_id, status, comment
-def bank_order(date, queryset):
+def bank_order(date, sent_at, queryset):
response = HttpResponse(content_type='application/octet-stream')
response['Content-Disposition'] = 'attachment; filename=order.PLD'
rows = []
no_dates = []
no_amounts = []
+ cancelled = []
if date is None:
raise ValueError('Payment date not set yet.')
no_dates.append(debit)
if debit.amount is None:
no_amounts.append(debit)
+ if debit.cancelled_at and debit.cancelled_at.date() <= date and (sent_at is None or debit.cancelled_at < sent_at):
+ cancelled.append(debit)
- if no_dates or no_amounts:
+ if no_dates or no_amounts or cancelled:
t = ''
if no_dates:
t += 'Bank acceptance not received for: '
for debit in no_amounts
)
t += '. '
+ if cancelled:
+ t += 'Debits have been cancelled: '
+ t += ', '.join(
+ '<a href="/admin/pz/directdebit/{}/change">{}</a>'.format(
+ debit.pk, debit
+ )
+ for debit in cancelled
+ )
+ t += '. '
raise ValueError(mark_safe(t))
for debit in queryset:
--- /dev/null
+# Generated by Django 2.2.19 on 2021-10-14 09:00
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('pz', '0006_bankorder'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='directdebit',
+ name='cancelled_at',
+ field=models.DateTimeField(blank=True, null=True, verbose_name='cancelled at'),
+ ),
+ migrations.AlterField(
+ model_name='bankorder',
+ name='debits',
+ field=models.ManyToManyField(blank=True, to='pz.DirectDebit'),
+ ),
+ ]
--- /dev/null
+# Generated by Django 2.2.19 on 2021-10-14 09:01
+
+from django.db import migrations
+from django.utils.timezone import now
+
+
+def fill_cancelled_date(apps, schema_editor):
+ DirectDebit = apps.get_model('pz', 'DirectDebit')
+ DirectDebit.objects.filter(is_cancelled=True).update(
+ cancelled_at=now()
+ )
+
+
+def fill_is_cancelled(apps, schema_editor):
+ DirectDebit = apps.get_model('pz', 'DirectDebit')
+ DirectDebit.objects.exclude(cancelled_at=None).update(
+ is_cancelled=True
+ )
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('pz', '0007_auto_20211014_1100'),
+ ]
+
+ operations = [
+ migrations.RunPython(
+ fill_cancelled_date,
+ fill_is_cancelled
+ )
+ ]
--- /dev/null
+# Generated by Django 2.2.19 on 2021-10-14 09:04
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('pz', '0008_fill_cancelled_date'),
+ ]
+
+ operations = [
+ migrations.RemoveField(
+ model_name='directdebit',
+ name='is_cancelled',
+ ),
+ ]
notes = models.TextField(_('notes'), blank=True)
needs_redo = models.BooleanField(_('needs redo'), default=False)
- is_cancelled = models.BooleanField(_('is cancelled'), default=False)
+ cancelled_at = models.DateTimeField(_('cancelled at'), null=True, blank=True)
optout = models.BooleanField(_('optout'), default=False)
campaign = models.ForeignKey(Campaign, models.PROTECT, null=True, blank=True, verbose_name=_('campaign'))