Fix for carousel management.
[wolnelektury.git] / src / pz / admin.py
index bc1ee8a..1b88383 100644 (file)
@@ -1,5 +1,5 @@
 from django.contrib import admin
 from django.contrib import admin
-from django.contrib.admin.filters import FieldListFilter
+from django.contrib.admin.filters import FieldListFilter, SimpleListFilter
 from django.contrib import messages
 from django.db.models import Q
 from django.shortcuts import get_object_or_404, redirect
 from django.contrib import messages
 from django.db.models import Q
 from django.shortcuts import get_object_or_404, redirect
@@ -18,6 +18,8 @@ admin.site.register(models.Campaign)
 
 # Backport from Django 3.1.
 class EmptyFieldListFilter(FieldListFilter):
 
 # 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)
     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)
@@ -30,6 +32,8 @@ class EmptyFieldListFilter(FieldListFilter):
             raise IncorrectLookupParameters
 
         lookup_condition = Q(**{'%s__isnull' % self.field_path: True})
             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)
         if self.lookup_val == '1':
             return queryset.filter(lookup_condition)
         return queryset.exclude(lookup_condition)
@@ -50,6 +54,23 @@ class EmptyFieldListFilter(FieldListFilter):
             }
 
 
             }
 
 
+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
@@ -90,6 +111,7 @@ class DirectDebitAdmin(admin.ModelAdmin):
         'is_consumer',
         ('fundraiser_commission', EmptyFieldListFilter),
         ('fundraiser_bonus', EmptyFieldListFilter),
         'is_consumer',
         ('fundraiser_commission', EmptyFieldListFilter),
         ('fundraiser_bonus', EmptyFieldListFilter),
+        PayedListFilter,
     ]
     fieldsets = [
         (None, {
     ]
     fieldsets = [
         (None, {