recherche avec statut
[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, HttpResponse
5 from django.template import Context, RequestContext
6 from django.template.loader import get_template
7 from django.core.urlresolvers import reverse
8 from django.core.mail import send_mail
9 from django.conf import settings
10
11 from forms import *
12 from django.forms.models import inlineformset_factory
13
14 from auf_references_client.models import Discipline, TypeImplantation
15 from models import Personne, Utilisateur, Groupe, ChercheurGroupe
16
17 from django.contrib.auth.decorators import login_required
18
19 from django.db.models import Q
20 from django.shortcuts import get_object_or_404
21
22 from django.utils.translation import ugettext_lazy as _
23 from django.contrib.auth.forms import AuthenticationForm as OriginalAuthenticationForm
24
25 #TODO: Migrer tout ce qui a rapport aux users dans une nouvelle app
26
27 class AuthenticationForm(OriginalAuthenticationForm):
28 username = forms.CharField(label=_("Username"), max_length=255)
29
30 def send_password(request):
31 if request.method == "POST":
32 form = SendPasswordForm(data=request.POST)
33 if form.is_valid():
34 u = Utilisateur.objects.get(courriel=form.cleaned_data['email'])
35 code = hashlib.md5(u.courriel+u.password).hexdigest()
36 code = code[0:6]
37 link = "%saccounts/new_password/%s/%s/" % (settings.SITE_ROOT_URL, u.courriel, code)
38
39 variables = { 'user': u,
40 'link': link,
41 'SITE_ROOT_URL': settings.SITE_ROOT_URL,
42 'CONTACT_EMAIL': settings.CONTACT_EMAIL,
43 }
44 t = get_template('accounts/email_password.html')
45 content = t.render(Context(variables))
46
47 send_mail('Savoirs en partage: changement de mot de passe',
48 content, settings.CONTACT_EMAIL,
49 [u.courriel], fail_silently=False)
50 else:
51 form = SendPasswordForm()
52
53 variables = { 'form': form,
54 }
55 return render_to_response ("accounts/send_password.html", \
56 Context (variables),
57 context_instance = RequestContext(request))
58
59 def new_password(request, email, code):
60 u = Utilisateur.objects.get(courriel=email)
61 original_code = hashlib.md5(u.courriel+u.password).hexdigest()
62 original_code = original_code[0:6]
63 message=""
64 if(code == original_code):
65 if request.method == "POST":
66 form = NewPasswordForm(data=request.POST)
67 if form.is_valid():
68 new_password = form.cleaned_data['password']
69 u.password = hashlib.md5(new_password).hexdigest()
70 u.save()
71 message = "Votre mot de passe a été modifié."
72 else:
73 form = NewPasswordForm()
74 else:
75 return HttpResponseRedirect('/')
76 variables = { 'form': form,
77 'message': message,
78 }
79 return render_to_response ("accounts/new_password.html", \
80 Context (variables),
81 context_instance = RequestContext(request))
82
83 @login_required()
84 def change_password(request):
85 context_instance = RequestContext(request)
86 u = context_instance['user_sep']
87 message = ""
88 if request.method == "POST":
89 form = NewPasswordForm(data=request.POST)
90 if form.is_valid():
91 new_password = form.cleaned_data['password']
92 u.password = hashlib.md5(new_password).hexdigest()
93 u.save()
94 message = "Votre mot de passe a été modifié."
95 else:
96 form = NewPasswordForm()
97 variables = { 'form': form,
98 'message': message,
99 }
100 return render_to_response ("accounts/new_password.html", \
101 Context (variables),
102 context_instance = RequestContext(request))
103
104 def chercheur_login(request, template_name='registration/login.html', redirect_field_name='next'):
105 "Displays the login form and handles the login action."
106 redirect_to = request.REQUEST.get(redirect_field_name, '')
107 if request.method == "POST":
108 form = AuthenticationForm(data=request.POST)
109 if form.is_valid():
110 # Light security check -- make sure redirect_to isn't garbage.
111 if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
112 redirect_to = settings.LOGIN_REDIRECT_URL
113 from django.contrib.auth import login
114 login(request, form.get_user())
115 if request.session.test_cookie_worked():
116 request.session.delete_test_cookie()
117 return HttpResponseRedirect(redirect_to)
118 else:
119 form = AuthenticationForm(request)
120 request.session.set_test_cookie()
121 return render_to_response(template_name, {
122 'form': form,
123 redirect_field_name: redirect_to,
124 }, context_instance=RequestContext(request))
125
126
127 def chercheur_queryset (request):
128 list = Chercheur.objects.order_by("id")
129 pays = ""
130
131 simpleForm = RepertoireSearchForm (request.GET)
132 if simpleForm.is_valid ():
133 pays = simpleForm.cleaned_data["pays"]
134 if pays:
135 list = list.filter(Q(etablissement__pays = pays.pk) | Q(etablissement_autre_pays = pays.pk))
136 discipline = simpleForm.cleaned_data["discipline"]
137 if discipline:
138 list = list.filter(discipline=discipline)
139 domaine = simpleForm.cleaned_data["domaine"]
140 if domaine:
141 list = list.filter(groupes=domaine)
142 mots_cles = simpleForm.cleaned_data["mots_cles"]
143 if mots_cles:
144 list = list.search(mots_cles)
145 statut = simpleForm.cleaned_data["statut"]
146 if statut:
147 if statut == "expert":
148 list = list.exclude(expertise=None)
149 else:
150 list = list.filter(statut=statut)
151 return list
152
153 def index(request):
154 """Répertoire des chercheurs"""
155
156 chercheurs = chercheur_queryset (request)
157 repertoire_form = RepertoireSearchForm (request.GET)
158
159 nb_chercheurs = chercheurs.count()
160 variables = { 'chercheurs': chercheurs,
161 'nb_chercheurs': nb_chercheurs,
162 'repertoire_form': repertoire_form,
163 }
164 return render_to_response ("chercheurs/index.html", \
165 Context (variables),
166 context_instance = RequestContext(request))
167
168 def inscription(request):
169 if request.method == 'POST':
170 personne_form = PersonneForm (request.POST, prefix="personne")
171 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
172 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
173 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
174 discipline_form = DisciplineForm (request.POST, prefix="discipline")
175 publication1_form = PublicationForm (request.POST, prefix="publication1")
176 publication2_form = PublicationForm (request.POST, prefix="publication2")
177 publication3_form = PublicationForm (request.POST, prefix="publication3")
178 publication4_form = PublicationForm (request.POST, prefix="publication4")
179 these_form = TheseForm(request.POST, prefix="these")
180 expertise_form = ExpertiseForm(request.POST, prefix="expertise")
181 groupe_form = GroupeForm(request.POST, prefix="groupe")
182
183 if personne_form.is_valid():
184 if chercheur_form.is_valid() and groupe_form.is_valid():
185 c = chercheur_form.save(commit=False)
186
187 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
188 etablissement_autre_form = EtablissementAutreForm (request.POST, prefix="etablissement_autre", instance=c)
189 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
190
191 if etablissement_form.is_valid() and discipline_form.is_valid() and these_form.is_valid():
192 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
193 pub = publication1_form.save()
194 c.publication1 = pub
195 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
196 pub = publication2_form.save()
197 c.publication2 = pub
198 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
199 pub = publication3_form.save()
200 c.publication3 = pub
201 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
202 pub = publication4_form.save()
203 c.publication4 = pub
204 these = these_form.save()
205 if expertise_form.is_valid() and expertise_form.cleaned_data['nom']:
206 expertise = expertise_form.save()
207 c.expertise = expertise
208 c.these = these
209 etablissement_form.save(commit=False)
210 etablissement_autre_form.save(commit=False)
211 discipline_form.save(commit=False)
212 #encodage du mot de passe de l'utilisateur (refactorer car c'est pas clean
213 #et c'est pas la bonne place pour faire ca - AJ
214 personne_form.cleaned_data['password'] = hashlib.md5(personne_form.cleaned_data['password']).hexdigest()
215 p = personne_form.save()
216 c.personne = p
217 c.save()
218
219 #sauvegarde des groupes
220 groupes = request.POST.getlist('groupe-groupes')
221 for g in groupes:
222 g = Groupe.objects.get(pk=g)
223 ChercheurGroupe.objects.get_or_create(chercheur=c, groupe=g, actif=1)
224 return HttpResponseRedirect("/chercheurs/%d/?inscription=1" % c.id)
225 #return HttpResponseRedirect(reverse('chercheurs.views.retrieve', args=(c.id,)))
226 else:
227 personne_form = PersonneForm(prefix="personne")
228 chercheur_form = ChercheurForm(prefix="chercheur")
229 etablissement_form = EtablissementForm(prefix="etablissement")
230 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
231 discipline_form = DisciplineForm(prefix="discipline")
232 publication1_form = PublicationForm(prefix="publication1")
233 publication2_form = PublicationForm(prefix="publication2")
234 publication3_form = PublicationForm(prefix="publication3")
235 publication4_form = PublicationForm(prefix="publication4")
236 these_form = TheseForm(prefix="these")
237 expertise_form = ExpertiseForm(prefix="expertise")
238 groupe_form = GroupeForm(prefix="groupe")
239
240 variables = { 'personne_form': personne_form,
241 'chercheur_form': chercheur_form,
242 'etablissement_form': etablissement_form,
243 'discipline_form': discipline_form,
244 'etablissement_autre_form': etablissement_autre_form,
245 'publication1_form': publication1_form,
246 'publication2_form': publication2_form,
247 'publication3_form': publication3_form,
248 'publication4_form': publication4_form,
249 'these_form': these_form,
250 'expertise_form': expertise_form,
251 'groupe_form': groupe_form,
252 }
253
254 return render_to_response ("chercheurs/inscription.html", \
255 Context (variables),
256 context_instance = RequestContext(request))
257
258 @login_required()
259 def edit(request):
260 """Edition d'un chercheur"""
261 context_instance = RequestContext(request)
262 chercheur = context_instance['user_chercheur']
263 #GroupeFormset = inlineformset_factory(Chercheur, ChercheurGroupe)
264
265 if request.method == 'POST':
266 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
267 chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
268 etablissement_form = EtablissementForm(request.POST, prefix="etablissement", instance=chercheur)
269 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre", instance=chercheur)
270 discipline_form = DisciplineForm(request.POST, prefix="discipline", instance=chercheur)
271 publication1_form = PublicationForm(request.POST, prefix="publication1", instance=chercheur.publication1)
272 publication2_form = PublicationForm(request.POST, prefix="publication2", instance=chercheur.publication2)
273 publication3_form = PublicationForm(request.POST, prefix="publication3", instance=chercheur.publication3)
274 publication4_form = PublicationForm(request.POST, prefix="publication4", instance=chercheur.publication4)
275 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
276 expertise_form = ExpertiseForm(request.POST, prefix="expertise", instance=chercheur.expertise)
277 groupe_form = GroupeForm(request.POST, prefix="groupe", instance=chercheur)
278
279 #formset = GroupeFormset(request.POST, prefix="groupes", instance = chercheur)
280
281 if( personne_form.is_valid() and discipline_form.is_valid() and chercheur_form.is_valid() and these_form.is_valid()
282 and etablissement_form.is_valid() and etablissement_autre_form.save() and groupe_form.is_valid() and expertise_form.is_valid() ):
283 personne_form.save()
284 discipline_form.save()
285 chercheur_form.save()
286 etablissement_form.save()
287 etablissement_autre_form.save()
288
289 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
290 chercheur.publication1 = publication1_form.save()
291 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
292 chercheur.publication2 = publication2_form.save()
293 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
294 chercheur.publication3 = publication3_form.save()
295 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
296 chercheur.publication4 = publication4_form.save()
297 chercheur.these = these_form.save()
298 if expertise_form.cleaned_data['nom']:
299 chercheur.expertise = expertise_form.save()
300 chercheur.save()
301 #Gestion des groupes
302 groupes = request.POST.getlist('groupe-groupes')
303 #On delete les chercheurs deselectionnés
304 ChercheurGroupe.objects.filter(chercheur=chercheur).exclude(groupe__in=groupes).delete()
305 #Sauvegarde des groupes...
306 for g in groupes:
307 g = Groupe.objects.get(pk=g)
308 ChercheurGroupe.objects.get_or_create(chercheur=chercheur, groupe=g, actif=1)
309 return HttpResponseRedirect("/chercheurs/perso/?modification=1")
310
311 #formset.save()
312
313 else:
314 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
315 chercheur_form = ChercheurForm (prefix="chercheur", instance=chercheur)
316 etablissement_form = EtablissementForm(prefix="etablissement", instance=chercheur)
317 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre", instance=chercheur)
318 discipline_form = DisciplineForm(prefix="discipline", instance=chercheur)
319 publication1_form = PublicationForm(prefix="publication1", instance=chercheur.publication1)
320 publication2_form = PublicationForm(prefix="publication2", instance=chercheur.publication2)
321 publication3_form = PublicationForm(prefix="publication3", instance=chercheur.publication3)
322 publication4_form = PublicationForm(prefix="publication4", instance=chercheur.publication4)
323 these_form = TheseForm(prefix="these", instance=chercheur.these)
324 expertise_form = ExpertiseForm(prefix="expertise", instance=chercheur.expertise)
325 groupe_form = GroupeForm(prefix="groupe", instance=chercheur)
326 #formset = GroupeFormset(prefix="groupes", instance = chercheur)
327
328 variables = { 'chercheur': chercheur,
329 'personne_form':personne_form,
330 'chercheur_form': chercheur_form,
331 'etablissement_form': etablissement_form,
332 'discipline_form': discipline_form,
333 'etablissement_autre_form': etablissement_autre_form,
334 'publication1_form': publication1_form,
335 'publication2_form': publication2_form,
336 'publication3_form': publication3_form,
337 'publication4_form': publication4_form,
338 'these_form': these_form,
339 'expertise_form': expertise_form,
340 'groupe_form': groupe_form,
341 #'formset' : formset
342 }
343 return render_to_response ("chercheurs/edit.html", \
344 Context (variables),
345 context_instance = RequestContext(request))
346
347
348 @login_required()
349 def perso(request):
350 """Espace chercheur (espace personnel du chercheur)"""
351 context_instance = RequestContext(request)
352 chercheur = context_instance['user_chercheur']
353 modification = request.GET.get('modification')
354 if not chercheur:
355 return HttpResponseRedirect(reverse('chercheurs.views.chercheur_login'))
356 variables = { 'chercheur': chercheur,
357 'modification': modification,
358 }
359 return render_to_response ("chercheurs/perso.html", \
360 Context (variables),
361 context_instance = RequestContext(request))
362
363 def retrieve(request, id):
364 """Fiche du chercheur"""
365 #chercheur = Chercheur.objects.get(id=id)
366 inscription = request.GET.get('inscription')
367 chercheur = get_object_or_404(Chercheur, id=id)
368 variables = { 'chercheur': chercheur,
369 'inscription': inscription,
370 }
371 return render_to_response ("chercheurs/retrieve.html", \
372 Context (variables),
373 context_instance = RequestContext(request))
374
375 def conversion(request):
376 return render_to_response ("chercheurs/conversion.html", \
377 Context (), \
378 context_instance = RequestContext(request))