Login form avec plus de 30 chars
[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
5ecd9e43 4from django.http import HttpResponseRedirect
932eef9a 5from django.template import Context, RequestContext
5ecd9e43 6from django.core.urlresolvers import reverse
932eef9a
AJ
7from forms import *
8
9from auf_references_client.models import Discipline, TypeImplantation
b3e1079e 10from models import Personne, Utilisateur
932eef9a 11
9af73c99 12from django.contrib.auth.decorators import login_required
13146d99 13
510b5321
AJ
14from django.db.models import Q
15
c18af6bd
AJ
16from django.utils.translation import ugettext_lazy as _
17from django.contrib.auth.forms import AuthenticationForm as OriginalAuthenticationForm
18
19class AuthenticationForm(OriginalAuthenticationForm):
20 username = forms.CharField(label=_("Username"), max_length=255)
21
22
23def chercheur_login(request, template_name='registration/login.html', redirect_field_name='next'):
24 "Displays the login form and handles the login action."
25 redirect_to = request.REQUEST.get(redirect_field_name, '')
26 if request.method == "POST":
27 form = AuthenticationForm(data=request.POST)
28 if form.is_valid():
29 # Light security check -- make sure redirect_to isn't garbage.
30 if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
31 redirect_to = settings.LOGIN_REDIRECT_URL
32 from django.contrib.auth import login
33 login(request, form.get_user())
34 if request.session.test_cookie_worked():
35 request.session.delete_test_cookie()
36 return HttpResponseRedirect(redirect_to)
37 else:
38 form = AuthenticationForm(request)
39 request.session.set_test_cookie()
40 return render_to_response(template_name, {
41 'form': form,
42 redirect_field_name: redirect_to,
43 }, context_instance=RequestContext(request))
44
45
13146d99
AJ
46def chercheur_queryset (request):
47 list = Chercheur.objects.order_by("id")
48 pays = ""
49
50 simpleForm = RepertoireSearchForm (request.GET)
51 if simpleForm.is_valid ():
52 pays = simpleForm.cleaned_data["pays"]
53 if pays:
6befc7c9 54 list = list.filter (nationalite = pays.pk)
13146d99
AJ
55 fonction = simpleForm.cleaned_data["fonction"]
56 if fonction:
57 list = list.filter (fonction = fonction)
58 genre = simpleForm.cleaned_data["genre"]
59 if genre:
60 list = list.filter (personne__genre=genre)
61 discipline = simpleForm.cleaned_data["discipline"]
62 if discipline:
63 list = list.filter (discipline=discipline)
510b5321
AJ
64 mots_cles = simpleForm.cleaned_data["mots_cles"]
65 if mots_cles:
66 list = list.filter (Q(personne__nom__icontains=mots_cles) | Q(personne__prenom__icontains=mots_cles))
13146d99
AJ
67 return list
68
f8c16b3d 69def index(request):
70 """Répertoire des chercheurs"""
13146d99
AJ
71
72 chercheurs = chercheur_queryset (request)
73 repertoire_form = RepertoireSearchForm (request.GET)
74
9f7c169e 75 nb_chercheurs = chercheurs.count()
588d6b93 76 variables = { 'chercheurs': chercheurs,
9f7c169e 77 'nb_chercheurs': nb_chercheurs,
13146d99 78 'repertoire_form': repertoire_form,
588d6b93 79 }
03bce417 80 return render_to_response ("chercheurs/index.html", \
588d6b93 81 Context (variables),
82 context_instance = RequestContext(request))
83
932eef9a
AJ
84def inscription(request):
85 if request.method == 'POST':
86 personne_form = PersonneForm (request.POST, prefix="personne")
87 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
7c596de2 88 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
00755d9b 89 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
7c596de2 90 discipline_form = DisciplineForm (request.POST, prefix="discipline")
00755d9b
AJ
91 publication1_form = PublicationForm (request.POST, prefix="publication1")
92 publication2_form = PublicationForm (request.POST, prefix="publication2")
93 publication3_form = PublicationForm (request.POST, prefix="publication3")
f810842d
AJ
94 publication4_form = PublicationForm (request.POST, prefix="publication4")
95 these_form = TheseForm(request.POST, prefix="these")
7c596de2 96
932eef9a
AJ
97 if personne_form.is_valid():
98 if chercheur_form.is_valid():
932eef9a 99 c = chercheur_form.save(commit=False)
7c596de2
AJ
100
101 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
102 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
103
f810842d 104 if etablissement_form.is_valid() and discipline_form.is_valid() and these_form.is_valid():
6befc7c9 105 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
00755d9b
AJ
106 pub = publication1_form.save()
107 c.publication1 = pub
6befc7c9 108 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
00755d9b
AJ
109 pub = publication2_form.save()
110 c.publication2 = pub
6befc7c9 111 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
00755d9b
AJ
112 pub = publication3_form.save()
113 c.publication3 = pub
6befc7c9 114 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
00755d9b
AJ
115 pub = publication4_form.save()
116 c.publication4 = pub
f810842d
AJ
117 these = these_form.save()
118 c.these = these
00755d9b
AJ
119 etablissement_form.save(commit=False)
120 etablissement_autre_form.save(commit=False)
7c596de2 121 discipline_form.save(commit=False)
5ecd9e43
AJ
122 #encodage du mot de passe de l'utilisateur (refactorer car c'est pas clean
123 #et c'est pas la bonne place pour faire ca - AJ
124 personne_form.cleaned_data['password'] = hashlib.md5(personne_form.cleaned_data['password']).hexdigest()
7c596de2
AJ
125 p = personne_form.save()
126 c.personne = p
127 c.save()
f810842d 128 return HttpResponseRedirect(reverse('chercheurs.views.retrieve', args=(c.id,)))
932eef9a
AJ
129 else:
130 personne_form = PersonneForm(prefix="personne")
131 chercheur_form = ChercheurForm(prefix="chercheur")
7c596de2 132 etablissement_form = EtablissementForm(prefix="etablissement")
00755d9b 133 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
7c596de2 134 discipline_form = DisciplineForm(prefix="discipline")
00755d9b
AJ
135 publication1_form = PublicationForm(prefix="publication1")
136 publication2_form = PublicationForm(prefix="publication2")
137 publication3_form = PublicationForm(prefix="publication3")
f810842d
AJ
138 publication4_form = PublicationForm(prefix="publication4")
139 these_form = TheseForm(prefix="these")
932eef9a
AJ
140
141 variables = { 'personne_form': personne_form,
142 'chercheur_form': chercheur_form,
7c596de2
AJ
143 'etablissement_form': etablissement_form,
144 'discipline_form': discipline_form,
00755d9b
AJ
145 'etablissement_autre_form': etablissement_autre_form,
146 'publication1_form': publication1_form,
147 'publication2_form': publication2_form,
148 'publication3_form': publication3_form,
149 'publication4_form': publication4_form,
f810842d 150 'these_form': these_form,
932eef9a
AJ
151 }
152
153 return render_to_response ("chercheurs/inscription.html", \
154 Context (variables),
155 context_instance = RequestContext(request))
9af73c99
AJ
156
157
b3e1079e
AJ
158def edit(request):
159 """Edition d'un chercheur"""
160 context_instance = RequestContext(request)
161 chercheur = context_instance['user_chercheur']
162 if request.method == 'POST':
a955bdb8 163 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
f810842d 164 #chercheur_form = ChercheurForm (request.POST, prefix="chercheur", instance=chercheur)
a955bdb8
AJ
165 etablissement_form = EtablissementForm(request.POST, prefix="etablissement", instance=chercheur)
166 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre", instance=chercheur)
167 discipline_form = DisciplineForm(request.POST, prefix="discipline", instance=chercheur)
168 publication1_form = PublicationForm(request.POST, prefix="publication1", instance=chercheur.publication1)
169 publication2_form = PublicationForm(request.POST, prefix="publication2", instance=chercheur.publication2)
170 publication3_form = PublicationForm(request.POST, prefix="publication3", instance=chercheur.publication3)
171 publication4_form = PublicationForm(request.POST, prefix="publication4", instance=chercheur.publication4)
f810842d 172 these_form = TheseForm(request.POST, prefix="these", instance=chercheur.these)
a955bdb8
AJ
173
174
f810842d 175 if( personne_form.is_valid() and discipline_form.is_valid() and publication1_form.is_valid() and publication2_form.is_valid() and publication3_form.is_valid() and publication4_form.is_valid() and these_form.is_valid() ):
a955bdb8 176 personne_form.save()
f810842d 177 #chercheur_form.save()
a955bdb8
AJ
178 discipline_form.save()
179 publication1_form.save()
180 publication2_form.save()
181 publication3_form.save()
182 publication4_form.save()
f810842d 183 these_form.save()
b3e1079e 184 else:
a955bdb8 185 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
f810842d 186 #chercheur_form = ChercheurForm (prefix="chercheur", instance=chercheur)
a955bdb8
AJ
187 etablissement_form = EtablissementForm(prefix="etablissement", instance=chercheur)
188 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre", instance=chercheur)
189 discipline_form = DisciplineForm(prefix="discipline", instance=chercheur)
190 publication1_form = PublicationForm(prefix="publication1", instance=chercheur.publication1)
191 publication2_form = PublicationForm(prefix="publication2", instance=chercheur.publication2)
192 publication3_form = PublicationForm(prefix="publication3", instance=chercheur.publication3)
f810842d
AJ
193 publication4_form = PublicationForm(prefix="publication4", instance=chercheur.publication4)
194 these_form = TheseForm(prefix="these", instance=chercheur.these)
b3e1079e
AJ
195
196 variables = { 'chercheur': chercheur,
197 'personne_form':personne_form,
f810842d 198 #'chercheur_form': chercheur_form,
a955bdb8
AJ
199 'etablissement_form': etablissement_form,
200 'discipline_form': discipline_form,
201 'etablissement_autre_form': etablissement_autre_form,
202 'publication1_form': publication1_form,
203 'publication2_form': publication2_form,
204 'publication3_form': publication3_form,
205 'publication4_form': publication4_form,
f810842d 206 'these_form': these_form,
b3e1079e
AJ
207 }
208 return render_to_response ("chercheurs/edit.html", \
209 Context (variables),
210 context_instance = RequestContext(request))
211
212
213
9af73c99 214def perso(request):
0d9d1c4d 215 """Espace chercheur (espace personnel du chercheur)"""
9af73c99
AJ
216 context_instance = RequestContext(request)
217 chercheur = context_instance['user_chercheur']
0d9d1c4d 218 if not chercheur:
219 return HttpResponseRedirect(reverse('django.contrib.auth.views.login'))
588d6b93 220 variables = { 'chercheur': chercheur,
221 }
222 return render_to_response ("chercheurs/perso.html", \
223 Context (variables),
224 context_instance = RequestContext(request))
da091176 225
226def retrieve(request, id):
227 """Fiche du chercheur"""
228 chercheur = Chercheur.objects.get(id=id)
229 variables = { 'chercheur': chercheur,
230 }
231 return render_to_response ("chercheurs/retrieve.html", \
232 Context (variables),
233 context_instance = RequestContext(request))