repertoire chercheurs ajustements
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / views.py
1 # -*- encoding: utf-8 -*-
2 import hashlib
3 from django.shortcuts import render_to_response
4 from django.http import HttpResponseRedirect
5 from django.template import Context, RequestContext
6 from django.core.urlresolvers import reverse
7 from forms import *
8
9 from auf_references_client.models import Discipline, TypeImplantation
10 from models import Personne, Utilisateur
11
12 from django.contrib.auth.decorators import login_required
13
14 def 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:
22 list = list.filter (nationalite = pays.pk)
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
37 def repertoire(request):
38 """Mock up du répertoire"""
39
40 chercheurs = chercheur_queryset (request)
41 repertoire_form = RepertoireSearchForm (request.GET)
42
43 nb_chercheurs = chercheurs.count()
44 variables = { 'chercheurs': chercheurs,
45 'nb_chercheurs': nb_chercheurs,
46 'repertoire_form': repertoire_form,
47 }
48 return render_to_response ("chercheurs/repertoire.html", \
49 Context (variables),
50 context_instance = RequestContext(request))
51
52 def inscription(request):
53 if request.method == 'POST':
54 personne_form = PersonneForm (request.POST, prefix="personne")
55 chercheur_form = ChercheurForm (request.POST, prefix="chercheur")
56 etablissement_form = EtablissementForm (request.POST, prefix="etablissement")
57 etablissement_autre_form = EtablissementAutreForm(request.POST, prefix="etablissement_autre")
58 discipline_form = DisciplineForm (request.POST, prefix="discipline")
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")
63
64 if personne_form.is_valid():
65 if chercheur_form.is_valid():
66 c = chercheur_form.save(commit=False)
67
68 etablissement_form = EtablissementForm (request.POST, prefix="etablissement", instance=c)
69 discipline_form = DisciplineForm (request.POST, prefix="discipline", instance=c)
70
71 if etablissement_form.is_valid() and discipline_form.is_valid():
72 if publication1_form.is_valid() and publication1_form.cleaned_data['titre']:
73 pub = publication1_form.save()
74 c.publication1 = pub
75 if publication2_form.is_valid() and publication2_form.cleaned_data['titre']:
76 pub = publication2_form.save()
77 c.publication2 = pub
78 if publication3_form.is_valid() and publication3_form.cleaned_data['titre']:
79 pub = publication3_form.save()
80 c.publication3 = pub
81 if publication4_form.is_valid() and publication4_form.cleaned_data['titre']:
82 pub = publication4_form.save()
83 c.publication4 = pub
84 etablissement_form.save(commit=False)
85 etablissement_autre_form.save(commit=False)
86 discipline_form.save(commit=False)
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()
90 p = personne_form.save()
91 c.personne = p
92 c.save()
93 return HttpResponseRedirect(reverse('chercheurs.views.retrieve', args=(p.id,)))
94 else:
95 personne_form = PersonneForm(prefix="personne")
96 chercheur_form = ChercheurForm(prefix="chercheur")
97 etablissement_form = EtablissementForm(prefix="etablissement")
98 etablissement_autre_form = EtablissementAutreForm(prefix="etablissement_autre")
99 discipline_form = DisciplineForm(prefix="discipline")
100 publication1_form = PublicationForm(prefix="publication1")
101 publication2_form = PublicationForm(prefix="publication2")
102 publication3_form = PublicationForm(prefix="publication3")
103 publication4_form = PublicationForm(prefix="publication4")
104
105 variables = { 'personne_form': personne_form,
106 'chercheur_form': chercheur_form,
107 'etablissement_form': etablissement_form,
108 'discipline_form': discipline_form,
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,
114 }
115
116 return render_to_response ("chercheurs/inscription.html", \
117 Context (variables),
118 context_instance = RequestContext(request))
119
120
121 def 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
140 def perso(request):
141 """Mock up de l'espace perso"""
142 context_instance = RequestContext(request)
143 chercheur = context_instance['user_chercheur']
144 variables = { 'chercheur': chercheur,
145 }
146 return render_to_response ("chercheurs/perso.html", \
147 Context (variables),
148 context_instance = RequestContext(request))
149
150 def 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))