Browse Source

Also display member warnings in detail view

Ref #137
Jocelyn Delalande 6 years ago
parent
commit
dc432aa15d
1 changed files with 25 additions and 8 deletions
  1. 25 8
      coin/members/admin.py

+ 25 - 8
coin/members/admin.py

@@ -123,6 +123,15 @@ class MembershipFeeFilter(SimpleListFilter):
 
 
 class MemberAdmin(UserAdmin):
+    SERVICE_NO_FEE_MSG = (
+        "Cet·te adhérent·e bénéficie d'un service"
+        " mais n'est pas à jour de cotisation.")
+
+    RECENT_SERVICE_MSG = (
+        "Cet·te ancien·ne adhérent·e a un service résilié depuis moins d'un an"
+        ". Légalement, il faut conserver sa fiche un an après la résiliation "
+        "du service.")
+
     list_display = ('id', 'status', 'username', 'first_name',
                     'name_or_organization_name',
                     'nickname', 'email',
@@ -153,18 +162,12 @@ class MemberAdmin(UserAdmin):
         if (membership_end is not None and membership_end < date.today()
             and recent_inactive_subscriptions.exists() and not active_subscriptions.exists()):
                 note = ' <i class="fa fa-question-circle"  style="color: #cccc40"></i>'
-                tooltip = (
-                    "Cet·te ancien·ne adhérent·e a un service résilié depuis moins d'un an. "
-                    "Légalement, il faut conserver sa fiche un an après la résiliation du service."
-                )
+                tooltip = self.RECENT_SERVICE_MSG
 
         if (active_subscriptions.exists() and (
                 membership_end is None or membership_end <= date.today())):
             note = ' <i class="fa fa-question-circle"  style="color: red"></i>'
-            tooltip = (
-                "Cet·te adhérent·e bénéficie d'un service mais n'est pas à jour de cotisation."
-                )
-
+            tooltip = self.SERVICE_NO_FEE_MSG
 
         out = '{}{}'.format(
             obj.end_date_of_membership() or "pas de cotisation", note)
@@ -224,6 +227,20 @@ class MemberAdmin(UserAdmin):
 
     inlines = [CryptoKeyInline, MembershipFeeInline, OfferSubscriptionInline]
 
+    def add_member_warnings(self, request, member):
+        has_active_subscriptions = member.get_active_subscriptions().exists()
+        has_recent_resigned_subscriptions = member.get_recent_inactive_subscriptions()
+
+        if not member.is_paid_up() and has_active_subscriptions:
+            messages.error(request, self.SERVICE_NO_FEE_MSG)
+        elif not has_active_subscriptions and has_recent_resigned_subscriptions:
+            messages.warning(request, self.RECENT_SERVICE_MSG)
+
+    def get_form(self, request, obj=None, *args, **kwargs):
+        if obj:
+            self.add_member_warnings(request, obj)
+        return super(MemberAdmin, self).get_form(request, obj, *args, **kwargs)
+
     def get_queryset(self, request):
         qs = super(MemberAdmin, self).get_queryset(request)
         if request.user.is_superuser: