Ajout des chercheurs à la recherche transversale.
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / views.py
1 # -*- encoding: utf-8 -*-
2 import hashlib
3 from django.shortcuts import render_to_response
4 from django.http import HttpResponseRedirect, HttpResponse
5 from django.template import Context, RequestContext
6 from django.template.loader import get_template
7 from django.core.urlresolvers import reverse
8 from django.core.mail import send_mail
9 from django.conf import settings
10
11 from forms import *
12 from django.forms.models import inlineformset_factory
13
14 from auf_references_client.models import Discipline, TypeImplantation
15 from models import Personne, Utilisateur, Groupe, ChercheurGroupe
16
17 from django.contrib.auth.decorators import login_required
18
19 from django.db.models import Q
20 from django.shortcuts import get_object_or_404
21
22 from django.utils.translation import ugettext_lazy as _
23 from django.contrib.auth.forms import AuthenticationForm as OriginalAuthenticationForm
24
25 #TODO: Migrer tout ce qui a rapport aux users dans une nouvelle app
26
27 class AuthenticationForm(OriginalAuthenticationForm):
28 username = forms.CharField(label=_("Username"), max_length=255)
29
30 def 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)
38
39 variables = { 'user': u,
40 'link': link,
41 'SITE_ROOT_URL': settings.SITE_ROOT_URL,
42 'CONTACT_EMAIL': settings.CONTACT_EMAIL,
43 }
44 t = get_template('accounts/email_password.html')
45 content = t.render(Context(variables))
46
47 send_mail('Savoirs en partage: changement de mot de passe',
48 content, settings.CONTACT_EMAIL,
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))
58
59 def 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]
63 message=""
64 if(code == original_code):
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()
74 else:
75 return HttpResponseRedirect('/')
76 variables = { 'form': form,
77 'message': message,
78 }
79 return render_to_response ("accounts/new_password.html", \
80 Context (variables),
81 context_instance = RequestContext(request))
82
83 @login_required()
84 def 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
104 def 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
127 def 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:
135 list = list.filter(Q(etablissement__pays = pays.pk) | Q(etablissement_autre_pays = pays.pk))
136 fonction = simpleForm.cleaned_data["fonction"]
137 if fonction:
138 list = list.filter(fonction = fonction)
139 discipline = simpleForm.cleaned_data["discipline"]
140 if discipline:
141 list = list.filter(discipline=discipline)
142 domaine = simpleForm.cleaned_data["domaine"]
143 if domaine:
144 list = list.filter(groupes=domaine)
145 mots_cles = simpleForm.cleaned_data["mots_cles"]
146 if mots_cles:
147 list = list.search(mots_cles)
148 return list
149
150 def index(request):
151 """Répertoire des chercheurs"""
152
153 chercheurs = chercheur_queryset (request)
154 repertoire_form = RepertoireSearchForm (request.GET)
155
156 nb_chercheurs = chercheurs.count()
157 variables = { 'chercheurs': chercheurs,
158 'nb_chercheurs': nb_chercheurs,
159 'repertoire_form': repertoire_form,
160 }
161 return render_to_response ("chercheurs/index.html", \
162 Context (variables),
163 context_instance = RequestContext(request))
164
165 def inscription(request):
166 if request.method == 'POST':
167 personne_form = PersonneForm (request.POST, prefix="personne")
168 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
169 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
170 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
171 discipline_form = DisciplineForm (request.POST, prefix="discipline")
172 publication1_form = PublicationForm (request.POST, prefix="publication1")
173 publication2_form = PublicationForm (request.POST, prefix="publication2")
174 publication3_form = PublicationForm (request.POST, prefix="publication3")
175 publication4_form = PublicationForm (request.POST, prefix="publication4")
176 these_form = TheseForm(request.POST, prefix="these")
177 groupe_form = GroupeForm(request.POST, prefix="groupe")
178
179 if personne_form.is_valid():
180 if chercheur_form.is_valid() and groupe_form.is_valid():
181 c = chercheur_form.save(commit=False)
182
183 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
184 etablissement_autre_form = EtablissementAutreForm (request.POST, prefix="etablissement_autre", instance=c)
185 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
186
187 if etablissement_form.is_valid() and discipline_form.is_valid() and these_form.is_valid():
188 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
189 pub = publication1_form.save()
190 c.publication1 = pub
191 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
192 pub = publication2_form.save()
193 c.publication2 = pub
194 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
195 pub = publication3_form.save()
196 c.publication3 = pub
197 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
198 pub = publication4_form.save()
199 c.publication4 = pub
200 these = these_form.save()
201 c.these = these
202 etablissement_form.save(commit=False)
203 etablissement_autre_form.save(commit=False)
204 discipline_form.save(commit=False)
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()
208 p = personne_form.save()
209 c.personne = p
210 c.save()
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)
217 return HttpResponseRedirect("/chercheurs/%d/?inscription=1" % c.id)
218 #return HttpResponseRedirect(reverse('chercheurs.views.retrieve', args=(c.id,)))
219 else:
220 personne_form = PersonneForm(prefix="personne")
221 chercheur_form = ChercheurForm(prefix="chercheur")
222 etablissement_form = EtablissementForm(prefix="etablissement")
223 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
224 discipline_form = DisciplineForm(prefix="discipline")
225 publication1_form = PublicationForm(prefix="publication1")
226 publication2_form = PublicationForm(prefix="publication2")
227 publication3_form = PublicationForm(prefix="publication3")
228 publication4_form = PublicationForm(prefix="publication4")
229 these_form = TheseForm(prefix="these")
230 groupe_form = GroupeForm(prefix="groupe")
231
232 variables = { 'personne_form': personne_form,
233 'chercheur_form': chercheur_form,
234 'etablissement_form': etablissement_form,
235 'discipline_form': discipline_form,
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,
241 'these_form': these_form,
242 'groupe_form': groupe_form,
243 }
244
245 return render_to_response ("chercheurs/inscription.html", \
246 Context (variables),
247 context_instance = RequestContext(request))
248
249 @login_required()
250 def edit(request):
251 """Edition d'un chercheur"""
252 context_instance = RequestContext(request)
253 chercheur = context_instance['user_chercheur']
254 #GroupeFormset = inlineformset_factory(Chercheur, ChercheurGroupe)
255
256 if request.method == 'POST':
257 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
258 chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
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)
266 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
267 groupe_form = GroupeForm(request.POST, prefix="groupe", instance=chercheur)
268
269 #formset = GroupeFormset(request.POST, prefix="groupes", instance = chercheur)
270
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() ):
273 personne_form.save()
274 discipline_form.save()
275 chercheur_form.save()
276 etablissement_form.save()
277 etablissement_autre_form.save()
278
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
290 groupes = request.POST.getlist('groupe-groupes')
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)
297 return HttpResponseRedirect("/chercheurs/%d/?inscription=1" % chercheur.id)
298
299 #formset.save()
300
301 else:
302 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
303 chercheur_form = ChercheurForm (prefix="chercheur", instance=chercheur)
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)
310 publication4_form = PublicationForm(prefix="publication4", instance=chercheur.publication4)
311 these_form = TheseForm(prefix="these", instance=chercheur.these)
312 groupe_form = GroupeForm(prefix="groupe", instance=chercheur)
313 #formset = GroupeFormset(prefix="groupes", instance = chercheur)
314
315 variables = { 'chercheur': chercheur,
316 'personne_form':personne_form,
317 'chercheur_form': chercheur_form,
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,
325 'these_form': these_form,
326 'groupe_form': groupe_form,
327 #'formset' : formset
328 }
329 return render_to_response ("chercheurs/edit.html", \
330 Context (variables),
331 context_instance = RequestContext(request))
332
333
334 @login_required()
335 def perso(request):
336 """Espace chercheur (espace personnel du chercheur)"""
337 context_instance = RequestContext(request)
338 chercheur = context_instance['user_chercheur']
339 if not chercheur:
340 return HttpResponseRedirect(reverse('chercheurs.views.chercheur_login'))
341 variables = { 'chercheur': chercheur,
342 }
343 return render_to_response ("chercheurs/perso.html", \
344 Context (variables),
345 context_instance = RequestContext(request))
346
347 def retrieve(request, id):
348 """Fiche du chercheur"""
349 #chercheur = Chercheur.objects.get(id=id)
350 inscription = request.GET.get('inscription')
351 chercheur = get_object_or_404(Chercheur, id=id)
352 variables = { 'chercheur': chercheur,
353 'inscription': inscription,
354 }
355 return render_to_response ("chercheurs/retrieve.html", \
356 Context (variables),
357 context_instance = RequestContext(request))