Communauté -> communautés
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / templatetags / chercheurs_admin.py
CommitLineData
6f8d521e
EMS
1# coding: utf-8
2
3from django import template
4from django.utils.encoding import smart_unicode
5
22b31ca9 6from chercheurs.models import GroupeChercheur, DomaineRecherche, GENRE_CHOICES, STATUT_CHOICES
6f8d521e
EMS
7from datamaster_modeles.models import Region, Pays
8from savoirs.models import Discipline
9
10register = template.Library()
11
12OUI_NON_CHOICES = [(1, 'Oui'), (0, 'Non')]
13
14@register.inclusion_tag('admin/filter.html', takes_context=True)
15def 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)
20def 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)
25def 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)
30def 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)
35def 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)
40def filter_region(context):
41 return {'title': u"région",
15ceabe6
EMS
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)
45def filter_nord_sud(context):
46 return {'title': u'nord/sud',
47 'choices': prepare_choices([('Nord', 'Nord'), ('Sud', 'Sud')], 'nord_sud', context, remove=['pays', 'region'])}
6f8d521e
EMS
48
49@register.inclusion_tag('admin/filter.html', takes_context=True)
50def filter_pays(context):
51 request = context['request']
52 region = request.GET.get('region')
15ceabe6 53 nord_sud = request.GET.get('nord_sud')
6f8d521e
EMS
54 choices = Pays.objects
55 if region is not None:
56 choices = choices.filter(region=region)
15ceabe6
EMS
57 elif nord_sud is not None:
58 choices = choices.filter(nord_sud=nord_sud)
6f8d521e
EMS
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)
22b31ca9 63def filter_groupe_chercheurs(context):
d26ca87a 64 return {'title': u"communautés de chercheurs",
22b31ca9
PP
65 'choices': prepare_choices(GroupeChercheur.objects.values_list('id', 'nom'), 'groupes', context)}
66
67@register.inclusion_tag('admin/filter.html', takes_context=True)
68def filter_domaine_recherche(context):
6f8d521e 69 return {'title': u"domaine de recherche",
22b31ca9 70 'choices': prepare_choices(DomaineRecherche.objects.values_list('id', 'nom'), 'groupes', context)}
6f8d521e
EMS
71
72@register.inclusion_tag('admin/filter.html', takes_context=True)
73def filter_expert(context):
74 return {'title': u"expert",
75 'choices': prepare_choices(OUI_NON_CHOICES, 'expert', context)}
76
77def prepare_choices(choices, query_param, context, remove=[]):
78 request = context['request']
79 cl = context['cl']
80 query_val = request.GET.get(query_param)
81 result = [{'selected': query_val is None,
82 'query_string': cl.get_query_string({}, [query_param] + remove),
83 'display': 'Tout'}]
84 for k, v in choices:
85 result.append({'selected': smart_unicode(k) == query_val,
86 'query_string': cl.get_query_string({query_param: k}, remove),
87 'display': v})
88 return result