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