blackout
[wolnelektury.git] / src / pz / admin.py
index 6c51aef..daacdce 100644 (file)
@@ -1,9 +1,10 @@
 from django.contrib import admin
 from django.contrib import admin
+from django.contrib.admin.filters import FieldListFilter, SimpleListFilter
 from django.contrib import messages
 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
 from django.shortcuts import get_object_or_404, redirect
 from django.urls import path, reverse
 from django.utils.safestring import mark_safe
-from django.utils.translation import gettext_lazy as _
 from django.utils.timezone import now
 from fnpdjango.actions import export_as_csv_action
 from . import bank
 from django.utils.timezone import now
 from fnpdjango.actions import export_as_csv_action
 from . import bank
@@ -14,16 +15,77 @@ admin.site.register(models.Fundraiser)
 admin.site.register(models.Campaign)
 
 
 admin.site.register(models.Campaign)
 
 
+# Backport from Django 3.1.
+class EmptyFieldListFilter(FieldListFilter):
+    with_empty_str = False
+
+    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.with_empty_str:
+            lookup_condition |= Q(**{self.field_path: ''})
+        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, 'Wszystkie'),
+            ('1', 'Puste'),
+            ('0', 'Niepuste'),
+        ):
+            yield {
+                'selected': self.lookup_val == lookup,
+                'query_string': changelist.get_query_string({self.lookup_kwarg: lookup}),
+                'display': title,
+            }
+
+
+class PayedListFilter(SimpleListFilter):
+    title = 'pobrane'
+    parameter_name = 'payed'
+    def lookups(self, request, model_admin):
+        return (
+                ('yes', 'tak'),
+                ('no', 'nie'),
+                )
+
+    def queryset(self, request, queryset):
+        if self.value() == 'yes':
+            return queryset.filter(payment__is_dd=True, payment__realised=True).distinct()
+        if self.value() == 'no':
+            return queryset.exclude(payment__is_dd=True, payment__realised=True).distinct()
+
+
+
 class BankExportFeedbackLineInline(admin.TabularInline):
     model = models.BankExportFeedbackLine
     extra = 0
 
 class BankExportFeedbackLineInline(admin.TabularInline):
     model = models.BankExportFeedbackLine
     extra = 0
 
+class BankPaymentInline(admin.TabularInline):
+    model = models.Payment
+    extra = 0
 
 @admin.register(models.DirectDebit)
 class DirectDebitAdmin(admin.ModelAdmin):
 
 @admin.register(models.DirectDebit)
 class DirectDebitAdmin(admin.ModelAdmin):
+    #unpaginate
+
     list_display = [
         'payment_id', 'acquisition_date',
         'iban_valid',
     list_display = [
         'payment_id', 'acquisition_date',
         'iban_valid',
+        'latest_status',
         'bank_submission_date',
         'bank_acceptance_date',
         'amount', 'first_name', 'last_name',
         'bank_submission_date',
         'bank_acceptance_date',
         'amount', 'first_name', 'last_name',
@@ -41,12 +103,16 @@ class DirectDebitAdmin(admin.ModelAdmin):
         'agree_newsletter',
         'fundraiser',
         'campaign',
         'agree_newsletter',
         'fundraiser',
         'campaign',
-        'is_cancelled',
+        ('cancelled_at', EmptyFieldListFilter),
+        'latest_status',
         'needs_redo',
         'optout',
         'amount',
         'sex',
         'is_consumer',
         'needs_redo',
         'optout',
         'amount',
         'sex',
         'is_consumer',
+        ('fundraiser_commission', EmptyFieldListFilter),
+        ('fundraiser_bonus', EmptyFieldListFilter),
+        PayedListFilter,
     ]
     fieldsets = [
         (None, {
     ]
     fieldsets = [
         (None, {
@@ -58,7 +124,7 @@ class DirectDebitAdmin(admin.ModelAdmin):
                 ('postal_code', 'phone'),
                 'email',
                 ('iban', 'iban_valid', 'iban_warning'),
                 ('postal_code', 'phone'),
                 'email',
                 ('iban', 'iban_valid', 'iban_warning'),
-                'payment_id',
+                ('payment_id', 'latest_status'),
                 'agree_contact',
                 'agree_fundraising',
                 'agree_newsletter',
                 'agree_contact',
                 'agree_fundraising',
                 'agree_newsletter',
@@ -68,10 +134,11 @@ class DirectDebitAdmin(admin.ModelAdmin):
                 'campaign',
             ]
         }),
                 'campaign',
             ]
         }),
-        (_('Processing'), {"fields": [
-            ('is_cancelled', 'needs_redo', 'optout'),
+        ('Przetwarzanie', {"fields": [
+            ('cancelled_at', 'needs_redo', 'optout'),
             'submission_date',
             'fundraiser_commission',
             'submission_date',
             'fundraiser_commission',
+            'fundraiser_bonus',
             'fundraiser_bill',
             'bank_submission_date',
             'bank_acceptance_date',
             'fundraiser_bill',
             'bank_submission_date',
             'bank_acceptance_date',
@@ -79,8 +146,8 @@ class DirectDebitAdmin(admin.ModelAdmin):
             ]
         })
     ]
             ]
         })
     ]
-    readonly_fields = ['agree_contact', 'iban_valid', 'iban_warning']
-    inlines = [BankExportFeedbackLineInline]
+    readonly_fields = ['agree_contact', 'iban_valid', 'iban_warning', 'latest_status']
+    inlines = [BankExportFeedbackLineInline, BankPaymentInline]
 
     def set_bank_submission(m, r, q):
         q.update(bank_submission_date=now())
 
     def set_bank_submission(m, r, q):
         q.update(bank_submission_date=now())
@@ -103,8 +170,8 @@ class DirectDebitAdmin(admin.ModelAdmin):
     ]
 
     def agree_contact(self, obj):
     ]
 
     def agree_contact(self, obj):
-        return _('obligatory')
-    agree_contact.short_description = _('agree contact')
+        return 'obowiązkowo'
+    agree_contact.short_description = 'zgoda na kontakt'
 
     def get_changeform_initial_data(self, request):
         return {
 
     def get_changeform_initial_data(self, request):
         return {
@@ -114,7 +181,7 @@ class DirectDebitAdmin(admin.ModelAdmin):
 
 @admin.register(models.BankExportFeedback)
 class BankExportFeedbackAdmin(admin.ModelAdmin):
 
 @admin.register(models.BankExportFeedback)
 class BankExportFeedbackAdmin(admin.ModelAdmin):
-    inlines = [BankExportFeedbackLineInline]
+    inlines = [BankExportFeedbackLineInline, BankPaymentInline]
 
 
 
 
 
 
@@ -130,9 +197,12 @@ class BankOrderAdmin(admin.ModelAdmin):
         return fields
 
     def download(self, obj):
         return fields
 
     def download(self, obj):
-        return mark_safe('<a href="{}">Download</a>'.format(
-            reverse('admin:pz_bankorder_download', args=[obj.pk])
-        ))
+        if obj is not None and obj.pk:
+            return mark_safe('<a href="{}">Download</a>'.format(
+                reverse('admin:pz_bankorder_download', args=[obj.pk])
+            ))
+        else:
+            return ''
 
     def get_urls(self):
         urls = super().get_urls()
 
     def get_urls(self):
         urls = super().get_urls()
@@ -149,7 +219,11 @@ class BankOrderAdmin(admin.ModelAdmin):
         order = get_object_or_404(
             models.BankOrder, pk=pk)
         try:
         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)
         except Exception as e:
             messages.error(request, str(e))
             return redirect('admin:pz_bankorder_change', pk)