#628 : conversion.txt en .html
[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 discipline = simpleForm.cleaned_data["discipline"]
137 if discipline:
138 list = list.filter(discipline=discipline)
139 domaine = simpleForm.cleaned_data["domaine"]
140 if domaine:
141 list = list.filter(groupes=domaine)
142 mots_cles = simpleForm.cleaned_data["mots_cles"]
143 if mots_cles:
144 list = list.search(mots_cles)
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
153 return list
154
155 def index(request):
156 """Répertoire des chercheurs"""
157
158 chercheurs = chercheur_queryset (request)
159 repertoire_form = RepertoireSearchForm (request.GET)
160
161 nb_chercheurs = chercheurs.count()
162 variables = { 'chercheurs': chercheurs,
163 'nb_chercheurs': nb_chercheurs,
164 'repertoire_form': repertoire_form,
165 }
166 return render_to_response ("chercheurs/index.html", \
167 Context (variables),
168 context_instance = RequestContext(request))
169
170 def inscription(request):
171 if request.method == 'POST':
172 personne_form = PersonneForm (request.POST, prefix="personne")
173 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
174 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
175 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
176 discipline_form = DisciplineForm (request.POST, prefix="discipline")
177 publication1_form = PublicationForm (request.POST, prefix="publication1")
178 publication2_form = PublicationForm (request.POST, prefix="publication2")
179 publication3_form = PublicationForm (request.POST, prefix="publication3")
180 publication4_form = PublicationForm (request.POST, prefix="publication4")
181 these_form = TheseForm(request.POST, prefix="these")
182 expertise_form = ExpertiseForm(request.POST, prefix="expertise")
183 groupe_form = GroupeForm(request.POST, prefix="groupe")
184
185 if personne_form.is_valid():
186 if chercheur_form.is_valid() and groupe_form.is_valid():
187 c = chercheur_form.save(commit=False)
188
189 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
190 etablissement_autre_form = EtablissementAutreForm (request.POST, prefix="etablissement_autre", instance=c)
191 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
192
193 if etablissement_form.is_valid() and discipline_form.is_valid() and these_form.is_valid():
194 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
195 pub = publication1_form.save()
196 c.publication1 = pub
197 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
198 pub = publication2_form.save()
199 c.publication2 = pub
200 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
201 pub = publication3_form.save()
202 c.publication3 = pub
203 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
204 pub = publication4_form.save()
205 c.publication4 = pub
206 these = these_form.save()
207 if expertise_form.is_valid() and expertise_form.cleaned_data['nom']:
208 expertise = expertise_form.save()
209 c.these = these
210 c.expertise = expertise
211 etablissement_form.save(commit=False)
212 etablissement_autre_form.save(commit=False)
213 discipline_form.save(commit=False)
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()
217 p = personne_form.save()
218 c.personne = p
219 c.save()
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)
226 return HttpResponseRedirect("/chercheurs/%d/?inscription=1" % c.id)
227 #return HttpResponseRedirect(reverse('chercheurs.views.retrieve', args=(c.id,)))
228 else:
229 personne_form = PersonneForm(prefix="personne")
230 chercheur_form = ChercheurForm(prefix="chercheur")
231 etablissement_form = EtablissementForm(prefix="etablissement")
232 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
233 discipline_form = DisciplineForm(prefix="discipline")
234 publication1_form = PublicationForm(prefix="publication1")
235 publication2_form = PublicationForm(prefix="publication2")
236 publication3_form = PublicationForm(prefix="publication3")
237 publication4_form = PublicationForm(prefix="publication4")
238 these_form = TheseForm(prefix="these")
239 expertise_form = ExpertiseForm(prefix="expertise")
240 groupe_form = GroupeForm(prefix="groupe")
241
242 variables = { 'personne_form': personne_form,
243 'chercheur_form': chercheur_form,
244 'etablissement_form': etablissement_form,
245 'discipline_form': discipline_form,
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,
251 'these_form': these_form,
252 'expertise_form': expertise_form,
253 'groupe_form': groupe_form,
254 }
255
256 return render_to_response ("chercheurs/inscription.html", \
257 Context (variables),
258 context_instance = RequestContext(request))
259
260 @login_required()
261 def edit(request):
262 """Edition d'un chercheur"""
263 context_instance = RequestContext(request)
264 chercheur = context_instance['user_chercheur']
265 #GroupeFormset = inlineformset_factory(Chercheur, ChercheurGroupe)
266
267 if request.method == 'POST':
268 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
269 chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
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)
277 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
278 expertise_form = ExpertiseForm(request.POST, prefix="expertise", instance=chercheur.expertise)
279 groupe_form = GroupeForm(request.POST, prefix="groupe", instance=chercheur)
280
281 #formset = GroupeFormset(request.POST, prefix="groupes", instance = chercheur)
282
283 if( personne_form.is_valid() and discipline_form.is_valid() and chercheur_form.is_valid() and these_form.is_valid()
284 and etablissement_form.is_valid() and etablissement_autre_form.save() and groupe_form.is_valid() and expertise_form.is_valid() ):
285 personne_form.save()
286 discipline_form.save()
287 chercheur_form.save()
288 etablissement_form.save()
289 etablissement_autre_form.save()
290
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()
299 chercheur.these = these_form.save()
300 if expertise_form.cleaned_data['nom']:
301 chercheur.expertise = expertise_form.save()
302 chercheur.save()
303 #Gestion des groupes
304 groupes = request.POST.getlist('groupe-groupes')
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)
311 return HttpResponseRedirect("/chercheurs/perso/?modification=1")
312
313 #formset.save()
314
315 else:
316 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
317 chercheur_form = ChercheurForm (prefix="chercheur", instance=chercheur)
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)
324 publication4_form = PublicationForm(prefix="publication4", instance=chercheur.publication4)
325 these_form = TheseForm(prefix="these", instance=chercheur.these)
326 expertise_form = ExpertiseForm(prefix="expertise", instance=chercheur.expertise)
327 groupe_form = GroupeForm(prefix="groupe", instance=chercheur)
328 #formset = GroupeFormset(prefix="groupes", instance = chercheur)
329
330 variables = { 'chercheur': chercheur,
331 'personne_form':personne_form,
332 'chercheur_form': chercheur_form,
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,
340 'these_form': these_form,
341 'expertise_form': expertise_form,
342 'groupe_form': groupe_form,
343 #'formset' : formset
344 }
345 return render_to_response ("chercheurs/edit.html", \
346 Context (variables),
347 context_instance = RequestContext(request))
348
349
350 @login_required()
351 def perso(request):
352 """Espace chercheur (espace personnel du chercheur)"""
353 context_instance = RequestContext(request)
354 chercheur = context_instance['user_chercheur']
355 modification = request.GET.get('modification')
356 if not chercheur:
357 return HttpResponseRedirect(reverse('chercheurs.views.chercheur_login'))
358 variables = { 'chercheur': chercheur,
359 'modification': modification,
360 }
361 return render_to_response ("chercheurs/perso.html", \
362 Context (variables),
363 context_instance = RequestContext(request))
364
365 def retrieve(request, id):
366 """Fiche du chercheur"""
367 #chercheur = Chercheur.objects.get(id=id)
368 inscription = request.GET.get('inscription')
369 chercheur = get_object_or_404(Chercheur, id=id)
370 variables = { 'chercheur': chercheur,
371 'inscription': inscription,
372 }
373 return render_to_response ("chercheurs/retrieve.html", \
374 Context (variables),
375 context_instance = RequestContext(request))
376
377 def conversion(request):
378 return render_to_response ("chercheurs/conversion.html", \
379 Context (), \
380 context_instance = RequestContext(request))