From d3a444a1d78427ba4e7dc8c350f7a43c8e9776a5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20=20MENAGER?= <herve.menager@pasteur.fr>
Date: Wed, 5 Dec 2018 15:40:54 +0100
Subject: [PATCH] add pubchem, chembl and chemspider ID existence filters

Former-commit-id: 0cd068ad893696c595d2f1583ed8bc747a2b5b73
---
 ippisite/ippidb/templates/base.html          |  8 ++++++
 ippisite/ippidb/templates/compound_list.html | 24 ++++++++++++------
 ippisite/ippidb/views.py                     | 26 +++++++++++++++++++-
 3 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/ippisite/ippidb/templates/base.html b/ippisite/ippidb/templates/base.html
index 95fdda98..ee778149 100644
--- a/ippisite/ippidb/templates/base.html
+++ b/ippisite/ippidb/templates/base.html
@@ -235,6 +235,14 @@
                     $('#'+paramName+'_textvalue_max').text(slideEvt.value[1]);
                 });
             }
+            var toggleCheckBox = function(id){
+                var cb = $('#'+id);
+                if(cb.prop('checked')==true){
+                    modifyUrl(id,cb.prop('checked'));
+                }else{
+                    modifyUrl(id,null);
+                }
+            }
         </script>
     </head>
 
diff --git a/ippisite/ippidb/templates/compound_list.html b/ippisite/ippidb/templates/compound_list.html
index c5f42623..5db30ab9 100644
--- a/ippisite/ippidb/templates/compound_list.html
+++ b/ippisite/ippidb/templates/compound_list.html
@@ -76,14 +76,22 @@
                     </div>
                 </div>
                 <div class="dropdown btn-group" style="display: inline-flex;">
-                        <button class="btn btn-primary dropdown-toggle" type="button" id="elMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
-                            External Links
-                        </button>
-                        <div class="dropdown-menu" aria-labelledby="elMenuButton">
-                        </div>
-                    </div>            
-                </span>
-            <span class="border border-primary rounded p-1 m-1">
+                    <button class="btn btn-primary dropdown-toggle" type="button" id="elMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
+                        External Links
+                    </button>
+                    <div class="dropdown-menu" aria-labelledby="elMenuButton">
+                        <label class="dropdown-item" onclick="toggleCheckBox('pubchem_id')">
+                            <input type="checkbox" id="pubchem_id" {% if pubchem_id %}checked{% endif %}/> in PubChem
+                        </label>
+                        <label class="dropdown-item" onclick="toggleCheckBox('chembl_id')">
+                            <input type="checkbox" id="chembl_id"  {% if chembl_id %}checked{% endif %}/> in ChEMBL
+                        </label>
+                        <label class="dropdown-item" onclick="toggleCheckBox('chemspider_id')">
+                            <input type="checkbox" id="chemspider_id" {% if chemspider_id %}checked{% endif %}/> in ChemSpider
+                        </label>                                            
+                    </div>
+                </div>            
+        <span class="border border-primary rounded p-1 m-1">
                 <i class="icon icon-conceptual icon-structures-3d"  style="font-size: 1.75rem; vertical-align: middle" title="PPI target filters"></i>
                 <div class="dropdown btn-group" style="display: inline-flex;">
                         <button class="btn btn-primary dropdown-toggle" type="button" id="ptfMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
diff --git a/ippisite/ippidb/views.py b/ippisite/ippidb/views.py
index bc16bc98..3b0cc3ce 100644
--- a/ippisite/ippidb/views.py
+++ b/ippisite/ippidb/views.py
@@ -301,7 +301,28 @@ class CompoundRangeFilterHandler(object):
         if not self.value:
             self.filter_context[self.parameter_name+'_max'] = str(int(math.ceil(float(queryset.aggregate(Max(self.parameter_name))[self.parameter_name + '__max'] or 0))))
             self.filter_context[self.parameter_name+'_min'] = str(int(math.floor(float(queryset.aggregate(Min(self.parameter_name))[self.parameter_name + '__min'] or 0))))
-        print(self.parameter_name, self.filter_context[self.parameter_name+'_min'], self.filter_context[self.parameter_name+'_max'])
+
+class ExistsFilterHandler(object):
+
+    def __init__(self, parameter_name, filter_context, request_get):
+        self.parameter_name = parameter_name
+        self.filter_context = filter_context
+        self.value = None
+        if request_get.get(parameter_name):
+            self.value = request_get.get(parameter_name)
+            self.filter_context[self.parameter_name] = self.value
+
+    def process(self, queryset):
+        """ to be called during queryset filtering """
+        # if a value has been set for this filter
+        if self.value:
+            # filter queryset on the ID values specified
+            queryset = queryset.exclude(**{self.parameter_name+'__isnull':not(self.value)})
+        return queryset
+
+    def post_process(self, compound_ids, queryset):
+        """ to be called after queryset filtering """
+        pass
 
 class CompoundListView(ListView):
 
@@ -393,6 +414,9 @@ class CompoundListView(ListView):
             CompoundRangeFilterHandler('qs_ba', self.filter_context, self.request.GET),
             CompoundRangeFilterHandler('qs_le', self.filter_context, self.request.GET),
             CompoundRangeFilterHandler('qs_lle', self.filter_context, self.request.GET),
+            ExistsFilterHandler('pubchem_id', self.filter_context, self.request.GET),
+            ExistsFilterHandler('chemspider_id', self.filter_context, self.request.GET),
+            ExistsFilterHandler('chembl_id', self.filter_context, self.request.GET),
             CompoundSimilarityFilterHandler('similar_to', self.filter_context, self.request.GET),
             TextSearchFilterHandler('q', self.filter_context, self.request.GET),
         ]
-- 
GitLab