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