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