Merge branch 'master' of ssh://git.auf/auf_savoirs_en_partage_django
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / views.py
CommitLineData
588d6b93 1# -*- encoding: utf-8 -*-
5ecd9e43 2import hashlib
932eef9a 3from django.shortcuts import render_to_response
0e9597af 4from django.http import HttpResponseRedirect, HttpResponse
932eef9a 5from django.template import Context, RequestContext
e427f068 6from django.template.loader import get_template
5ecd9e43 7from django.core.urlresolvers import reverse
0e9597af
AJ
8from django.core.mail import send_mail
9from django.conf import settings
e8e9e4fd 10
932eef9a 11from forms import *
e8e9e4fd 12from django.forms.models import inlineformset_factory
932eef9a
AJ
13
14from auf_references_client.models import Discipline, TypeImplantation
e8e9e4fd 15from models import Personne, Utilisateur, Groupe, ChercheurGroupe
932eef9a 16
9af73c99 17from django.contrib.auth.decorators import login_required
13146d99 18
510b5321 19from django.db.models import Q
3eb00212 20from django.shortcuts import get_object_or_404
510b5321 21
c18af6bd
AJ
22from django.utils.translation import ugettext_lazy as _
23from django.contrib.auth.forms import AuthenticationForm as OriginalAuthenticationForm
24
0e9597af
AJ
25#TODO: Migrer tout ce qui a rapport aux users dans une nouvelle app
26
c18af6bd 27class AuthenticationForm(OriginalAuthenticationForm):
b5af3133 28 username = forms.CharField(label='Adresse électronique', max_length=255)
c18af6bd 29
0e9597af
AJ
30def 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)
e427f068
AJ
38
39 variables = { 'user': u,
40 'link': link,
206989d4
AJ
41 'SITE_ROOT_URL': settings.SITE_ROOT_URL,
42 'CONTACT_EMAIL': settings.CONTACT_EMAIL,
e427f068
AJ
43 }
44 t = get_template('accounts/email_password.html')
45 content = t.render(Context(variables))
46
0e9597af 47 send_mail('Savoirs en partage: changement de mot de passe',
e427f068 48 content, settings.CONTACT_EMAIL,
0e9597af
AJ
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))
e427f068 58
0e9597af
AJ
59def 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]
e427f068 63 message=""
0e9597af 64 if(code == original_code):
e427f068
AJ
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()
0e9597af
AJ
74 else:
75 return HttpResponseRedirect('/')
e427f068
AJ
76 variables = { 'form': form,
77 'message': message,
0e9597af
AJ
78 }
79 return render_to_response ("accounts/new_password.html", \
80 Context (variables),
81 context_instance = RequestContext(request))
c18af6bd 82
e427f068
AJ
83@login_required()
84def 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
c18af6bd
AJ
104def 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
13146d99
AJ
127def 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:
54ab837c 135 list = list.filter(Q(etablissement__pays = pays.pk) | Q(etablissement_autre_pays = pays.pk))
13146d99
AJ
136 discipline = simpleForm.cleaned_data["discipline"]
137 if discipline:
ee46b1a2
AJ
138 list = list.filter(discipline=discipline)
139 domaine = simpleForm.cleaned_data["domaine"]
140 if domaine:
141 list = list.filter(groupes=domaine)
510b5321
AJ
142 mots_cles = simpleForm.cleaned_data["mots_cles"]
143 if mots_cles:
a2c6bb72 144 list = list.search(mots_cles)
6bd49ff1
AJ
145 statut = simpleForm.cleaned_data["statut"]
146 if statut:
147 if statut == "expert":
8ea92a0d 148 list = list.exclude(expertise=None)
6bd49ff1
AJ
149 else:
150 list = list.filter(statut=statut)
13146d99
AJ
151 return list
152
f8c16b3d 153def index(request):
154 """Répertoire des chercheurs"""
13146d99
AJ
155
156 chercheurs = chercheur_queryset (request)
157 repertoire_form = RepertoireSearchForm (request.GET)
158
9f7c169e 159 nb_chercheurs = chercheurs.count()
588d6b93 160 variables = { 'chercheurs': chercheurs,
9f7c169e 161 'nb_chercheurs': nb_chercheurs,
13146d99 162 'repertoire_form': repertoire_form,
588d6b93 163 }
03bce417 164 return render_to_response ("chercheurs/index.html", \
588d6b93 165 Context (variables),
166 context_instance = RequestContext(request))
167
932eef9a
AJ
168def inscription(request):
169 if request.method == 'POST':
170 personne_form = PersonneForm (request.POST, prefix="personne")
171 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
7c596de2 172 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
00755d9b 173 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
7c596de2 174 discipline_form = DisciplineForm (request.POST, prefix="discipline")
00755d9b
AJ
175 publication1_form = PublicationForm (request.POST, prefix="publication1")
176 publication2_form = PublicationForm (request.POST, prefix="publication2")
177 publication3_form = PublicationForm (request.POST, prefix="publication3")
f810842d
AJ
178 publication4_form = PublicationForm (request.POST, prefix="publication4")
179 these_form = TheseForm(request.POST, prefix="these")
5b9abc81 180 expertise_form = ExpertiseForm(request.POST, prefix="expertise")
3eb00212 181 groupe_form = GroupeForm(request.POST, prefix="groupe")
7c596de2 182
932eef9a 183 if personne_form.is_valid():
3eb00212 184 if chercheur_form.is_valid() and groupe_form.is_valid():
932eef9a 185 c = chercheur_form.save(commit=False)
7c596de2
AJ
186
187 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
ddf6d787 188 etablissement_autre_form = EtablissementAutreForm (request.POST, prefix="etablissement_autre", instance=c)
7c596de2
AJ
189 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
190
f810842d 191 if etablissement_form.is_valid() and discipline_form.is_valid() and these_form.is_valid():
6befc7c9 192 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
00755d9b
AJ
193 pub = publication1_form.save()
194 c.publication1 = pub
6befc7c9 195 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
00755d9b
AJ
196 pub = publication2_form.save()
197 c.publication2 = pub
6befc7c9 198 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
00755d9b
AJ
199 pub = publication3_form.save()
200 c.publication3 = pub
6befc7c9 201 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
00755d9b
AJ
202 pub = publication4_form.save()
203 c.publication4 = pub
f810842d 204 these = these_form.save()
8ea92a0d
AJ
205 if expertise_form.is_valid() and expertise_form.cleaned_data['nom']:
206 expertise = expertise_form.save()
a4e383ac 207 c.expertise = expertise
f810842d 208 c.these = these
00755d9b
AJ
209 etablissement_form.save(commit=False)
210 etablissement_autre_form.save(commit=False)
7c596de2 211 discipline_form.save(commit=False)
5ecd9e43
AJ
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()
7c596de2
AJ
215 p = personne_form.save()
216 c.personne = p
217 c.save()
3eb00212
AJ
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)
0e9597af
AJ
224 return HttpResponseRedirect("/chercheurs/%d/?inscription=1" % c.id)
225 #return HttpResponseRedirect(reverse('chercheurs.views.retrieve', args=(c.id,)))
932eef9a
AJ
226 else:
227 personne_form = PersonneForm(prefix="personne")
228 chercheur_form = ChercheurForm(prefix="chercheur")
7c596de2 229 etablissement_form = EtablissementForm(prefix="etablissement")
00755d9b 230 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
7c596de2 231 discipline_form = DisciplineForm(prefix="discipline")
00755d9b
AJ
232 publication1_form = PublicationForm(prefix="publication1")
233 publication2_form = PublicationForm(prefix="publication2")
234 publication3_form = PublicationForm(prefix="publication3")
f810842d
AJ
235 publication4_form = PublicationForm(prefix="publication4")
236 these_form = TheseForm(prefix="these")
5b9abc81 237 expertise_form = ExpertiseForm(prefix="expertise")
3eb00212 238 groupe_form = GroupeForm(prefix="groupe")
932eef9a
AJ
239
240 variables = { 'personne_form': personne_form,
241 'chercheur_form': chercheur_form,
7c596de2
AJ
242 'etablissement_form': etablissement_form,
243 'discipline_form': discipline_form,
00755d9b
AJ
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,
f810842d 249 'these_form': these_form,
5b9abc81 250 'expertise_form': expertise_form,
3eb00212 251 'groupe_form': groupe_form,
932eef9a
AJ
252 }
253
254 return render_to_response ("chercheurs/inscription.html", \
255 Context (variables),
256 context_instance = RequestContext(request))
9af73c99 257
0e9597af 258@login_required()
b3e1079e
AJ
259def edit(request):
260 """Edition d'un chercheur"""
261 context_instance = RequestContext(request)
262 chercheur = context_instance['user_chercheur']
e8e9e4fd
AJ
263 #GroupeFormset = inlineformset_factory(Chercheur, ChercheurGroupe)
264
b3e1079e 265 if request.method == 'POST':
a955bdb8 266 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
e8e9e4fd 267 chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
a955bdb8
AJ
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)
f810842d 275 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
5b9abc81 276 expertise_form = ExpertiseForm(request.POST, prefix="expertise", instance=chercheur.expertise)
3eb00212 277 groupe_form = GroupeForm(request.POST, prefix="groupe", instance=chercheur)
a955bdb8 278
e8e9e4fd
AJ
279 #formset = GroupeFormset(request.POST, prefix="groupes", instance = chercheur)
280
ddf6d787 281 if( personne_form.is_valid() and discipline_form.is_valid() and chercheur_form.is_valid() and these_form.is_valid()
5b9abc81 282 and etablissement_form.is_valid() and etablissement_autre_form.save() and groupe_form.is_valid() and expertise_form.is_valid() ):
a955bdb8 283 personne_form.save()
a955bdb8 284 discipline_form.save()
3eb00212 285 chercheur_form.save()
ddf6d787
AJ
286 etablissement_form.save()
287 etablissement_autre_form.save()
288
e8e9e4fd
AJ
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()
5b9abc81 297 chercheur.these = these_form.save()
8ea92a0d
AJ
298 if expertise_form.cleaned_data['nom']:
299 chercheur.expertise = expertise_form.save()
e8e9e4fd
AJ
300 chercheur.save()
301 #Gestion des groupes
3eb00212 302 groupes = request.POST.getlist('groupe-groupes')
e8e9e4fd
AJ
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)
2a36714f 309 return HttpResponseRedirect("/chercheurs/perso/?modification=1")
e8e9e4fd
AJ
310
311 #formset.save()
312
b3e1079e 313 else:
a955bdb8 314 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
e8e9e4fd 315 chercheur_form = ChercheurForm (prefix="chercheur", instance=chercheur)
a955bdb8
AJ
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)
f810842d
AJ
322 publication4_form = PublicationForm(prefix="publication4", instance=chercheur.publication4)
323 these_form = TheseForm(prefix="these", instance=chercheur.these)
5b9abc81 324 expertise_form = ExpertiseForm(prefix="expertise", instance=chercheur.expertise)
3eb00212 325 groupe_form = GroupeForm(prefix="groupe", instance=chercheur)
e8e9e4fd 326 #formset = GroupeFormset(prefix="groupes", instance = chercheur)
b3e1079e
AJ
327
328 variables = { 'chercheur': chercheur,
329 'personne_form':personne_form,
e8e9e4fd 330 'chercheur_form': chercheur_form,
a955bdb8
AJ
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,
f810842d 338 'these_form': these_form,
5b9abc81 339 'expertise_form': expertise_form,
3eb00212 340 'groupe_form': groupe_form,
e8e9e4fd 341 #'formset' : formset
b3e1079e
AJ
342 }
343 return render_to_response ("chercheurs/edit.html", \
344 Context (variables),
345 context_instance = RequestContext(request))
346
347
0e9597af 348@login_required()
9af73c99 349def perso(request):
0d9d1c4d 350 """Espace chercheur (espace personnel du chercheur)"""
9af73c99
AJ
351 context_instance = RequestContext(request)
352 chercheur = context_instance['user_chercheur']
2a36714f 353 modification = request.GET.get('modification')
0d9d1c4d 354 if not chercheur:
909db6d4 355 return HttpResponseRedirect(reverse('chercheurs.views.chercheur_login'))
588d6b93 356 variables = { 'chercheur': chercheur,
2a36714f 357 'modification': modification,
588d6b93 358 }
359 return render_to_response ("chercheurs/perso.html", \
360 Context (variables),
361 context_instance = RequestContext(request))
da091176 362
363def retrieve(request, id):
364 """Fiche du chercheur"""
3eb00212 365 #chercheur = Chercheur.objects.get(id=id)
0e9597af 366 inscription = request.GET.get('inscription')
3eb00212 367 chercheur = get_object_or_404(Chercheur, id=id)
da091176 368 variables = { 'chercheur': chercheur,
0e9597af 369 'inscription': inscription,
da091176 370 }
371 return render_to_response ("chercheurs/retrieve.html", \
372 Context (variables),
373 context_instance = RequestContext(request))
d9885bf7 374
375def conversion(request):
376 return render_to_response ("chercheurs/conversion.html", \
377 Context (), \
378 context_instance = RequestContext(request))