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