]> git.parisson.com Git - teleforma.git/commitdiff
Add new filter to filter student based on multiple trainings
authortest test <yoanl@pilotsystems.net>
Thu, 1 Jul 2021 15:43:45 +0000 (17:43 +0200)
committerYoan Le Clanche <yoanl@pilotsystems.net>
Thu, 1 Jul 2021 15:43:45 +0000 (17:43 +0200)
teleforma/admin.py
teleforma/admin_filter.py [new file with mode: 0644]
teleforma/templates/admin/multiselect.html [new file with mode: 0644]

index 8d01b57b5fcb1db9e1b5bf9673f87e883a486470..e3dbbc3cf768e0342b4b9d2589868dbd89c108d1 100644 (file)
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 import csv
 import datetime
+from teleforma.admin_filter import MultipleChoiceListFilter
 from teleforma.models.chat import ChatMessage
 
 from django.contrib import admin
@@ -121,7 +122,12 @@ class BalanceFilter(admin.SimpleListFilter):
             return queryset.filter(balance__gt=0)
         else:
             return queryset
+class TrainingsFilter(MultipleChoiceListFilter):
+    title = 'Formations'
+    parameter_name = 'trainings__in'
 
+    def lookups(self, request, model_admin):
+        return [(training.id, str(training)) for training in Training.objects.all()]
 
 class StudentAdmin(admin.ModelAdmin):
     model = Student
@@ -131,7 +137,7 @@ class StudentAdmin(admin.ModelAdmin):
     inlines = [PaymentInline, OptionalFeeInline, DiscountInline, PaybackInline]
     search_fields = ['user__first_name', 'user__last_name', 'user__username']
     list_filter = ['user__is_active', 'restricted', 'is_subscribed', 'platform_only', PeriodListFilter,
-                   'trainings', 'iej', 'procedure', 'written_speciality', 'oral_speciality',
+                   TrainingsFilter, 'iej', 'procedure', 'written_speciality', 'oral_speciality',
                    'oral_1', 'oral_2', 'fascicule', BalanceFilter]
     list_display = ['student_name', 'restricted', 'get_trainings', 'platform_only',
                     'total_payments', 'total_fees', 'balance', 'balance_intermediary']
diff --git a/teleforma/admin_filter.py b/teleforma/admin_filter.py
new file mode 100644 (file)
index 0000000..4191884
--- /dev/null
@@ -0,0 +1,41 @@
+  
+from django.contrib import admin
+
+
+class MultipleChoiceListFilter(admin.SimpleListFilter):
+    template = 'admin/multiselect.html'
+
+
+    def __init__(self, request, params, model, model_admin):
+        super().__init__(request, params, model, model_admin)
+        self.used_parameters[self.parameter_name] = request.GET.getlist(self.parameter_name) or []
+        lookup_choices = self.lookups(request, model_admin)
+        if lookup_choices is None:
+            lookup_choices = ()
+        self.lookup_choices = list(lookup_choices)
+
+
+    def lookups(self, request, model_admin):
+        """
+        Must be overridden to return a list of tuples (value, verbose value)
+        """
+        raise NotImplementedError(
+            'The MultipleChoiceListFilter.lookups() method must be overridden to '
+            'return a list of tuples (value, verbose value).'
+        )
+
+    def queryset(self, request, queryset):
+        if request.GET.get(self.parameter_name):
+            kwargs = {self.parameter_name: request.GET.getlist(self.parameter_name)}
+            queryset = queryset.filter(**kwargs)
+        return queryset
+
+
+    def choices(self, changelist):
+
+        for lookup, title in self.lookup_choices:
+            yield {
+                'selected': self.value() and str(lookup) in self.value(),
+                'query_string': changelist.get_query_string({self.parameter_name: lookup}),
+                'display': title,
+            }
\ No newline at end of file
diff --git a/teleforma/templates/admin/multiselect.html b/teleforma/templates/admin/multiselect.html
new file mode 100644 (file)
index 0000000..cd4a679
--- /dev/null
@@ -0,0 +1,23 @@
+
+{% comment %} {{ choices }} {% endcomment %}
+
+<div class="form-group" style="max-width: 100%;">
+    <select class="form-control {{ field_name }}_select2" name="{{ field_name }}" data-name="{{ field_name }}" style="width: 100%;max-width: 80%;" tabindex="-1" aria-hidden="true" multiple>
+
+    {% for choice in choices %}
+        <option value="{{ choice.value }}" {% if choice.selected %}selected{% endif %}>{{ choice.display }}</option>
+    {% endfor %}
+</select>
+
+<script>
+    document.addEventListener("DOMContentLoaded", function(event) { 
+        jQuery(".{{ field_name }}_select2").select2({
+            placeholder: "{{ title }}",
+            width: 'resolve',
+            closeOnSelect: false,
+            dropdownAutoWidth: true,
+        });
+    });
+</script>
+
+</div>
\ No newline at end of file