repertoire chercheurs ajustements
[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)
32 mots_cles = simpleForm.cleaned_data["mots_cles"]
33 if mots_cles:
34 list = list.filter (personne__nom__icontains=mots_cles)
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':
126 personne_form = PersonneEditForm(request.POST, prefix="personne", instance=chercheur.personne)
127 personne_form.save()
128 else:
129 personne_form = PersonneEditForm(prefix="personne", instance=chercheur.personne)
130
131 variables = { 'chercheur': chercheur,
132 'personne_form':personne_form,
133 }
134 return render_to_response ("chercheurs/edit.html", \
135 Context (variables),
136 context_instance = RequestContext(request))
137
138
139
9af73c99 140def perso(request):
588d6b93 141 """Mock up de l'espace perso"""
9af73c99
AJ
142 context_instance = RequestContext(request)
143 chercheur = context_instance['user_chercheur']
588d6b93 144 variables = { 'chercheur': chercheur,
145 }
146 return render_to_response ("chercheurs/perso.html", \
147 Context (variables),
148 context_instance = RequestContext(request))
da091176 149
150def retrieve(request, id):
151 """Fiche du chercheur"""
152 chercheur = Chercheur.objects.get(id=id)
153 variables = { 'chercheur': chercheur,
154 }
155 return render_to_response ("chercheurs/retrieve.html", \
156 Context (variables),
157 context_instance = RequestContext(request))