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