Remplacé les champs Facebook et LinkedIn pour un champ "Réseau social" dans la
[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
72923b8e 17from django.contrib.auth import authenticate, login
9af73c99 18from django.contrib.auth.decorators import login_required
72923b8e 19from django.contrib.auth.forms import AuthenticationForm as OriginalAuthenticationForm
13146d99 20
510b5321 21from django.db.models import Q
3eb00212 22from django.shortcuts import get_object_or_404
510b5321 23
c18af6bd 24from django.utils.translation import ugettext_lazy as _
c18af6bd 25
0e9597af
AJ
26#TODO: Migrer tout ce qui a rapport aux users dans une nouvelle app
27
c18af6bd 28class AuthenticationForm(OriginalAuthenticationForm):
b5af3133 29 username = forms.CharField(label='Adresse électronique', max_length=255)
c18af6bd 30
0e9597af
AJ
31def send_password(request):
32 if request.method == "POST":
33 form = SendPasswordForm(data=request.POST)
34 if form.is_valid():
35 u = Utilisateur.objects.get(courriel=form.cleaned_data['email'])
36 code = hashlib.md5(u.courriel+u.password).hexdigest()
37 code = code[0:6]
38 link = "%saccounts/new_password/%s/%s/" % (settings.SITE_ROOT_URL, u.courriel, code)
e427f068
AJ
39
40 variables = { 'user': u,
41 'link': link,
206989d4
AJ
42 'SITE_ROOT_URL': settings.SITE_ROOT_URL,
43 'CONTACT_EMAIL': settings.CONTACT_EMAIL,
e427f068
AJ
44 }
45 t = get_template('accounts/email_password.html')
46 content = t.render(Context(variables))
47
0e9597af 48 send_mail('Savoirs en partage: changement de mot de passe',
e427f068 49 content, settings.CONTACT_EMAIL,
0e9597af
AJ
50 [u.courriel], fail_silently=False)
51 else:
52 form = SendPasswordForm()
53
54 variables = { 'form': form,
55 }
56 return render_to_response ("accounts/send_password.html", \
57 Context (variables),
58 context_instance = RequestContext(request))
e427f068 59
0e9597af
AJ
60def new_password(request, email, code):
61 u = Utilisateur.objects.get(courriel=email)
62 original_code = hashlib.md5(u.courriel+u.password).hexdigest()
63 original_code = original_code[0:6]
e427f068 64 message=""
0e9597af 65 if(code == original_code):
e427f068
AJ
66 if request.method == "POST":
67 form = NewPasswordForm(data=request.POST)
68 if form.is_valid():
69 new_password = form.cleaned_data['password']
70 u.password = hashlib.md5(new_password).hexdigest()
71 u.save()
72 message = "Votre mot de passe a été modifié."
73 else:
74 form = NewPasswordForm()
0e9597af
AJ
75 else:
76 return HttpResponseRedirect('/')
e427f068
AJ
77 variables = { 'form': form,
78 'message': message,
0e9597af
AJ
79 }
80 return render_to_response ("accounts/new_password.html", \
81 Context (variables),
82 context_instance = RequestContext(request))
c18af6bd 83
e427f068
AJ
84@login_required()
85def change_password(request):
86 context_instance = RequestContext(request)
87 u = context_instance['user_sep']
88 message = ""
89 if request.method == "POST":
90 form = NewPasswordForm(data=request.POST)
91 if form.is_valid():
92 new_password = form.cleaned_data['password']
93 u.password = hashlib.md5(new_password).hexdigest()
94 u.save()
95 message = "Votre mot de passe a été modifié."
96 else:
97 form = NewPasswordForm()
98 variables = { 'form': form,
99 'message': message,
100 }
101 return render_to_response ("accounts/new_password.html", \
102 Context (variables),
103 context_instance = RequestContext(request))
104
51515982 105def chercheur_login(request):
c18af6bd 106 "Displays the login form and handles the login action."
c18af6bd
AJ
107 if request.method == "POST":
108 form = AuthenticationForm(data=request.POST)
109 if form.is_valid():
c18af6bd
AJ
110 from django.contrib.auth import login
111 login(request, form.get_user())
112 if request.session.test_cookie_worked():
113 request.session.delete_test_cookie()
51515982 114 return HttpResponseRedirect(url('chercheurs.views.perso'))
c18af6bd
AJ
115 else:
116 form = AuthenticationForm(request)
117 request.session.set_test_cookie()
51515982 118 return render_to_response('accounts/login.html', dict(form=form), context_instance=RequestContext(request))
c18af6bd
AJ
119
120
f8c16b3d 121def index(request):
122 """Répertoire des chercheurs"""
3efbacbe
EMS
123 search_form = RepertoireSearchForm(request.GET)
124 chercheurs = search_form.get_query_set()
9f7c169e 125 nb_chercheurs = chercheurs.count()
3efbacbe
EMS
126 return render_to_response("chercheurs/index.html",
127 dict(chercheurs=chercheurs, nb_chercheurs=nb_chercheurs, repertoire_form=search_form),
128 context_instance=RequestContext(request))
588d6b93 129
932eef9a
AJ
130def inscription(request):
131 if request.method == 'POST':
132 personne_form = PersonneForm (request.POST, prefix="personne")
133 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
7c596de2 134 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
00755d9b 135 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
7c596de2 136 discipline_form = DisciplineForm (request.POST, prefix="discipline")
00755d9b
AJ
137 publication1_form = PublicationForm (request.POST, prefix="publication1")
138 publication2_form = PublicationForm (request.POST, prefix="publication2")
139 publication3_form = PublicationForm (request.POST, prefix="publication3")
f810842d
AJ
140 publication4_form = PublicationForm (request.POST, prefix="publication4")
141 these_form = TheseForm(request.POST, prefix="these")
5b9abc81 142 expertise_form = ExpertiseForm(request.POST, prefix="expertise")
3eb00212 143 groupe_form = GroupeForm(request.POST, prefix="groupe")
7c596de2 144
932eef9a 145 if personne_form.is_valid():
3eb00212 146 if chercheur_form.is_valid() and groupe_form.is_valid():
932eef9a 147 c = chercheur_form.save(commit=False)
7c596de2
AJ
148
149 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
ddf6d787 150 etablissement_autre_form = EtablissementAutreForm (request.POST, prefix="etablissement_autre", instance=c)
7c596de2
AJ
151 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
152
f810842d 153 if etablissement_form.is_valid() and discipline_form.is_valid() and these_form.is_valid():
6befc7c9 154 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
00755d9b
AJ
155 pub = publication1_form.save()
156 c.publication1 = pub
6befc7c9 157 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
00755d9b
AJ
158 pub = publication2_form.save()
159 c.publication2 = pub
6befc7c9 160 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
00755d9b
AJ
161 pub = publication3_form.save()
162 c.publication3 = pub
6befc7c9 163 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
00755d9b
AJ
164 pub = publication4_form.save()
165 c.publication4 = pub
f810842d 166 these = these_form.save()
8ea92a0d
AJ
167 if expertise_form.is_valid() and expertise_form.cleaned_data['nom']:
168 expertise = expertise_form.save()
a4e383ac 169 c.expertise = expertise
f810842d 170 c.these = these
00755d9b
AJ
171 etablissement_form.save(commit=False)
172 etablissement_autre_form.save(commit=False)
7c596de2 173 discipline_form.save(commit=False)
5ecd9e43
AJ
174 #encodage du mot de passe de l'utilisateur (refactorer car c'est pas clean
175 #et c'est pas la bonne place pour faire ca - AJ
176 personne_form.cleaned_data['password'] = hashlib.md5(personne_form.cleaned_data['password']).hexdigest()
7c596de2
AJ
177 p = personne_form.save()
178 c.personne = p
179 c.save()
3eb00212
AJ
180
181 #sauvegarde des groupes
182 groupes = request.POST.getlist('groupe-groupes')
183 for g in groupes:
184 g = Groupe.objects.get(pk=g)
185 ChercheurGroupe.objects.get_or_create(chercheur=c, groupe=g, actif=1)
72923b8e
EMS
186
187 # login automatique
188 login(request, authenticate(username=personne_form.cleaned_data['courriel'],
189 password=personne_form.cleaned_data['password']))
190 return HttpResponseRedirect(url('chercheurs.views.perso'))
932eef9a
AJ
191 else:
192 personne_form = PersonneForm(prefix="personne")
193 chercheur_form = ChercheurForm(prefix="chercheur")
7c596de2 194 etablissement_form = EtablissementForm(prefix="etablissement")
00755d9b 195 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
7c596de2 196 discipline_form = DisciplineForm(prefix="discipline")
00755d9b
AJ
197 publication1_form = PublicationForm(prefix="publication1")
198 publication2_form = PublicationForm(prefix="publication2")
199 publication3_form = PublicationForm(prefix="publication3")
f810842d
AJ
200 publication4_form = PublicationForm(prefix="publication4")
201 these_form = TheseForm(prefix="these")
5b9abc81 202 expertise_form = ExpertiseForm(prefix="expertise")
3eb00212 203 groupe_form = GroupeForm(prefix="groupe")
932eef9a
AJ
204
205 variables = { 'personne_form': personne_form,
206 'chercheur_form': chercheur_form,
7c596de2
AJ
207 'etablissement_form': etablissement_form,
208 'discipline_form': discipline_form,
00755d9b
AJ
209 'etablissement_autre_form': etablissement_autre_form,
210 'publication1_form': publication1_form,
211 'publication2_form': publication2_form,
212 'publication3_form': publication3_form,
213 'publication4_form': publication4_form,
f810842d 214 'these_form': these_form,
5b9abc81 215 'expertise_form': expertise_form,
3eb00212 216 'groupe_form': groupe_form,
932eef9a
AJ
217 }
218
219 return render_to_response ("chercheurs/inscription.html", \
220 Context (variables),
221 context_instance = RequestContext(request))
9af73c99 222
0e9597af 223@login_required()
b3e1079e
AJ
224def edit(request):
225 """Edition d'un chercheur"""
226 context_instance = RequestContext(request)
227 chercheur = context_instance['user_chercheur']
e8e9e4fd
AJ
228 #GroupeFormset = inlineformset_factory(Chercheur, ChercheurGroupe)
229
b3e1079e 230 if request.method == 'POST':
a955bdb8 231 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
e8e9e4fd 232 chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
a955bdb8
AJ
233 etablissement_form = EtablissementForm(request.POST, prefix="etablissement", instance=chercheur)
234 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre", instance=chercheur)
235 discipline_form = DisciplineForm(request.POST, prefix="discipline", instance=chercheur)
236 publication1_form = PublicationForm(request.POST, prefix="publication1", instance=chercheur.publication1)
237 publication2_form = PublicationForm(request.POST, prefix="publication2", instance=chercheur.publication2)
238 publication3_form = PublicationForm(request.POST, prefix="publication3", instance=chercheur.publication3)
239 publication4_form = PublicationForm(request.POST, prefix="publication4", instance=chercheur.publication4)
f810842d 240 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
5b9abc81 241 expertise_form = ExpertiseForm(request.POST, prefix="expertise", instance=chercheur.expertise)
3eb00212 242 groupe_form = GroupeForm(request.POST, prefix="groupe", instance=chercheur)
a955bdb8 243
e8e9e4fd
AJ
244 #formset = GroupeFormset(request.POST, prefix="groupes", instance = chercheur)
245
ddf6d787 246 if( personne_form.is_valid() and discipline_form.is_valid() and chercheur_form.is_valid() and these_form.is_valid()
5b9abc81 247 and etablissement_form.is_valid() and etablissement_autre_form.save() and groupe_form.is_valid() and expertise_form.is_valid() ):
a955bdb8 248 personne_form.save()
a955bdb8 249 discipline_form.save()
3eb00212 250 chercheur_form.save()
ddf6d787
AJ
251 etablissement_form.save()
252 etablissement_autre_form.save()
253
e8e9e4fd
AJ
254 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
255 chercheur.publication1 = publication1_form.save()
256 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
257 chercheur.publication2 = publication2_form.save()
258 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
259 chercheur.publication3 = publication3_form.save()
260 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
261 chercheur.publication4 = publication4_form.save()
5b9abc81 262 chercheur.these = these_form.save()
8ea92a0d
AJ
263 if expertise_form.cleaned_data['nom']:
264 chercheur.expertise = expertise_form.save()
e8e9e4fd
AJ
265 chercheur.save()
266 #Gestion des groupes
3eb00212 267 groupes = request.POST.getlist('groupe-groupes')
e8e9e4fd
AJ
268 #On delete les chercheurs deselectionnés
269 ChercheurGroupe.objects.filter(chercheur=chercheur).exclude(groupe__in=groupes).delete()
270 #Sauvegarde des groupes...
271 for g in groupes:
272 g = Groupe.objects.get(pk=g)
273 ChercheurGroupe.objects.get_or_create(chercheur=chercheur, groupe=g, actif=1)
2a36714f 274 return HttpResponseRedirect("/chercheurs/perso/?modification=1")
e8e9e4fd
AJ
275
276 #formset.save()
277
b3e1079e 278 else:
a955bdb8 279 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
e8e9e4fd 280 chercheur_form = ChercheurForm (prefix="chercheur", instance=chercheur)
a955bdb8
AJ
281 etablissement_form = EtablissementForm(prefix="etablissement", instance=chercheur)
282 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre", instance=chercheur)
283 discipline_form = DisciplineForm(prefix="discipline", instance=chercheur)
284 publication1_form = PublicationForm(prefix="publication1", instance=chercheur.publication1)
285 publication2_form = PublicationForm(prefix="publication2", instance=chercheur.publication2)
286 publication3_form = PublicationForm(prefix="publication3", instance=chercheur.publication3)
f810842d
AJ
287 publication4_form = PublicationForm(prefix="publication4", instance=chercheur.publication4)
288 these_form = TheseForm(prefix="these", instance=chercheur.these)
5b9abc81 289 expertise_form = ExpertiseForm(prefix="expertise", instance=chercheur.expertise)
3eb00212 290 groupe_form = GroupeForm(prefix="groupe", instance=chercheur)
e8e9e4fd 291 #formset = GroupeFormset(prefix="groupes", instance = chercheur)
b3e1079e
AJ
292
293 variables = { 'chercheur': chercheur,
294 'personne_form':personne_form,
e8e9e4fd 295 'chercheur_form': chercheur_form,
a955bdb8
AJ
296 'etablissement_form': etablissement_form,
297 'discipline_form': discipline_form,
298 'etablissement_autre_form': etablissement_autre_form,
299 'publication1_form': publication1_form,
300 'publication2_form': publication2_form,
301 'publication3_form': publication3_form,
302 'publication4_form': publication4_form,
f810842d 303 'these_form': these_form,
5b9abc81 304 'expertise_form': expertise_form,
3eb00212 305 'groupe_form': groupe_form,
e8e9e4fd 306 #'formset' : formset
b3e1079e
AJ
307 }
308 return render_to_response ("chercheurs/edit.html", \
309 Context (variables),
310 context_instance = RequestContext(request))
311
312
0e9597af 313@login_required()
9af73c99 314def perso(request):
0d9d1c4d 315 """Espace chercheur (espace personnel du chercheur)"""
9af73c99
AJ
316 context_instance = RequestContext(request)
317 chercheur = context_instance['user_chercheur']
2a36714f 318 modification = request.GET.get('modification')
0d9d1c4d 319 if not chercheur:
51515982 320 return HttpResponseRedirect(url('chercheurs.views.chercheur_login'))
588d6b93 321 variables = { 'chercheur': chercheur,
2a36714f 322 'modification': modification,
588d6b93 323 }
324 return render_to_response ("chercheurs/perso.html", \
325 Context (variables),
326 context_instance = RequestContext(request))
da091176 327
328def retrieve(request, id):
329 """Fiche du chercheur"""
3eb00212 330 chercheur = get_object_or_404(Chercheur, id=id)
72923b8e 331 variables = { 'chercheur': chercheur }
da091176 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))