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