Ajout d'un sous-menu dans les briques qui en ont un. (#569)
[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
5ecd9e43 6from django.core.urlresolvers import reverse
0e9597af
AJ
7from django.core.mail import send_mail
8from django.conf import settings
e8e9e4fd 9
932eef9a 10from forms import *
e8e9e4fd 11from django.forms.models import inlineformset_factory
932eef9a
AJ
12
13from auf_references_client.models import Discipline, TypeImplantation
e8e9e4fd 14from models import Personne, Utilisateur, Groupe, ChercheurGroupe
932eef9a 15
9af73c99 16from django.contrib.auth.decorators import login_required
13146d99 17
510b5321 18from django.db.models import Q
3eb00212 19from django.shortcuts import get_object_or_404
510b5321 20
c18af6bd
AJ
21from django.utils.translation import ugettext_lazy as _
22from django.contrib.auth.forms import AuthenticationForm as OriginalAuthenticationForm
23
0e9597af
AJ
24#TODO: Migrer tout ce qui a rapport aux users dans une nouvelle app
25
c18af6bd
AJ
26class AuthenticationForm(OriginalAuthenticationForm):
27 username = forms.CharField(label=_("Username"), max_length=255)
28
0e9597af
AJ
29def send_password(request):
30 if request.method == "POST":
31 form = SendPasswordForm(data=request.POST)
32 if form.is_valid():
33 u = Utilisateur.objects.get(courriel=form.cleaned_data['email'])
34 code = hashlib.md5(u.courriel+u.password).hexdigest()
35 code = code[0:6]
36 link = "%saccounts/new_password/%s/%s/" % (settings.SITE_ROOT_URL, u.courriel, code)
37 send_mail('Savoirs en partage: changement de mot de passe',
38 'Bonjour \n. Veuillez acceder a ce lien pour modifier votre mot de passe'+link, settings.CONTACT_EMAIL,
39 [u.courriel], fail_silently=False)
40 else:
41 form = SendPasswordForm()
42
43 variables = { 'form': form,
44 }
45 return render_to_response ("accounts/send_password.html", \
46 Context (variables),
47 context_instance = RequestContext(request))
48
49def random_password():
50 import string
51 from random import choice
52 chars = string.ascii_letters + string.digits
53 password = "".join(choice(chars) for x in range(0,8))
54 return password
55
56
57def new_password(request, email, code):
58 u = Utilisateur.objects.get(courriel=email)
59 original_code = hashlib.md5(u.courriel+u.password).hexdigest()
60 original_code = original_code[0:6]
61 if(code == original_code):
62 new_password = random_password()
63 u.password = hashlib.md5(new_password).hexdigest()
64 u.save()
65 else:
66 return HttpResponseRedirect('/')
67 variables = { 'new_password': new_password,
68 }
69 return render_to_response ("accounts/new_password.html", \
70 Context (variables),
71 context_instance = RequestContext(request))
72
73
c18af6bd
AJ
74
75def chercheur_login(request, template_name='registration/login.html', redirect_field_name='next'):
76 "Displays the login form and handles the login action."
77 redirect_to = request.REQUEST.get(redirect_field_name, '')
78 if request.method == "POST":
79 form = AuthenticationForm(data=request.POST)
80 if form.is_valid():
81 # Light security check -- make sure redirect_to isn't garbage.
82 if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
83 redirect_to = settings.LOGIN_REDIRECT_URL
84 from django.contrib.auth import login
85 login(request, form.get_user())
86 if request.session.test_cookie_worked():
87 request.session.delete_test_cookie()
88 return HttpResponseRedirect(redirect_to)
89 else:
90 form = AuthenticationForm(request)
91 request.session.set_test_cookie()
92 return render_to_response(template_name, {
93 'form': form,
94 redirect_field_name: redirect_to,
95 }, context_instance=RequestContext(request))
96
97
13146d99
AJ
98def chercheur_queryset (request):
99 list = Chercheur.objects.order_by("id")
100 pays = ""
101
102 simpleForm = RepertoireSearchForm (request.GET)
103 if simpleForm.is_valid ():
104 pays = simpleForm.cleaned_data["pays"]
105 if pays:
54ab837c 106 list = list.filter(Q(etablissement__pays = pays.pk) | Q(etablissement_autre_pays = pays.pk))
13146d99
AJ
107 fonction = simpleForm.cleaned_data["fonction"]
108 if fonction:
ee46b1a2 109 list = list.filter(fonction = fonction)
13146d99
AJ
110 discipline = simpleForm.cleaned_data["discipline"]
111 if discipline:
ee46b1a2
AJ
112 list = list.filter(discipline=discipline)
113 domaine = simpleForm.cleaned_data["domaine"]
114 if domaine:
115 list = list.filter(groupes=domaine)
510b5321
AJ
116 mots_cles = simpleForm.cleaned_data["mots_cles"]
117 if mots_cles:
54ab837c
AJ
118 list = list.filter( Q(personne__nom__search=mots_cles)
119 | Q(personne__prenom__search=mots_cles)
120 | Q(expertise__search=mots_cles)
121 | Q(etablissement_autre_nom__search=mots_cles)
122 | Q(etablissement__nom__search=mots_cles) )
13146d99
AJ
123 return list
124
f8c16b3d 125def index(request):
126 """Répertoire des chercheurs"""
13146d99
AJ
127
128 chercheurs = chercheur_queryset (request)
129 repertoire_form = RepertoireSearchForm (request.GET)
130
9f7c169e 131 nb_chercheurs = chercheurs.count()
588d6b93 132 variables = { 'chercheurs': chercheurs,
9f7c169e 133 'nb_chercheurs': nb_chercheurs,
13146d99 134 'repertoire_form': repertoire_form,
588d6b93 135 }
03bce417 136 return render_to_response ("chercheurs/index.html", \
588d6b93 137 Context (variables),
138 context_instance = RequestContext(request))
139
932eef9a
AJ
140def inscription(request):
141 if request.method == 'POST':
142 personne_form = PersonneForm (request.POST, prefix="personne")
143 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
7c596de2 144 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
00755d9b 145 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
7c596de2 146 discipline_form = DisciplineForm (request.POST, prefix="discipline")
00755d9b
AJ
147 publication1_form = PublicationForm (request.POST, prefix="publication1")
148 publication2_form = PublicationForm (request.POST, prefix="publication2")
149 publication3_form = PublicationForm (request.POST, prefix="publication3")
f810842d
AJ
150 publication4_form = PublicationForm (request.POST, prefix="publication4")
151 these_form = TheseForm(request.POST, prefix="these")
3eb00212 152 groupe_form = GroupeForm(request.POST, prefix="groupe")
7c596de2 153
932eef9a 154 if personne_form.is_valid():
3eb00212 155 if chercheur_form.is_valid() and groupe_form.is_valid():
932eef9a 156 c = chercheur_form.save(commit=False)
7c596de2
AJ
157
158 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
ddf6d787 159 etablissement_autre_form = EtablissementAutreForm (request.POST, prefix="etablissement_autre", instance=c)
7c596de2
AJ
160 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
161
f810842d 162 if etablissement_form.is_valid() and discipline_form.is_valid() and these_form.is_valid():
6befc7c9 163 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
00755d9b
AJ
164 pub = publication1_form.save()
165 c.publication1 = pub
6befc7c9 166 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
00755d9b
AJ
167 pub = publication2_form.save()
168 c.publication2 = pub
6befc7c9 169 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
00755d9b
AJ
170 pub = publication3_form.save()
171 c.publication3 = pub
6befc7c9 172 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
00755d9b
AJ
173 pub = publication4_form.save()
174 c.publication4 = pub
f810842d
AJ
175 these = these_form.save()
176 c.these = these
00755d9b
AJ
177 etablissement_form.save(commit=False)
178 etablissement_autre_form.save(commit=False)
7c596de2 179 discipline_form.save(commit=False)
5ecd9e43
AJ
180 #encodage du mot de passe de l'utilisateur (refactorer car c'est pas clean
181 #et c'est pas la bonne place pour faire ca - AJ
182 personne_form.cleaned_data['password'] = hashlib.md5(personne_form.cleaned_data['password']).hexdigest()
7c596de2
AJ
183 p = personne_form.save()
184 c.personne = p
185 c.save()
3eb00212
AJ
186
187 #sauvegarde des groupes
188 groupes = request.POST.getlist('groupe-groupes')
189 for g in groupes:
190 g = Groupe.objects.get(pk=g)
191 ChercheurGroupe.objects.get_or_create(chercheur=c, groupe=g, actif=1)
0e9597af
AJ
192 return HttpResponseRedirect("/chercheurs/%d/?inscription=1" % c.id)
193 #return HttpResponseRedirect(reverse('chercheurs.views.retrieve', args=(c.id,)))
932eef9a
AJ
194 else:
195 personne_form = PersonneForm(prefix="personne")
196 chercheur_form = ChercheurForm(prefix="chercheur")
7c596de2 197 etablissement_form = EtablissementForm(prefix="etablissement")
00755d9b 198 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
7c596de2 199 discipline_form = DisciplineForm(prefix="discipline")
00755d9b
AJ
200 publication1_form = PublicationForm(prefix="publication1")
201 publication2_form = PublicationForm(prefix="publication2")
202 publication3_form = PublicationForm(prefix="publication3")
f810842d
AJ
203 publication4_form = PublicationForm(prefix="publication4")
204 these_form = TheseForm(prefix="these")
3eb00212 205 groupe_form = GroupeForm(prefix="groupe")
932eef9a
AJ
206
207 variables = { 'personne_form': personne_form,
208 'chercheur_form': chercheur_form,
7c596de2
AJ
209 'etablissement_form': etablissement_form,
210 'discipline_form': discipline_form,
00755d9b
AJ
211 'etablissement_autre_form': etablissement_autre_form,
212 'publication1_form': publication1_form,
213 'publication2_form': publication2_form,
214 'publication3_form': publication3_form,
215 'publication4_form': publication4_form,
f810842d 216 'these_form': these_form,
3eb00212 217 'groupe_form': groupe_form,
932eef9a
AJ
218 }
219
220 return render_to_response ("chercheurs/inscription.html", \
221 Context (variables),
222 context_instance = RequestContext(request))
9af73c99 223
0e9597af 224@login_required()
b3e1079e
AJ
225def edit(request):
226 """Edition d'un chercheur"""
227 context_instance = RequestContext(request)
228 chercheur = context_instance['user_chercheur']
e8e9e4fd
AJ
229 #GroupeFormset = inlineformset_factory(Chercheur, ChercheurGroupe)
230
b3e1079e 231 if request.method == 'POST':
a955bdb8 232 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
e8e9e4fd 233 chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
a955bdb8
AJ
234 etablissement_form = EtablissementForm(request.POST, prefix="etablissement", instance=chercheur)
235 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre", instance=chercheur)
236 discipline_form = DisciplineForm(request.POST, prefix="discipline", instance=chercheur)
237 publication1_form = PublicationForm(request.POST, prefix="publication1", instance=chercheur.publication1)
238 publication2_form = PublicationForm(request.POST, prefix="publication2", instance=chercheur.publication2)
239 publication3_form = PublicationForm(request.POST, prefix="publication3", instance=chercheur.publication3)
240 publication4_form = PublicationForm(request.POST, prefix="publication4", instance=chercheur.publication4)
f810842d 241 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
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
AJ
246 if( personne_form.is_valid() and discipline_form.is_valid() and chercheur_form.is_valid() and these_form.is_valid()
247 and etablissement_form.is_valid() and etablissement_autre_form.save() and groupe_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()
262 chercheur.these = these_form.save()
263 chercheur.save()
264 #Gestion des groupes
3eb00212 265 groupes = request.POST.getlist('groupe-groupes')
e8e9e4fd
AJ
266 #On delete les chercheurs deselectionnés
267 ChercheurGroupe.objects.filter(chercheur=chercheur).exclude(groupe__in=groupes).delete()
268 #Sauvegarde des groupes...
269 for g in groupes:
270 g = Groupe.objects.get(pk=g)
271 ChercheurGroupe.objects.get_or_create(chercheur=chercheur, groupe=g, actif=1)
272
273
274 #formset.save()
275
b3e1079e 276 else:
a955bdb8 277 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
e8e9e4fd 278 chercheur_form = ChercheurForm (prefix="chercheur", instance=chercheur)
a955bdb8
AJ
279 etablissement_form = EtablissementForm(prefix="etablissement", instance=chercheur)
280 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre", instance=chercheur)
281 discipline_form = DisciplineForm(prefix="discipline", instance=chercheur)
282 publication1_form = PublicationForm(prefix="publication1", instance=chercheur.publication1)
283 publication2_form = PublicationForm(prefix="publication2", instance=chercheur.publication2)
284 publication3_form = PublicationForm(prefix="publication3", instance=chercheur.publication3)
f810842d
AJ
285 publication4_form = PublicationForm(prefix="publication4", instance=chercheur.publication4)
286 these_form = TheseForm(prefix="these", instance=chercheur.these)
3eb00212 287 groupe_form = GroupeForm(prefix="groupe", instance=chercheur)
e8e9e4fd 288 #formset = GroupeFormset(prefix="groupes", instance = chercheur)
b3e1079e
AJ
289
290 variables = { 'chercheur': chercheur,
291 'personne_form':personne_form,
e8e9e4fd 292 'chercheur_form': chercheur_form,
a955bdb8
AJ
293 'etablissement_form': etablissement_form,
294 'discipline_form': discipline_form,
295 'etablissement_autre_form': etablissement_autre_form,
296 'publication1_form': publication1_form,
297 'publication2_form': publication2_form,
298 'publication3_form': publication3_form,
299 'publication4_form': publication4_form,
f810842d 300 'these_form': these_form,
3eb00212 301 'groupe_form': groupe_form,
e8e9e4fd 302 #'formset' : formset
b3e1079e
AJ
303 }
304 return render_to_response ("chercheurs/edit.html", \
305 Context (variables),
306 context_instance = RequestContext(request))
307
308
0e9597af 309@login_required()
9af73c99 310def perso(request):
0d9d1c4d 311 """Espace chercheur (espace personnel du chercheur)"""
9af73c99
AJ
312 context_instance = RequestContext(request)
313 chercheur = context_instance['user_chercheur']
0d9d1c4d 314 if not chercheur:
909db6d4 315 return HttpResponseRedirect(reverse('chercheurs.views.chercheur_login'))
588d6b93 316 variables = { 'chercheur': chercheur,
317 }
318 return render_to_response ("chercheurs/perso.html", \
319 Context (variables),
320 context_instance = RequestContext(request))
da091176 321
322def retrieve(request, id):
323 """Fiche du chercheur"""
3eb00212 324 #chercheur = Chercheur.objects.get(id=id)
0e9597af 325 inscription = request.GET.get('inscription')
3eb00212 326 chercheur = get_object_or_404(Chercheur, id=id)
da091176 327 variables = { 'chercheur': chercheur,
0e9597af 328 'inscription': inscription,
da091176 329 }
330 return render_to_response ("chercheurs/retrieve.html", \
331 Context (variables),
332 context_instance = RequestContext(request))