From 9179cf7612cec9f52842e1efd163c42bada6ac57 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Herv=C3=A9=20=20MENAGER?= <herve.menager@pasteur.fr>
Date: Sun, 13 May 2018 20:25:41 +0200
Subject: [PATCH] add sorting options to compounds list table headers

as requested in #48


Former-commit-id: 2002ce71cfd5a26f73bd99286b58362f025a01c8
---
 .../ippidb/templates/compound_t_colhead.html  | 17 ++++++
 .../ippidb/templates/compound_t_list.html     | 56 +++++--------------
 ippisite/ippidb/views.py                      |  2 +
 3 files changed, 32 insertions(+), 43 deletions(-)
 create mode 100644 ippisite/ippidb/templates/compound_t_colhead.html

diff --git a/ippisite/ippidb/templates/compound_t_colhead.html b/ippisite/ippidb/templates/compound_t_colhead.html
new file mode 100644
index 00000000..386cee7b
--- /dev/null
+++ b/ippisite/ippidb/templates/compound_t_colhead.html
@@ -0,0 +1,17 @@
+{% if col_id in fields %}
+{% with '-'|add:col_id as col_id_desc %}
+  <th scope="col">
+    {% if col_id == sort_by %}
+      <i class="fa fa-arrow-up" title="sorted by ascending {{ col_name }}"></i>
+      <a onclick="modifyUrl('sort_by','{{ col_id_desc }}')" title="click to sort descending on {{ col_name }}" href="#">{{ col_name | capfirst }}</a>
+    {% elif col_id_desc == sort_by %}
+      <i class="fa fa-arrow-down" title="sorted by descending {{ col_name }}"></i>
+      <a onclick="modifyUrl('sort_by','{{ col_id }}')" title="click to sort ascending on {{ col_name }}" href="#">{{ col_name | capfirst }}</a>
+    {% elif col_id in sort_by_options %}
+      <a onclick="modifyUrl('sort_by','{{ col_id }}')" title="click to sort ascending on {{ col_name }}" href="#">{{ col_name | capfirst }}</a>
+    {% else %}
+      {{ col_name | capfirst }}
+    {% endif %}
+  </th>
+{% endwith %}
+{% endif %}
diff --git a/ippisite/ippidb/templates/compound_t_list.html b/ippisite/ippidb/templates/compound_t_list.html
index 5b91101e..14953516 100644
--- a/ippisite/ippidb/templates/compound_t_list.html
+++ b/ippisite/ippidb/templates/compound_t_list.html
@@ -1,49 +1,19 @@
 <table class="table">
   <thead>
     <tr>
-      <th scope="col">#</th>
-      {% if "common_name" in fields %}
-      <th scope="col">Common name</th>
-      {% endif %}
-      {% if "molecular_weight" in fields %}
-      <th scope="col">Molecular weight</th>
-      {% endif %}
-      {% if "a_log_p" in fields %}
-      <th scope="col">AlogP</th>
-      {% endif %}
-      {% if "compound_action_pdb_ids" in fields %}
-      <th scope="col">PDB ligand</th>
-      {% endif %}
-      {% if "mddr_phase" in fields %}
-      <th scope="col">MDDR phase</th>
-      {% endif %}
-      {% if "available_tests" in fields %}
-      <th scope="col">Available tests</th>
-      {% endif %}
-      {% if "biblio_refs" in fields %}
-      <th scope="col">Available publications
-      </th>
-      {% endif %}
-      {% if "nb_acceptor_h" in fields %}
-      <th scope="col">Number of hydrogen bond acceptors
-      </th>
-      {% endif %}
-      {% if "nb_donor_h" in fields %}
-      <th scope="col">Number of hydrogen bond donors
-      </th>
-      {% endif %}
-      {% if "tpsa" in fields %}
-      <th scope="col">Topological Polar Surface Area (TPSA)
-      </th>
-      {% endif %}
-      {% if "nb_rotatable_bonds" in fields %}
-      <th scope="col">Number of rotatable bonds
-      </th>
-      {% endif %}
-      {% if "nb_aromatic_sssr" in fields %}
-      <th scope="col">Number of aromatic Smallest Set of System Rings (SSSR)
-      </th>
-      {% endif %}
+      {% include "compound_t_colhead.html" with col_id="id" col_name="#" %}
+      {% include "compound_t_colhead.html" with col_id="common_name" col_name="common name" %}
+      {% include "compound_t_colhead.html" with col_id="molecular_weight" col_name="molecular weight" %}
+      {% include "compound_t_colhead.html" with col_id="a_log_p" col_name="AlogP" %}
+      {% include "compound_t_colhead.html" with col_id="compound_action_pdb_ids" col_name="PDB ligand(s)" %}
+      {% include "compound_t_colhead.html" with col_id="mddr_phase" col_name="MDDR phase" %}
+      {% include "compound_t_colhead.html" with col_id="available_tests" col_name="available tests" %}
+      {% include "compound_t_colhead.html" with col_id="biblio_refs" col_name="available publications" %}
+      {% include "compound_t_colhead.html" with col_id="nb_acceptor_h" col_name="number of hydrogen bond acceptors" %}
+      {% include "compound_t_colhead.html" with col_id="nb_donor_h" col_name="number of hydrogen bond donors" %}
+      {% include "compound_t_colhead.html" with col_id="tpsa" col_name="TPSA" %}
+      {% include "compound_t_colhead.html" with col_id="nb_rotatable_bonds" col_name="number of rotatable bonds" %}
+      {% include "compound_t_colhead.html" with col_id="nb_aromatic_sssr" col_name="number of aromatic SSSR" %}
       <button type="button" class="btn btn-primary float-right" style="width: inherit!important" data-toggle="modal" data-target="#selectColumns">
         <i class="fa fa-columns" title="customize columns"></i>
       </button>
diff --git a/ippisite/ippidb/views.py b/ippisite/ippidb/views.py
index cad1fc9e..5d5a6d49 100644
--- a/ippisite/ippidb/views.py
+++ b/ippisite/ippidb/views.py
@@ -224,6 +224,8 @@ def compound_list(request):
         sort_by_options[sort_by_option_id] = {'name': compound_fields[sort_by_option_id], 'order':'ascending', 'id': sort_by_option_id}
         sort_by_options['-'+sort_by_option_id] = {'name': compound_fields[sort_by_option_id], 'order':'descending', 'id': sort_by_option_id}
     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,
-- 
GitLab