From 3ff9fc183f8160ef7ef307a67c40e3b90e1e4f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20=20MENAGER?= <herve.menager@pasteur.fr> Date: Thu, 19 Jul 2018 15:42:08 +0200 Subject: [PATCH] allow sort on number of publications cf #67 Former-commit-id: 9947d6f75f74085131ce6952cf4f1e0a2510e41f --- ippisite/ippidb/views.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ippisite/ippidb/views.py b/ippisite/ippidb/views.py index fde5a67e..e98eb100 100644 --- a/ippisite/ippidb/views.py +++ b/ippisite/ippidb/views.py @@ -1,7 +1,7 @@ import json import math -from django.db.models import Max, Min +from django.db.models import Max, Min, Count from django.shortcuts import render from django.http import HttpResponseRedirect, Http404 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger @@ -153,7 +153,7 @@ def compound_list(request): Display the list of compounds """ context = {} - compounds = Compound.objects.order_by('id') + compounds = Compound.objects.all() if request.GET.get('family'): compounds = compounds.filter(compoundaction__ppi__family__id__in=request.GET.getlist('family')) # if filtering on "action on PPI" @@ -179,8 +179,10 @@ def compound_list(request): taxonomies = Taxonomy.objects.exclude(id__in=request.GET.getlist('taxonomy')) selected_boundcomplexes = ProteinDomainBoundComplex.objects.filter(id__in=request.GET.getlist('boundcomplex')) 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_option_ids = ['id', 'molecular_weight', 'a_log_p', 'nb_aromatic_sssr', 'nb_chiral_centers', 'pubs'] sort_by = request.GET.get('sort_by', 'id') + if sort_by in ['pubs', '-pubs']: + context['compounds'] = context['compounds'].annotate(pubs=Count('refcompoundbiblio')) context['compounds'] = context['compounds'].order_by(sort_by) context['compounds'] = context['compounds'].distinct() # handle pagination in compounds list @@ -201,10 +203,14 @@ def compound_list(request): compound_fields = {f.name: f.verbose_name for f in Compound._meta.get_fields() if not(f.is_relation)} sort_by_options = {} for sort_by_option_id in sort_by_option_ids: - 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} + if sort_by_option_id == 'pubs': + name = 'Number of publications' + else: + name = compound_fields.get(sort_by_option_id) + sort_by_options[sort_by_option_id] = {'name': name, 'order':'ascending', 'id': sort_by_option_id} + sort_by_options['-'+sort_by_option_id] = {'name': name, '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']) + fields = request.GET.getlist('fields',['id', 'common_name', 'molecular_weight', 'a_log_p', 'compound_action_pdb_ids', 'pubs']) if 'id' not in fields: fields.append('id') context['selected_ppis'] = selected_ppis -- GitLab