From 0137e3f6326a0bec9374bb7da0f791907382a0da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20=20MENAGER?= <herve.menager@pasteur.fr>
Date: Tue, 15 May 2018 21:50:49 +0200
Subject: [PATCH] refactor cutoff selection code for compounds list

Former-commit-id: 933a7ef7694e2e54e12312dcfdd01910f3f87b33
---
 ippisite/ippidb/templates/compound_list.html |  2 +-
 ippisite/ippidb/views.py                     | 69 ++++++++++----------
 2 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/ippisite/ippidb/templates/compound_list.html b/ippisite/ippidb/templates/compound_list.html
index 3612c3f9..6b2c285d 100644
--- a/ippisite/ippidb/templates/compound_list.html
+++ b/ippisite/ippidb/templates/compound_list.html
@@ -41,7 +41,7 @@
        {% include "slider_button.html" with label="Molecular weight" param_name="molecular_weight" %}
         </div>
         <div class="row">
-        <span class="col-md-6"><span>{{ count }} compounds</span>
+        <span class="col-md-6"><span>{{ compounds.paginator.count }} compounds</span>
             {% if selected_ppis or selected_diseases or selected_taxonomies or selected_boundcomplexes or molecular_weight != molecular_weight_max %}
                 <span>&nbsp;-&nbsp;filters:&nbsp;</span>
                 {% if selected_ppis %}
diff --git a/ippisite/ippidb/views.py b/ippisite/ippidb/views.py
index 4b6dd833..a2f77cb1 100644
--- a/ippisite/ippidb/views.py
+++ b/ippisite/ippidb/views.py
@@ -174,10 +174,22 @@ def biblio_card(request, source, id_source):
                                                 'compound_biblios': refs,
                                                 'tad_ppis': tad_ppis}) 
 
+def process_cutoff_value(name, context, request):
+    context[name+'_max'] = Compound.objects.all().aggregate(Max(name))[name + '__max'].to_eng_string()
+    context[name+'_min'] = Compound.objects.all().aggregate(Min(name))[name + '__min'].to_eng_string()
+    if request.GET.get(name):
+        context[name] = request.GET.get(name)
+        filter_dict = {name + '__lte': context[name]}
+        context['compounds'] = context['compounds'].filter(**filter_dict)
+    else:
+        context[name] = context[name+'_max']
+    return context
+
 def compound_list(request):
     """
     Display the list of compounds
     """
+    context = {}
     compounds = Compound.objects.order_by('id')
     if request.GET.get('family'):
         compounds = compounds.filter(compoundaction__ppi__family__id__in=request.GET.getlist('family'))
@@ -190,13 +202,8 @@ def compound_list(request):
         compounds = compounds.filter(compoundaction__ppi_id__ppicomplex__complex__protein__organism__id__in=request.GET.getlist('taxonomy'))
     if request.GET.get('boundcomplex'):
         compounds = compounds.filter(compoundaction__ppi_id__ppicomplex__complex__id__in=request.GET.getlist('boundcomplex'))
-    mw_max = Compound.objects.all().aggregate(Max('molecular_weight'))['molecular_weight__max'].to_eng_string()
-    mw_min = Compound.objects.all().aggregate(Min('molecular_weight'))['molecular_weight__min'].to_eng_string()
-    if request.GET.get('molecular_weight'):
-        molecular_weight = request.GET.get('molecular_weight')
-        compounds = compounds.filter(molecular_weight__lte=molecular_weight)
-    else:
-        molecular_weight = mw_max
+    context['compounds'] = compounds
+    context = process_cutoff_value('molecular_weight', context, request)
     selected_families = PpiFamily.objects.filter(id__in=request.GET.getlist('family'))
     families = PpiFamily.objects.exclude(id__in=request.GET.getlist('family'))
     selected_ppis = Ppi.objects.filter(id__in=request.GET.getlist('ppi'))
@@ -209,20 +216,19 @@ def compound_list(request):
     boundcomplexes = ProteinDomainBoundComplex.objects.exclude(id__in=request.GET.getlist('boundcomplex'))
     sort_by_option_ids = ['id', 'molecular_weight', 'a_log_p', 'nb_aromatic_sssr', 'nb_chiral_centers']
     sort_by = request.GET.get('sort_by', 'id')
-    compounds = compounds.order_by(sort_by)
-    compounds = compounds.distinct()
-    count = compounds.count()
+    context['compounds'] = context['compounds'].order_by(sort_by)
+    context['compounds'] = context['compounds'].distinct()
     # handle pagination in compounds list
-    paginator = Paginator(compounds, 12)
+    paginator = Paginator(context['compounds'], 12)
     page = request.GET.get('page')
     try:
-        compounds = paginator.page(page)
+        context['compounds'] = paginator.page(page)
     except PageNotAnInteger:
         # If page is not an integer, deliver first page.
-        compounds = paginator.page(1)
+        context['compounds'] = paginator.page(1)
     except EmptyPage:
         # If page is out of range (e.g. 9999), deliver last page of results.
-        compounds = paginator.page(paginator.num_pages)
+        context['compounds'] = paginator.page(paginator.num_pages)
     display = request.GET.get('display')
     if display not in ['l', 't']:
         display = 'v'
@@ -235,27 +241,20 @@ def compound_list(request):
     fields = request.GET.getlist('fields',['id', 'common_name', 'molecular_weight', 'a_log_p', 'compound_action_pdb_ids', 'biblio_refs'])    
     if 'id' not in fields:
         fields.append('id')
-    return render(request, 'compound_list.html', {'compounds': compounds,
-                                                  'count': count,
-                                                  'selected_ppis': selected_ppis,
-                                                  'ppis': ppis,
-                                                  'selected_families': selected_families,
-                                                  'families': families,
-                                                  'selected_diseases': selected_diseases,
-                                                  'diseases': diseases,
-                                                  'selected_taxonomies': selected_taxonomies,
-                                                  'taxonomies': taxonomies,
-                                                  'selected_boundcomplexes': selected_boundcomplexes,
-                                                  'boundcomplexes': boundcomplexes,
-                                                  'display': display,
-                                                  'compound_fields': compound_fields,
-                                                  'fields': fields,
-                                                  'sort_by': sort_by,
-                                                  'sort_by_options': sort_by_options,
-                                                  'molecular_weight_max': mw_max,
-                                                  'molecular_weight_min': mw_min,
-                                                  'molecular_weight': molecular_weight
-                                                })
+    context['selected_ppis'] = selected_ppis
+    context['ppis'] = ppis
+    context['selected_families'] = selected_families
+    context['families'] = families
+    context['selected_diseases'] = selected_diseases
+    context['diseases'] = diseases
+    context['selected_taxonomies'] = selected_taxonomies
+    context['taxonomies'] = taxonomies
+    context['selected_boundcomplexes'] = selected_boundcomplexes
+    context['boundcomplexes'] = boundcomplexes
+    context['fields'] = fields
+    context['sort_by'] = sort_by
+    context['sort_by_options'] = sort_by_options
+    return render(request, 'compound_list.html', context)
 
 def compound_card(request, compound_id):
     try:
-- 
GitLab