5125b7fd15c394f536f0a8e7db16fc1a8f814709
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / templatetags / chercheurs_admin.py
1 # coding: utf-8
2
3 from django import template
4 from django.utils.encoding import smart_unicode
5
6 from chercheurs.models import Chercheur, Groupe, GENRE_CHOICES, STATUT_CHOICES
7 from datamaster_modeles.models import Region, Pays
8 from savoirs.models import Discipline
9
10 register = template.Library()
11
12 OUI_NON_CHOICES = [(1, 'Oui'), (0, 'Non')]
13
14 @register.inclusion_tag('admin/filter.html', takes_context=True)
15 def filter_genre(context):
16 return {'title': 'genre',
17 'choices': prepare_choices(GENRE_CHOICES, 'genre', context)}
18
19 @register.inclusion_tag('admin/filter.html', takes_context=True)
20 def filter_statut(context):
21 return {'title': 'statut',
22 'choices': prepare_choices(STATUT_CHOICES, 'statut', context)}
23
24 @register.inclusion_tag('admin/filter.html', takes_context=True)
25 def filter_membre_reseau_institutionnel(context):
26 return {'title': u"appartenance aux instances d'un réseau institutionnel de l'AUF",
27 'choices': prepare_choices(OUI_NON_CHOICES, 'membre_reseau_institutionnel', context)}
28
29 @register.inclusion_tag('admin/filter.html', takes_context=True)
30 def filter_membre_instance_auf(context):
31 return {'title': u"appartenance à une instance de l'AUF",
32 'choices': prepare_choices(OUI_NON_CHOICES, 'membre_instance_auf', context)}
33
34 @register.inclusion_tag('admin/filter.html', takes_context=True)
35 def filter_discipline(context):
36 return {'title': u"discipline",
37 'choices': prepare_choices(Discipline.objects.values_list('id', 'nom'), 'discipline', context)}
38
39 @register.inclusion_tag('admin/filter.html', takes_context=True)
40 def filter_region(context):
41 return {'title': u"région",
42 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'region', context, remove=['pays', 'nord_sud'])}
43
44 @register.inclusion_tag('admin/filter.html', takes_context=True)
45 def filter_nord_sud(context):
46 return {'title': u'nord/sud',
47 'choices': prepare_choices([('Nord', 'Nord'), ('Sud', 'Sud')], 'nord_sud', context, remove=['pays', 'region'])}
48
49 @register.inclusion_tag('admin/filter.html', takes_context=True)
50 def filter_pays(context):
51 request = context['request']
52 region = request.GET.get('region')
53 nord_sud = request.GET.get('nord_sud')
54 choices = Pays.objects
55 if region is not None:
56 choices = choices.filter(region=region)
57 elif nord_sud is not None:
58 choices = choices.filter(nord_sud=nord_sud)
59 return {'title': u"pays",
60 'choices': prepare_choices(choices.values_list('code', 'nom'), 'pays', context)}
61
62 @register.inclusion_tag('admin/filter.html', takes_context=True)
63 def filter_groupe(context):
64 return {'title': u"domaine de recherche",
65 'choices': prepare_choices(Groupe.objects.values_list('id', 'nom'), 'groupes', context)}
66
67 @register.inclusion_tag('admin/filter.html', takes_context=True)
68 def filter_expert(context):
69 return {'title': u"expert",
70 'choices': prepare_choices(OUI_NON_CHOICES, 'expert', context)}
71
72 def prepare_choices(choices, query_param, context, remove=[]):
73 request = context['request']
74 cl = context['cl']
75 query_val = request.GET.get(query_param)
76 result = [{'selected': query_val is None,
77 'query_string': cl.get_query_string({}, [query_param] + remove),
78 'display': 'Tout'}]
79 for k, v in choices:
80 result.append({'selected': smart_unicode(k) == query_val,
81 'query_string': cl.get_query_string({query_param: k}, remove),
82 'display': v})
83 return result