Commit | Line | Data |
---|---|---|
cbae0173 | 1 | # -*- encoding: utf-8 -*- |
55ef8558 | 2 | from chercheurs.models import Chercheur, Publication, Groupe, ChercheurGroupe |
cbae0173 AJ |
3 | from django.db import models |
4 | from django.contrib import admin | |
55ef8558 EMS |
5 | from django.core.urlresolvers import reverse as url |
6 | from django.forms.models import BaseInlineFormSet | |
7 | from django.http import HttpResponseRedirect | |
cbae0173 | 8 | |
55ef8558 | 9 | class ChercheurAdmin(admin.ModelAdmin): |
c0d2903e | 10 | list_filter = ('genre', 'statut', 'membre_reseau_institutionnel', 'membre_instance_auf', 'discipline', 'groupes') |
55ef8558 EMS |
11 | list_per_page = 25 |
12 | actions = ('remove_from_group',) | |
62354bdb | 13 | search_fields = ('nom', 'prenom') |
cbae0173 | 14 | |
c0d2903e EMS |
15 | def lookup_allowed(self, lookup): |
16 | return lookup in ['groupes__id__exact', 'discipline__id__exact'] or \ | |
17 | admin.ModelAdmin.lookup_allowed(self, lookup) | |
18 | ||
55ef8558 EMS |
19 | def remove_from_group(self, request, queryset): |
20 | groupe_id = request.GET.get('groupes__id__exact') | |
21 | chercheur_ids = queryset.values_list('id', flat=True) | |
22 | matches = ChercheurGroupe.objects.filter(groupe=groupe_id, chercheur__in=chercheur_ids) | |
23 | matches.delete() | |
24 | return HttpResponseRedirect(url('admin:chercheurs_chercheur_changelist') + '?groupes__id__exact=' + groupe_id) | |
25 | ||
26 | def get_actions(self, request): | |
27 | actions = super(ChercheurAdmin, self).get_actions(request) | |
28 | ||
29 | # Si on filtre par groupe de recherche, offrir d'en retirer les | |
30 | # chercheurs sélectionnés. | |
31 | groupe_id = request.GET.get('groupes__id__exact') | |
32 | if groupe_id: | |
33 | groupe = Groupe.objects.get(id=groupe_id) | |
34 | action_desc = actions['remove_from_group'] | |
e7b9234a | 35 | actions['remove_from_group'] = (action_desc[0], action_desc[1], u'Retirer du domaine de recherche « %s »' % groupe.nom) |
55ef8558 EMS |
36 | else: |
37 | del actions['remove_from_group'] | |
38 | return actions | |
39 | ||
40 | admin.site.register(Chercheur, ChercheurAdmin) | |
cbae0173 AJ |
41 | admin.site.register(Publication) |
42 | admin.site.register(Groupe) | |
43 |