recherche nom prenom
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / views.py
1 # -*- encoding: utf-8 -*-
2 import hashlib
3 from django.shortcuts import render_to_response
4 from django.http import HttpResponseRedirect
5 from django.template import Context, RequestContext
6 from django.core.urlresolvers import reverse
7 from forms import *
8
9 from auf_references_client.models import Discipline, TypeImplantation
10 from models import Personne, Utilisateur
11
12 from django.contrib.auth.decorators import login_required
13
14 from django.db.models import Q
15
16 def chercheur_queryset (request):
17 list = Chercheur.objects.order_by("id")
18 pays = ""
19
20 simpleForm = RepertoireSearchForm (request.GET)
21 if simpleForm.is_valid ():
22 pays = simpleForm.cleaned_data["pays"]
23 if pays:
24 list = list.filter (nationalite = pays.pk)
25 fonction = simpleForm.cleaned_data["fonction"]
26 if fonction:
27 list = list.filter (fonction = fonction)
28 genre = simpleForm.cleaned_data["genre"]
29 if genre:
30 list = list.filter (personne__genre=genre)
31 discipline = simpleForm.cleaned_data["discipline"]
32 if discipline:
33 list = list.filter (discipline=discipline)
34 mots_cles = simpleForm.cleaned_data["mots_cles"]
35 if mots_cles:
36 list = list.filter (Q(personne__nom__icontains=mots_cles) | Q(personne__prenom__icontains=mots_cles))
37 return list
38
39 def index(request):
40 """Répertoire des chercheurs"""
41
42 chercheurs = chercheur_queryset (request)
43 repertoire_form = RepertoireSearchForm (request.GET)
44
45 nb_chercheurs = chercheurs.count()
46 variables = { 'chercheurs': chercheurs,
47 'nb_chercheurs': nb_chercheurs,
48 'repertoire_form': repertoire_form,
49 }
50 return render_to_response ("chercheurs/index.html", \
51 Context (variables),
52 context_instance = RequestContext(request))
53
54 def inscription(request):
55 if request.method == 'POST':
56 personne_form = PersonneForm (request.POST, prefix="personne")
57 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
58 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
59 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
60 discipline_form = DisciplineForm (request.POST, prefix="discipline")
61 publication1_form = PublicationForm (request.POST, prefix="publication1")
62 publication2_form = PublicationForm (request.POST, prefix="publication2")
63 publication3_form = PublicationForm (request.POST, prefix="publication3")
64 publication4_form = PublicationForm (request.POST, prefix="publication4")
65 these_form = TheseForm(request.POST, prefix="these")
66
67 if personne_form.is_valid():
68 if chercheur_form.is_valid():
69 c = chercheur_form.save(commit=False)
70
71 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
72 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
73
74 if etablissement_form.is_valid() and discipline_form.is_valid() and these_form.is_valid():
75 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
76 pub = publication1_form.save()
77 c.publication1 = pub
78 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
79 pub = publication2_form.save()
80 c.publication2 = pub
81 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
82 pub = publication3_form.save()
83 c.publication3 = pub
84 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
85 pub = publication4_form.save()
86 c.publication4 = pub
87 these = these_form.save()
88 c.these = these
89 etablissement_form.save(commit=False)
90 etablissement_autre_form.save(commit=False)
91 discipline_form.save(commit=False)
92 #encodage du mot de passe de l'utilisateur (refactorer car c'est pas clean
93 #et c'est pas la bonne place pour faire ca - AJ
94 personne_form.cleaned_data['password'] = hashlib.md5(personne_form.cleaned_data['password']).hexdigest()
95 p = personne_form.save()
96 c.personne = p
97 c.save()
98 return HttpResponseRedirect(reverse('chercheurs.views.retrieve', args=(c.id,)))
99 else:
100 personne_form = PersonneForm(prefix="personne")
101 chercheur_form = ChercheurForm(prefix="chercheur")
102 etablissement_form = EtablissementForm(prefix="etablissement")
103 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
104 discipline_form = DisciplineForm(prefix="discipline")
105 publication1_form = PublicationForm(prefix="publication1")
106 publication2_form = PublicationForm(prefix="publication2")
107 publication3_form = PublicationForm(prefix="publication3")
108 publication4_form = PublicationForm(prefix="publication4")
109 these_form = TheseForm(prefix="these")
110
111 variables = { 'personne_form': personne_form,
112 'chercheur_form': chercheur_form,
113 'etablissement_form': etablissement_form,
114 'discipline_form': discipline_form,
115 'etablissement_autre_form': etablissement_autre_form,
116 'publication1_form': publication1_form,
117 'publication2_form': publication2_form,
118 'publication3_form': publication3_form,
119 'publication4_form': publication4_form,
120 'these_form': these_form,
121 }
122
123 return render_to_response ("chercheurs/inscription.html", \
124 Context (variables),
125 context_instance = RequestContext(request))
126
127
128 def edit(request):
129 """Edition d'un chercheur"""
130 context_instance = RequestContext(request)
131 chercheur = context_instance['user_chercheur']
132 if request.method == 'POST':
133 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
134 #chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
135 etablissement_form = EtablissementForm(request.POST, prefix="etablissement", instance=chercheur)
136 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre", instance=chercheur)
137 discipline_form = DisciplineForm(request.POST, prefix="discipline", instance=chercheur)
138 publication1_form = PublicationForm(request.POST, prefix="publication1", instance=chercheur.publication1)
139 publication2_form = PublicationForm(request.POST, prefix="publication2", instance=chercheur.publication2)
140 publication3_form = PublicationForm(request.POST, prefix="publication3", instance=chercheur.publication3)
141 publication4_form = PublicationForm(request.POST, prefix="publication4", instance=chercheur.publication4)
142 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
143
144
145 if( personne_form.is_valid() and discipline_form.is_valid() and publication1_form.is_valid() and publication2_form.is_valid() and publication3_form.is_valid() and publication4_form.is_valid() and these_form.is_valid() ):
146 personne_form.save()
147 #chercheur_form.save()
148 discipline_form.save()
149 publication1_form.save()
150 publication2_form.save()
151 publication3_form.save()
152 publication4_form.save()
153 these_form.save()
154 else:
155 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
156 #chercheur_form = ChercheurForm (prefix="chercheur", instance=chercheur)
157 etablissement_form = EtablissementForm(prefix="etablissement", instance=chercheur)
158 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre", instance=chercheur)
159 discipline_form = DisciplineForm(prefix="discipline", instance=chercheur)
160 publication1_form = PublicationForm(prefix="publication1", instance=chercheur.publication1)
161 publication2_form = PublicationForm(prefix="publication2", instance=chercheur.publication2)
162 publication3_form = PublicationForm(prefix="publication3", instance=chercheur.publication3)
163 publication4_form = PublicationForm(prefix="publication4", instance=chercheur.publication4)
164 these_form = TheseForm(prefix="these", instance=chercheur.these)
165
166 variables = { 'chercheur': chercheur,
167 'personne_form':personne_form,
168 #'chercheur_form': chercheur_form,
169 'etablissement_form': etablissement_form,
170 'discipline_form': discipline_form,
171 'etablissement_autre_form': etablissement_autre_form,
172 'publication1_form': publication1_form,
173 'publication2_form': publication2_form,
174 'publication3_form': publication3_form,
175 'publication4_form': publication4_form,
176 'these_form': these_form,
177 }
178 return render_to_response ("chercheurs/edit.html", \
179 Context (variables),
180 context_instance = RequestContext(request))
181
182
183
184 def perso(request):
185 """Espace chercheur (espace personnel du chercheur)"""
186 context_instance = RequestContext(request)
187 chercheur = context_instance['user_chercheur']
188 if not chercheur:
189 return HttpResponseRedirect(reverse('django.contrib.auth.views.login'))
190 variables = { 'chercheur': chercheur,
191 }
192 return render_to_response ("chercheurs/perso.html", \
193 Context (variables),
194 context_instance = RequestContext(request))
195
196 def retrieve(request, id):
197 """Fiche du chercheur"""
198 chercheur = Chercheur.objects.get(id=id)
199 variables = { 'chercheur': chercheur,
200 }
201 return render_to_response ("chercheurs/retrieve.html", \
202 Context (variables),
203 context_instance = RequestContext(request))