Merge branch 'master' of gitosis@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
AJ
27class AuthenticationForm(OriginalAuthenticationForm):
28 username = forms.CharField(label=_("Username"), max_length=255)
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)
13146d99
AJ
145 return list
146
f8c16b3d 147def index(request):
148 """Répertoire des chercheurs"""
13146d99
AJ
149
150 chercheurs = chercheur_queryset (request)
151 repertoire_form = RepertoireSearchForm (request.GET)
152
9f7c169e 153 nb_chercheurs = chercheurs.count()
588d6b93 154 variables = { 'chercheurs': chercheurs,
9f7c169e 155 'nb_chercheurs': nb_chercheurs,
13146d99 156 'repertoire_form': repertoire_form,
588d6b93 157 }
03bce417 158 return render_to_response ("chercheurs/index.html", \
588d6b93 159 Context (variables),
160 context_instance = RequestContext(request))
161
932eef9a
AJ
162def inscription(request):
163 if request.method == 'POST':
164 personne_form = PersonneForm (request.POST, prefix="personne")
165 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
7c596de2 166 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
00755d9b 167 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
7c596de2 168 discipline_form = DisciplineForm (request.POST, prefix="discipline")
00755d9b
AJ
169 publication1_form = PublicationForm (request.POST, prefix="publication1")
170 publication2_form = PublicationForm (request.POST, prefix="publication2")
171 publication3_form = PublicationForm (request.POST, prefix="publication3")
f810842d
AJ
172 publication4_form = PublicationForm (request.POST, prefix="publication4")
173 these_form = TheseForm(request.POST, prefix="these")
5b9abc81 174 expertise_form = ExpertiseForm(request.POST, prefix="expertise")
3eb00212 175 groupe_form = GroupeForm(request.POST, prefix="groupe")
7c596de2 176
932eef9a 177 if personne_form.is_valid():
3eb00212 178 if chercheur_form.is_valid() and groupe_form.is_valid():
932eef9a 179 c = chercheur_form.save(commit=False)
7c596de2
AJ
180
181 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
ddf6d787 182 etablissement_autre_form = EtablissementAutreForm (request.POST, prefix="etablissement_autre", instance=c)
7c596de2
AJ
183 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
184
f810842d 185 if etablissement_form.is_valid() and discipline_form.is_valid() and these_form.is_valid():
6befc7c9 186 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
00755d9b
AJ
187 pub = publication1_form.save()
188 c.publication1 = pub
6befc7c9 189 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
00755d9b
AJ
190 pub = publication2_form.save()
191 c.publication2 = pub
6befc7c9 192 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
00755d9b
AJ
193 pub = publication3_form.save()
194 c.publication3 = pub
6befc7c9 195 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
00755d9b
AJ
196 pub = publication4_form.save()
197 c.publication4 = pub
f810842d 198 these = these_form.save()
5b9abc81 199 expertise = expertise_form.save()
f810842d 200 c.these = these
5b9abc81 201 c.expertise = expertise
00755d9b
AJ
202 etablissement_form.save(commit=False)
203 etablissement_autre_form.save(commit=False)
7c596de2 204 discipline_form.save(commit=False)
5ecd9e43
AJ
205 #encodage du mot de passe de l'utilisateur (refactorer car c'est pas clean
206 #et c'est pas la bonne place pour faire ca - AJ
207 personne_form.cleaned_data['password'] = hashlib.md5(personne_form.cleaned_data['password']).hexdigest()
7c596de2
AJ
208 p = personne_form.save()
209 c.personne = p
210 c.save()
3eb00212
AJ
211
212 #sauvegarde des groupes
213 groupes = request.POST.getlist('groupe-groupes')
214 for g in groupes:
215 g = Groupe.objects.get(pk=g)
216 ChercheurGroupe.objects.get_or_create(chercheur=c, groupe=g, actif=1)
0e9597af
AJ
217 return HttpResponseRedirect("/chercheurs/%d/?inscription=1" % c.id)
218 #return HttpResponseRedirect(reverse('chercheurs.views.retrieve', args=(c.id,)))
932eef9a
AJ
219 else:
220 personne_form = PersonneForm(prefix="personne")
221 chercheur_form = ChercheurForm(prefix="chercheur")
7c596de2 222 etablissement_form = EtablissementForm(prefix="etablissement")
00755d9b 223 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
7c596de2 224 discipline_form = DisciplineForm(prefix="discipline")
00755d9b
AJ
225 publication1_form = PublicationForm(prefix="publication1")
226 publication2_form = PublicationForm(prefix="publication2")
227 publication3_form = PublicationForm(prefix="publication3")
f810842d
AJ
228 publication4_form = PublicationForm(prefix="publication4")
229 these_form = TheseForm(prefix="these")
5b9abc81 230 expertise_form = ExpertiseForm(prefix="expertise")
3eb00212 231 groupe_form = GroupeForm(prefix="groupe")
932eef9a
AJ
232
233 variables = { 'personne_form': personne_form,
234 'chercheur_form': chercheur_form,
7c596de2
AJ
235 'etablissement_form': etablissement_form,
236 'discipline_form': discipline_form,
00755d9b
AJ
237 'etablissement_autre_form': etablissement_autre_form,
238 'publication1_form': publication1_form,
239 'publication2_form': publication2_form,
240 'publication3_form': publication3_form,
241 'publication4_form': publication4_form,
f810842d 242 'these_form': these_form,
5b9abc81 243 'expertise_form': expertise_form,
3eb00212 244 'groupe_form': groupe_form,
932eef9a
AJ
245 }
246
247 return render_to_response ("chercheurs/inscription.html", \
248 Context (variables),
249 context_instance = RequestContext(request))
9af73c99 250
0e9597af 251@login_required()
b3e1079e
AJ
252def edit(request):
253 """Edition d'un chercheur"""
254 context_instance = RequestContext(request)
255 chercheur = context_instance['user_chercheur']
e8e9e4fd
AJ
256 #GroupeFormset = inlineformset_factory(Chercheur, ChercheurGroupe)
257
b3e1079e 258 if request.method == 'POST':
a955bdb8 259 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
e8e9e4fd 260 chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
a955bdb8
AJ
261 etablissement_form = EtablissementForm(request.POST, prefix="etablissement", instance=chercheur)
262 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre", instance=chercheur)
263 discipline_form = DisciplineForm(request.POST, prefix="discipline", instance=chercheur)
264 publication1_form = PublicationForm(request.POST, prefix="publication1", instance=chercheur.publication1)
265 publication2_form = PublicationForm(request.POST, prefix="publication2", instance=chercheur.publication2)
266 publication3_form = PublicationForm(request.POST, prefix="publication3", instance=chercheur.publication3)
267 publication4_form = PublicationForm(request.POST, prefix="publication4", instance=chercheur.publication4)
f810842d 268 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
5b9abc81 269 expertise_form = ExpertiseForm(request.POST, prefix="expertise", instance=chercheur.expertise)
3eb00212 270 groupe_form = GroupeForm(request.POST, prefix="groupe", instance=chercheur)
a955bdb8 271
e8e9e4fd
AJ
272 #formset = GroupeFormset(request.POST, prefix="groupes", instance = chercheur)
273
ddf6d787 274 if( personne_form.is_valid() and discipline_form.is_valid() and chercheur_form.is_valid() and these_form.is_valid()
5b9abc81 275 and etablissement_form.is_valid() and etablissement_autre_form.save() and groupe_form.is_valid() and expertise_form.is_valid() ):
a955bdb8 276 personne_form.save()
a955bdb8 277 discipline_form.save()
3eb00212 278 chercheur_form.save()
ddf6d787
AJ
279 etablissement_form.save()
280 etablissement_autre_form.save()
281
e8e9e4fd
AJ
282 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
283 chercheur.publication1 = publication1_form.save()
284 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
285 chercheur.publication2 = publication2_form.save()
286 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
287 chercheur.publication3 = publication3_form.save()
288 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
289 chercheur.publication4 = publication4_form.save()
5b9abc81
AJ
290 chercheur.these = these_form.save()
291 chercheur.expertise = expertise_form.save()
e8e9e4fd
AJ
292 chercheur.save()
293 #Gestion des groupes
3eb00212 294 groupes = request.POST.getlist('groupe-groupes')
e8e9e4fd
AJ
295 #On delete les chercheurs deselectionnés
296 ChercheurGroupe.objects.filter(chercheur=chercheur).exclude(groupe__in=groupes).delete()
297 #Sauvegarde des groupes...
298 for g in groupes:
299 g = Groupe.objects.get(pk=g)
300 ChercheurGroupe.objects.get_or_create(chercheur=chercheur, groupe=g, actif=1)
2a36714f 301 return HttpResponseRedirect("/chercheurs/perso/?modification=1")
e8e9e4fd
AJ
302
303 #formset.save()
304
b3e1079e 305 else:
a955bdb8 306 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
e8e9e4fd 307 chercheur_form = ChercheurForm (prefix="chercheur", instance=chercheur)
a955bdb8
AJ
308 etablissement_form = EtablissementForm(prefix="etablissement", instance=chercheur)
309 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre", instance=chercheur)
310 discipline_form = DisciplineForm(prefix="discipline", instance=chercheur)
311 publication1_form = PublicationForm(prefix="publication1", instance=chercheur.publication1)
312 publication2_form = PublicationForm(prefix="publication2", instance=chercheur.publication2)
313 publication3_form = PublicationForm(prefix="publication3", instance=chercheur.publication3)
f810842d
AJ
314 publication4_form = PublicationForm(prefix="publication4", instance=chercheur.publication4)
315 these_form = TheseForm(prefix="these", instance=chercheur.these)
5b9abc81 316 expertise_form = ExpertiseForm(prefix="expertise", instance=chercheur.expertise)
3eb00212 317 groupe_form = GroupeForm(prefix="groupe", instance=chercheur)
e8e9e4fd 318 #formset = GroupeFormset(prefix="groupes", instance = chercheur)
b3e1079e
AJ
319
320 variables = { 'chercheur': chercheur,
321 'personne_form':personne_form,
e8e9e4fd 322 'chercheur_form': chercheur_form,
a955bdb8
AJ
323 'etablissement_form': etablissement_form,
324 'discipline_form': discipline_form,
325 'etablissement_autre_form': etablissement_autre_form,
326 'publication1_form': publication1_form,
327 'publication2_form': publication2_form,
328 'publication3_form': publication3_form,
329 'publication4_form': publication4_form,
f810842d 330 'these_form': these_form,
5b9abc81 331 'expertise_form': expertise_form,
3eb00212 332 'groupe_form': groupe_form,
e8e9e4fd 333 #'formset' : formset
b3e1079e
AJ
334 }
335 return render_to_response ("chercheurs/edit.html", \
336 Context (variables),
337 context_instance = RequestContext(request))
338
339
0e9597af 340@login_required()
9af73c99 341def perso(request):
0d9d1c4d 342 """Espace chercheur (espace personnel du chercheur)"""
9af73c99
AJ
343 context_instance = RequestContext(request)
344 chercheur = context_instance['user_chercheur']
2a36714f 345 modification = request.GET.get('modification')
0d9d1c4d 346 if not chercheur:
909db6d4 347 return HttpResponseRedirect(reverse('chercheurs.views.chercheur_login'))
588d6b93 348 variables = { 'chercheur': chercheur,
2a36714f 349 'modification': modification,
588d6b93 350 }
351 return render_to_response ("chercheurs/perso.html", \
352 Context (variables),
353 context_instance = RequestContext(request))
da091176 354
355def retrieve(request, id):
356 """Fiche du chercheur"""
3eb00212 357 #chercheur = Chercheur.objects.get(id=id)
0e9597af 358 inscription = request.GET.get('inscription')
3eb00212 359 chercheur = get_object_or_404(Chercheur, id=id)
da091176 360 variables = { 'chercheur': chercheur,
0e9597af 361 'inscription': inscription,
da091176 362 }
363 return render_to_response ("chercheurs/retrieve.html", \
364 Context (variables),
365 context_instance = RequestContext(request))