Présenter un lien unique "Accès aux inscrits" pour la connexion et
[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
51515982 7from django.core.urlresolvers import reverse as url
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
51515982 104def chercheur_login(request):
c18af6bd 105 "Displays the login form and handles the login action."
c18af6bd
AJ
106 if request.method == "POST":
107 form = AuthenticationForm(data=request.POST)
108 if form.is_valid():
c18af6bd
AJ
109 from django.contrib.auth import login
110 login(request, form.get_user())
111 if request.session.test_cookie_worked():
112 request.session.delete_test_cookie()
51515982 113 return HttpResponseRedirect(url('chercheurs.views.perso'))
c18af6bd
AJ
114 else:
115 form = AuthenticationForm(request)
116 request.session.set_test_cookie()
51515982 117 return render_to_response('accounts/login.html', dict(form=form), context_instance=RequestContext(request))
c18af6bd
AJ
118
119
f8c16b3d 120def index(request):
121 """Répertoire des chercheurs"""
3efbacbe
EMS
122 search_form = RepertoireSearchForm(request.GET)
123 chercheurs = search_form.get_query_set()
9f7c169e 124 nb_chercheurs = chercheurs.count()
3efbacbe
EMS
125 return render_to_response("chercheurs/index.html",
126 dict(chercheurs=chercheurs, nb_chercheurs=nb_chercheurs, repertoire_form=search_form),
127 context_instance=RequestContext(request))
588d6b93 128
932eef9a
AJ
129def inscription(request):
130 if request.method == 'POST':
131 personne_form = PersonneForm (request.POST, prefix="personne")
132 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
7c596de2 133 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
00755d9b 134 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
7c596de2 135 discipline_form = DisciplineForm (request.POST, prefix="discipline")
00755d9b
AJ
136 publication1_form = PublicationForm (request.POST, prefix="publication1")
137 publication2_form = PublicationForm (request.POST, prefix="publication2")
138 publication3_form = PublicationForm (request.POST, prefix="publication3")
f810842d
AJ
139 publication4_form = PublicationForm (request.POST, prefix="publication4")
140 these_form = TheseForm(request.POST, prefix="these")
5b9abc81 141 expertise_form = ExpertiseForm(request.POST, prefix="expertise")
3eb00212 142 groupe_form = GroupeForm(request.POST, prefix="groupe")
7c596de2 143
932eef9a 144 if personne_form.is_valid():
3eb00212 145 if chercheur_form.is_valid() and groupe_form.is_valid():
932eef9a 146 c = chercheur_form.save(commit=False)
7c596de2
AJ
147
148 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
ddf6d787 149 etablissement_autre_form = EtablissementAutreForm (request.POST, prefix="etablissement_autre", instance=c)
7c596de2
AJ
150 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
151
f810842d 152 if etablissement_form.is_valid() and discipline_form.is_valid() and these_form.is_valid():
6befc7c9 153 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
00755d9b
AJ
154 pub = publication1_form.save()
155 c.publication1 = pub
6befc7c9 156 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
00755d9b
AJ
157 pub = publication2_form.save()
158 c.publication2 = pub
6befc7c9 159 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
00755d9b
AJ
160 pub = publication3_form.save()
161 c.publication3 = pub
6befc7c9 162 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
00755d9b
AJ
163 pub = publication4_form.save()
164 c.publication4 = pub
f810842d 165 these = these_form.save()
8ea92a0d
AJ
166 if expertise_form.is_valid() and expertise_form.cleaned_data['nom']:
167 expertise = expertise_form.save()
a4e383ac 168 c.expertise = expertise
f810842d 169 c.these = these
00755d9b
AJ
170 etablissement_form.save(commit=False)
171 etablissement_autre_form.save(commit=False)
7c596de2 172 discipline_form.save(commit=False)
5ecd9e43
AJ
173 #encodage du mot de passe de l'utilisateur (refactorer car c'est pas clean
174 #et c'est pas la bonne place pour faire ca - AJ
175 personne_form.cleaned_data['password'] = hashlib.md5(personne_form.cleaned_data['password']).hexdigest()
7c596de2
AJ
176 p = personne_form.save()
177 c.personne = p
178 c.save()
3eb00212
AJ
179
180 #sauvegarde des groupes
181 groupes = request.POST.getlist('groupe-groupes')
182 for g in groupes:
183 g = Groupe.objects.get(pk=g)
184 ChercheurGroupe.objects.get_or_create(chercheur=c, groupe=g, actif=1)
0e9597af 185 return HttpResponseRedirect("/chercheurs/%d/?inscription=1" % c.id)
51515982 186 #return HttpResponseRedirect(url('chercheurs.views.retrieve', args=(c.id,)))
932eef9a
AJ
187 else:
188 personne_form = PersonneForm(prefix="personne")
189 chercheur_form = ChercheurForm(prefix="chercheur")
7c596de2 190 etablissement_form = EtablissementForm(prefix="etablissement")
00755d9b 191 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
7c596de2 192 discipline_form = DisciplineForm(prefix="discipline")
00755d9b
AJ
193 publication1_form = PublicationForm(prefix="publication1")
194 publication2_form = PublicationForm(prefix="publication2")
195 publication3_form = PublicationForm(prefix="publication3")
f810842d
AJ
196 publication4_form = PublicationForm(prefix="publication4")
197 these_form = TheseForm(prefix="these")
5b9abc81 198 expertise_form = ExpertiseForm(prefix="expertise")
3eb00212 199 groupe_form = GroupeForm(prefix="groupe")
932eef9a
AJ
200
201 variables = { 'personne_form': personne_form,
202 'chercheur_form': chercheur_form,
7c596de2
AJ
203 'etablissement_form': etablissement_form,
204 'discipline_form': discipline_form,
00755d9b
AJ
205 'etablissement_autre_form': etablissement_autre_form,
206 'publication1_form': publication1_form,
207 'publication2_form': publication2_form,
208 'publication3_form': publication3_form,
209 'publication4_form': publication4_form,
f810842d 210 'these_form': these_form,
5b9abc81 211 'expertise_form': expertise_form,
3eb00212 212 'groupe_form': groupe_form,
932eef9a
AJ
213 }
214
215 return render_to_response ("chercheurs/inscription.html", \
216 Context (variables),
217 context_instance = RequestContext(request))
9af73c99 218
0e9597af 219@login_required()
b3e1079e
AJ
220def edit(request):
221 """Edition d'un chercheur"""
222 context_instance = RequestContext(request)
223 chercheur = context_instance['user_chercheur']
e8e9e4fd
AJ
224 #GroupeFormset = inlineformset_factory(Chercheur, ChercheurGroupe)
225
b3e1079e 226 if request.method == 'POST':
a955bdb8 227 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
e8e9e4fd 228 chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
a955bdb8
AJ
229 etablissement_form = EtablissementForm(request.POST, prefix="etablissement", instance=chercheur)
230 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre", instance=chercheur)
231 discipline_form = DisciplineForm(request.POST, prefix="discipline", instance=chercheur)
232 publication1_form = PublicationForm(request.POST, prefix="publication1", instance=chercheur.publication1)
233 publication2_form = PublicationForm(request.POST, prefix="publication2", instance=chercheur.publication2)
234 publication3_form = PublicationForm(request.POST, prefix="publication3", instance=chercheur.publication3)
235 publication4_form = PublicationForm(request.POST, prefix="publication4", instance=chercheur.publication4)
f810842d 236 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
5b9abc81 237 expertise_form = ExpertiseForm(request.POST, prefix="expertise", instance=chercheur.expertise)
3eb00212 238 groupe_form = GroupeForm(request.POST, prefix="groupe", instance=chercheur)
a955bdb8 239
e8e9e4fd
AJ
240 #formset = GroupeFormset(request.POST, prefix="groupes", instance = chercheur)
241
ddf6d787 242 if( personne_form.is_valid() and discipline_form.is_valid() and chercheur_form.is_valid() and these_form.is_valid()
5b9abc81 243 and etablissement_form.is_valid() and etablissement_autre_form.save() and groupe_form.is_valid() and expertise_form.is_valid() ):
a955bdb8 244 personne_form.save()
a955bdb8 245 discipline_form.save()
3eb00212 246 chercheur_form.save()
ddf6d787
AJ
247 etablissement_form.save()
248 etablissement_autre_form.save()
249
e8e9e4fd
AJ
250 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
251 chercheur.publication1 = publication1_form.save()
252 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
253 chercheur.publication2 = publication2_form.save()
254 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
255 chercheur.publication3 = publication3_form.save()
256 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
257 chercheur.publication4 = publication4_form.save()
5b9abc81 258 chercheur.these = these_form.save()
8ea92a0d
AJ
259 if expertise_form.cleaned_data['nom']:
260 chercheur.expertise = expertise_form.save()
e8e9e4fd
AJ
261 chercheur.save()
262 #Gestion des groupes
3eb00212 263 groupes = request.POST.getlist('groupe-groupes')
e8e9e4fd
AJ
264 #On delete les chercheurs deselectionnés
265 ChercheurGroupe.objects.filter(chercheur=chercheur).exclude(groupe__in=groupes).delete()
266 #Sauvegarde des groupes...
267 for g in groupes:
268 g = Groupe.objects.get(pk=g)
269 ChercheurGroupe.objects.get_or_create(chercheur=chercheur, groupe=g, actif=1)
2a36714f 270 return HttpResponseRedirect("/chercheurs/perso/?modification=1")
e8e9e4fd
AJ
271
272 #formset.save()
273
b3e1079e 274 else:
a955bdb8 275 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
e8e9e4fd 276 chercheur_form = ChercheurForm (prefix="chercheur", instance=chercheur)
a955bdb8
AJ
277 etablissement_form = EtablissementForm(prefix="etablissement", instance=chercheur)
278 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre", instance=chercheur)
279 discipline_form = DisciplineForm(prefix="discipline", instance=chercheur)
280 publication1_form = PublicationForm(prefix="publication1", instance=chercheur.publication1)
281 publication2_form = PublicationForm(prefix="publication2", instance=chercheur.publication2)
282 publication3_form = PublicationForm(prefix="publication3", instance=chercheur.publication3)
f810842d
AJ
283 publication4_form = PublicationForm(prefix="publication4", instance=chercheur.publication4)
284 these_form = TheseForm(prefix="these", instance=chercheur.these)
5b9abc81 285 expertise_form = ExpertiseForm(prefix="expertise", instance=chercheur.expertise)
3eb00212 286 groupe_form = GroupeForm(prefix="groupe", instance=chercheur)
e8e9e4fd 287 #formset = GroupeFormset(prefix="groupes", instance = chercheur)
b3e1079e
AJ
288
289 variables = { 'chercheur': chercheur,
290 'personne_form':personne_form,
e8e9e4fd 291 'chercheur_form': chercheur_form,
a955bdb8
AJ
292 'etablissement_form': etablissement_form,
293 'discipline_form': discipline_form,
294 'etablissement_autre_form': etablissement_autre_form,
295 'publication1_form': publication1_form,
296 'publication2_form': publication2_form,
297 'publication3_form': publication3_form,
298 'publication4_form': publication4_form,
f810842d 299 'these_form': these_form,
5b9abc81 300 'expertise_form': expertise_form,
3eb00212 301 'groupe_form': groupe_form,
e8e9e4fd 302 #'formset' : formset
b3e1079e
AJ
303 }
304 return render_to_response ("chercheurs/edit.html", \
305 Context (variables),
306 context_instance = RequestContext(request))
307
308
0e9597af 309@login_required()
9af73c99 310def perso(request):
0d9d1c4d 311 """Espace chercheur (espace personnel du chercheur)"""
9af73c99
AJ
312 context_instance = RequestContext(request)
313 chercheur = context_instance['user_chercheur']
2a36714f 314 modification = request.GET.get('modification')
0d9d1c4d 315 if not chercheur:
51515982 316 return HttpResponseRedirect(url('chercheurs.views.chercheur_login'))
588d6b93 317 variables = { 'chercheur': chercheur,
2a36714f 318 'modification': modification,
588d6b93 319 }
320 return render_to_response ("chercheurs/perso.html", \
321 Context (variables),
322 context_instance = RequestContext(request))
da091176 323
324def retrieve(request, id):
325 """Fiche du chercheur"""
3eb00212 326 #chercheur = Chercheur.objects.get(id=id)
0e9597af 327 inscription = request.GET.get('inscription')
3eb00212 328 chercheur = get_object_or_404(Chercheur, id=id)
da091176 329 variables = { 'chercheur': chercheur,
0e9597af 330 'inscription': inscription,
da091176 331 }
332 return render_to_response ("chercheurs/retrieve.html", \
333 Context (variables),
334 context_instance = RequestContext(request))
d9885bf7 335
336def conversion(request):
337 return render_to_response ("chercheurs/conversion.html", \
338 Context (), \
339 context_instance = RequestContext(request))