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