Browse Source

Refactor member fee filter

- group filters into members queryset
- do no longer use dedicated file (so java ;-))
Jocelyn Delalande 6 years ago
parent
commit
36196e5df6

+ 21 - 1
coin/members/admin.py

@@ -17,7 +17,6 @@ from django.utils.safestring import mark_safe
 
 from coin.members.models import (
     Member, CryptoKey, LdapUser, MembershipFee, Offer, OfferSubscription, RowLevelPermission)
-from coin.members.membershipfee_filter import MembershipFeeFilter
 from coin.members.forms import AdminMemberChangeForm, MemberCreationForm
 from coin.utils import delete_selected
 import autocomplete_light
@@ -84,6 +83,27 @@ class OfferSubscriptionInline(admin.TabularInline):
         return request.user.is_superuser
 
 
+class MembershipFeeFilter(SimpleListFilter):
+    # Human-readable title which will be displayed in the
+    # right admin sidebar just above the filter options.
+    title = 'Cotisations'
+
+    # Parameter for the filter that will be used in the URL query.
+    parameter_name = 'fee'
+
+    def lookups(self, request, model_admin):
+        return (
+            ('paidup', 'À jour de cotisation'),
+            ('late', 'En retard'),
+        )
+
+    def queryset(self, request, queryset):
+        if self.value() == 'paidup':
+            return queryset.paidup_fee()
+        if self.value() == 'late':
+            return queryset.no_fee_or_late()
+
+
 class MemberAdmin(UserAdmin):
     list_display = ('id', 'status', 'username', 'first_name',
                     'name_or_organization_name',

+ 0 - 42
coin/members/membershipfee_filter.py

@@ -1,42 +0,0 @@
-# -*- coding: utf-8 -*-
-from __future__ import unicode_literals
-
-from django.contrib.admin import SimpleListFilter
-import datetime
-
-
-class MembershipFeeFilter(SimpleListFilter):
-    # Human-readable title which will be displayed in the
-    # right admin sidebar just above the filter options.
-    title = 'Cotisations'
-
-    # Parameter for the filter that will be used in the URL query.
-    parameter_name = 'fee'
-
-    def lookups(self, request, model_admin):
-        """
-        Returns a list of tuples. The first element in each
-        tuple is the coded value for the option that will
-        appear in the URL query. The second element is the
-        human-readable name for the option that will appear
-        in the right sidebar.
-        """
-        return (
-            ('paidup', 'À jour de cotisation'),
-            ('late', 'En retard'),
-        )
-
-    def queryset(self, request, queryset):
-        """
-        Returns the filtered queryset based on the value
-        provided in the query string and retrievable via
-        `self.value()`.
-        """
-        if self.value() == 'paidup':
-            return queryset.filter(
-                membership_fees__start_date__lte=datetime.date.today,
-                membership_fees__end_date__gte=datetime.date.today)
-        if self.value() == 'late':
-            return queryset.filter(status='member').exclude(
-                membership_fees__start_date__lte=datetime.date.today,
-                membership_fees__end_date__gte=datetime.date.today)

+ 23 - 0
coin/members/migrations/0018_auto_20180819_0211.py

@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('members', '0017_merge'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='rowlevelpermission',
+            options={'verbose_name': 'permission fine', 'verbose_name_plural': 'permissions fines'},
+        ),
+        migrations.AlterModelManagers(
+            name='member',
+            managers=[
+            ],
+        ),
+    ]

+ 16 - 1
coin/members/models.py

@@ -22,8 +22,23 @@ from coin.mixins import CoinLdapSyncMixin
 from coin import utils
 
 
+class MemberQuerySet(models.QuerySet):
+    paidup_q = Q(
+        # we have at least one fee
+        membership_fees__isnull=False,
+        # and it is still running
+        membership_fees__end_date__gte=datetime.date.today(),)
+
+    def paidup_fee(self):
+        return self.filter(self.paidup_q)
+
+    def no_fee_or_late(self):
+        return self.exclude(self.paidup_q)
+
 
 class MemberManager(UserManager):
+    use_in_migrations = False
+
     def manageable_by(self, user):
         """" Renvoie la liste des members que l'utilisateur est autorisé à voir
         dans l'interface d'administration.
@@ -100,7 +115,7 @@ class Member(CoinLdapSyncMixin, AbstractUser):
     balance = models.DecimalField(max_digits=5, decimal_places=2, default=0,
                                   verbose_name='account balance')
 
-    objects = MemberManager()
+    objects = MemberManager.from_queryset(MemberQuerySet)()
 
     # Following fields are managed by the parent class AbstractUser :
     # username, first_name, last_name, email