Skip to content
Snippets Groups Projects
Commit 3ff9fc18 authored by Hervé  MENAGER's avatar Hervé MENAGER
Browse files

allow sort on number of publications

cf #67


Former-commit-id: 9947d6f75f74085131ce6952cf4f1e0a2510e41f
parent 4935bcb8
No related branches found
No related tags found
No related merge requests found
import json import json
import math import math
from django.db.models import Max, Min from django.db.models import Max, Min, Count
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponseRedirect, Http404 from django.http import HttpResponseRedirect, Http404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
...@@ -153,7 +153,7 @@ def compound_list(request): ...@@ -153,7 +153,7 @@ def compound_list(request):
Display the list of compounds Display the list of compounds
""" """
context = {} context = {}
compounds = Compound.objects.order_by('id') compounds = Compound.objects.all()
if request.GET.get('family'): if request.GET.get('family'):
compounds = compounds.filter(compoundaction__ppi__family__id__in=request.GET.getlist('family')) compounds = compounds.filter(compoundaction__ppi__family__id__in=request.GET.getlist('family'))
# if filtering on "action on PPI" # if filtering on "action on PPI"
...@@ -179,8 +179,10 @@ def compound_list(request): ...@@ -179,8 +179,10 @@ def compound_list(request):
taxonomies = Taxonomy.objects.exclude(id__in=request.GET.getlist('taxonomy')) taxonomies = Taxonomy.objects.exclude(id__in=request.GET.getlist('taxonomy'))
selected_boundcomplexes = ProteinDomainBoundComplex.objects.filter(id__in=request.GET.getlist('boundcomplex')) selected_boundcomplexes = ProteinDomainBoundComplex.objects.filter(id__in=request.GET.getlist('boundcomplex'))
boundcomplexes = ProteinDomainBoundComplex.objects.exclude(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') 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'].order_by(sort_by)
context['compounds'] = context['compounds'].distinct() context['compounds'] = context['compounds'].distinct()
# handle pagination in compounds list # handle pagination in compounds list
...@@ -201,10 +203,14 @@ def compound_list(request): ...@@ -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)} compound_fields = {f.name: f.verbose_name for f in Compound._meta.get_fields() if not(f.is_relation)}
sort_by_options = {} sort_by_options = {}
for sort_by_option_id in sort_by_option_ids: 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} if sort_by_option_id == 'pubs':
sort_by_options['-'+sort_by_option_id] = {'name': compound_fields[sort_by_option_id], 'order':'descending', 'id': sort_by_option_id} 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: if 'id' not in fields:
fields.append('id') fields.append('id')
context['selected_ppis'] = selected_ppis context['selected_ppis'] = selected_ppis
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment