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
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 fonction = simpleForm.cleaned_data["fonction"]
137 if fonction:
ee46b1a2 138 list = list.filter(fonction = fonction)
13146d99
AJ
139 discipline = simpleForm.cleaned_data["discipline"]
140 if discipline:
ee46b1a2
AJ
141 list = list.filter(discipline=discipline)
142 domaine = simpleForm.cleaned_data["domaine"]
143 if domaine:
144 list = list.filter(groupes=domaine)
510b5321
AJ
145 mots_cles = simpleForm.cleaned_data["mots_cles"]
146 if mots_cles:
54ab837c
AJ
147 list = list.filter( Q(personne__nom__search=mots_cles)
148 | Q(personne__prenom__search=mots_cles)
149 | Q(expertise__search=mots_cles)
150 | Q(etablissement_autre_nom__search=mots_cles)
151 | Q(etablissement__nom__search=mots_cles) )
13146d99
AJ
152 return list
153
f8c16b3d 154def index(request):
155 """Répertoire des chercheurs"""
13146d99
AJ
156
157 chercheurs = chercheur_queryset (request)
158 repertoire_form = RepertoireSearchForm (request.GET)
159
9f7c169e 160 nb_chercheurs = chercheurs.count()
588d6b93 161 variables = { 'chercheurs': chercheurs,
9f7c169e 162 'nb_chercheurs': nb_chercheurs,
13146d99 163 'repertoire_form': repertoire_form,
588d6b93 164 }
03bce417 165 return render_to_response ("chercheurs/index.html", \
588d6b93 166 Context (variables),
167 context_instance = RequestContext(request))
168
932eef9a
AJ
169def inscription(request):
170 if request.method == 'POST':
171 personne_form = PersonneForm (request.POST, prefix="personne")
172 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
7c596de2 173 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
00755d9b 174 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
7c596de2 175 discipline_form = DisciplineForm (request.POST, prefix="discipline")
00755d9b
AJ
176 publication1_form = PublicationForm (request.POST, prefix="publication1")
177 publication2_form = PublicationForm (request.POST, prefix="publication2")
178 publication3_form = PublicationForm (request.POST, prefix="publication3")
f810842d
AJ
179 publication4_form = PublicationForm (request.POST, prefix="publication4")
180 these_form = TheseForm(request.POST, prefix="these")
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
AJ
204 these = these_form.save()
205 c.these = these
00755d9b
AJ
206 etablissement_form.save(commit=False)
207 etablissement_autre_form.save(commit=False)
7c596de2 208 discipline_form.save(commit=False)
5ecd9e43
AJ
209 #encodage du mot de passe de l'utilisateur (refactorer car c'est pas clean
210 #et c'est pas la bonne place pour faire ca - AJ
211 personne_form.cleaned_data['password'] = hashlib.md5(personne_form.cleaned_data['password']).hexdigest()
7c596de2
AJ
212 p = personne_form.save()
213 c.personne = p
214 c.save()
3eb00212
AJ
215
216 #sauvegarde des groupes
217 groupes = request.POST.getlist('groupe-groupes')
218 for g in groupes:
219 g = Groupe.objects.get(pk=g)
220 ChercheurGroupe.objects.get_or_create(chercheur=c, groupe=g, actif=1)
0e9597af
AJ
221 return HttpResponseRedirect("/chercheurs/%d/?inscription=1" % c.id)
222 #return HttpResponseRedirect(reverse('chercheurs.views.retrieve', args=(c.id,)))
932eef9a
AJ
223 else:
224 personne_form = PersonneForm(prefix="personne")
225 chercheur_form = ChercheurForm(prefix="chercheur")
7c596de2 226 etablissement_form = EtablissementForm(prefix="etablissement")
00755d9b 227 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
7c596de2 228 discipline_form = DisciplineForm(prefix="discipline")
00755d9b
AJ
229 publication1_form = PublicationForm(prefix="publication1")
230 publication2_form = PublicationForm(prefix="publication2")
231 publication3_form = PublicationForm(prefix="publication3")
f810842d
AJ
232 publication4_form = PublicationForm(prefix="publication4")
233 these_form = TheseForm(prefix="these")
3eb00212 234 groupe_form = GroupeForm(prefix="groupe")
932eef9a
AJ
235
236 variables = { 'personne_form': personne_form,
237 'chercheur_form': chercheur_form,
7c596de2
AJ
238 'etablissement_form': etablissement_form,
239 'discipline_form': discipline_form,
00755d9b
AJ
240 'etablissement_autre_form': etablissement_autre_form,
241 'publication1_form': publication1_form,
242 'publication2_form': publication2_form,
243 'publication3_form': publication3_form,
244 'publication4_form': publication4_form,
f810842d 245 'these_form': these_form,
3eb00212 246 'groupe_form': groupe_form,
932eef9a
AJ
247 }
248
249 return render_to_response ("chercheurs/inscription.html", \
250 Context (variables),
251 context_instance = RequestContext(request))
9af73c99 252
0e9597af 253@login_required()
b3e1079e
AJ
254def edit(request):
255 """Edition d'un chercheur"""
256 context_instance = RequestContext(request)
257 chercheur = context_instance['user_chercheur']
e8e9e4fd
AJ
258 #GroupeFormset = inlineformset_factory(Chercheur, ChercheurGroupe)
259
b3e1079e 260 if request.method == 'POST':
a955bdb8 261 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
e8e9e4fd 262 chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
a955bdb8
AJ
263 etablissement_form = EtablissementForm(request.POST, prefix="etablissement", instance=chercheur)
264 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre", instance=chercheur)
265 discipline_form = DisciplineForm(request.POST, prefix="discipline", instance=chercheur)
266 publication1_form = PublicationForm(request.POST, prefix="publication1", instance=chercheur.publication1)
267 publication2_form = PublicationForm(request.POST, prefix="publication2", instance=chercheur.publication2)
268 publication3_form = PublicationForm(request.POST, prefix="publication3", instance=chercheur.publication3)
269 publication4_form = PublicationForm(request.POST, prefix="publication4", instance=chercheur.publication4)
f810842d 270 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
3eb00212 271 groupe_form = GroupeForm(request.POST, prefix="groupe", instance=chercheur)
a955bdb8 272
e8e9e4fd
AJ
273 #formset = GroupeFormset(request.POST, prefix="groupes", instance = chercheur)
274
ddf6d787
AJ
275 if( personne_form.is_valid() and discipline_form.is_valid() and chercheur_form.is_valid() and these_form.is_valid()
276 and etablissement_form.is_valid() and etablissement_autre_form.save() and groupe_form.is_valid() ):
a955bdb8 277 personne_form.save()
a955bdb8 278 discipline_form.save()
3eb00212 279 chercheur_form.save()
ddf6d787
AJ
280 etablissement_form.save()
281 etablissement_autre_form.save()
282
e8e9e4fd
AJ
283 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
284 chercheur.publication1 = publication1_form.save()
285 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
286 chercheur.publication2 = publication2_form.save()
287 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
288 chercheur.publication3 = publication3_form.save()
289 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
290 chercheur.publication4 = publication4_form.save()
291 chercheur.these = these_form.save()
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)
301
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)
3eb00212 316 groupe_form = GroupeForm(prefix="groupe", instance=chercheur)
e8e9e4fd 317 #formset = GroupeFormset(prefix="groupes", instance = chercheur)
b3e1079e
AJ
318
319 variables = { 'chercheur': chercheur,
320 'personne_form':personne_form,
e8e9e4fd 321 'chercheur_form': chercheur_form,
a955bdb8
AJ
322 'etablissement_form': etablissement_form,
323 'discipline_form': discipline_form,
324 'etablissement_autre_form': etablissement_autre_form,
325 'publication1_form': publication1_form,
326 'publication2_form': publication2_form,
327 'publication3_form': publication3_form,
328 'publication4_form': publication4_form,
f810842d 329 'these_form': these_form,
3eb00212 330 'groupe_form': groupe_form,
e8e9e4fd 331 #'formset' : formset
b3e1079e
AJ
332 }
333 return render_to_response ("chercheurs/edit.html", \
334 Context (variables),
335 context_instance = RequestContext(request))
336
337
0e9597af 338@login_required()
9af73c99 339def perso(request):
0d9d1c4d 340 """Espace chercheur (espace personnel du chercheur)"""
9af73c99
AJ
341 context_instance = RequestContext(request)
342 chercheur = context_instance['user_chercheur']
0d9d1c4d 343 if not chercheur:
909db6d4 344 return HttpResponseRedirect(reverse('chercheurs.views.chercheur_login'))
588d6b93 345 variables = { 'chercheur': chercheur,
346 }
347 return render_to_response ("chercheurs/perso.html", \
348 Context (variables),
349 context_instance = RequestContext(request))
da091176 350
351def retrieve(request, id):
352 """Fiche du chercheur"""
3eb00212 353 #chercheur = Chercheur.objects.get(id=id)
0e9597af 354 inscription = request.GET.get('inscription')
3eb00212 355 chercheur = get_object_or_404(Chercheur, id=id)
da091176 356 variables = { 'chercheur': chercheur,
0e9597af 357 'inscription': inscription,
da091176 358 }
359 return render_to_response ("chercheurs/retrieve.html", \
360 Context (variables),
361 context_instance = RequestContext(request))