Changé le libellé du bouton "Sauvegarder" en "Enregistrer".
[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
0e9597af 4from django.http import HttpResponseRedirect, HttpResponse
932eef9a 5from django.template import Context, RequestContext
e427f068 6from django.template.loader import get_template
51515982 7from django.core.urlresolvers import reverse as url
0e9597af
AJ
8from django.core.mail import send_mail
9from django.conf import settings
219710da 10from django.utils import simplejson
a7b16ec9 11from django.views.decorators.cache import never_cache
e8e9e4fd 12
932eef9a 13from forms import *
e8e9e4fd 14from django.forms.models import inlineformset_factory
932eef9a
AJ
15
16from auf_references_client.models import Discipline, TypeImplantation
13ec4813 17from models import Personne, Groupe, ChercheurGroupe
932eef9a 18
72923b8e 19from django.contrib.auth import authenticate, login
9af73c99 20from django.contrib.auth.decorators import login_required
72923b8e 21from django.contrib.auth.forms import AuthenticationForm as OriginalAuthenticationForm
8baa2a56 22from django.contrib.auth.models import User
13146d99 23
510b5321 24from django.db.models import Q
3eb00212 25from django.shortcuts import get_object_or_404
510b5321 26
c18af6bd 27from django.utils.translation import ugettext_lazy as _
c18af6bd 28
0e9597af
AJ
29#TODO: Migrer tout ce qui a rapport aux users dans une nouvelle app
30
c18af6bd 31class AuthenticationForm(OriginalAuthenticationForm):
b5af3133 32 username = forms.CharField(label='Adresse électronique', max_length=255)
c18af6bd 33
f0692c02 34def send_password(request):
0e9597af
AJ
35 if request.method == "POST":
36 form = SendPasswordForm(data=request.POST)
37 if form.is_valid():
13ec4813 38 u = Personne.objects.get(courriel=form.cleaned_data['email'])
92990258 39 code = u.get_new_password_code()
d0c97f36 40 link = "%s/accounts/new_password/%s/%s/" % (settings.SITE_ROOT_URL, u.courriel, code)
e427f068
AJ
41
42 variables = { 'user': u,
43 'link': link,
206989d4
AJ
44 'SITE_ROOT_URL': settings.SITE_ROOT_URL,
45 'CONTACT_EMAIL': settings.CONTACT_EMAIL,
e427f068
AJ
46 }
47 t = get_template('accounts/email_password.html')
48 content = t.render(Context(variables))
49
0e9597af 50 send_mail('Savoirs en partage: changement de mot de passe',
e427f068 51 content, settings.CONTACT_EMAIL,
0e9597af
AJ
52 [u.courriel], fail_silently=False)
53 else:
54 form = SendPasswordForm()
55
56 variables = { 'form': form,
57 }
58 return render_to_response ("accounts/send_password.html", \
59 Context (variables),
60 context_instance = RequestContext(request))
e427f068 61
0e9597af 62def new_password(request, email, code):
13ec4813 63 u = Personne.objects.get(courriel=email)
92990258 64 original_code = u.get_new_password_code()
e427f068 65 message=""
0e9597af 66 if(code == original_code):
e427f068
AJ
67 if request.method == "POST":
68 form = NewPasswordForm(data=request.POST)
69 if form.is_valid():
92990258 70 u.set_password(form.cleaned_data['password'])
e427f068
AJ
71 u.save()
72 message = "Votre mot de passe a été modifié."
73 else:
74 form = NewPasswordForm()
0e9597af
AJ
75 else:
76 return HttpResponseRedirect('/')
e427f068
AJ
77 variables = { 'form': form,
78 'message': message,
0e9597af
AJ
79 }
80 return render_to_response ("accounts/new_password.html", \
81 Context (variables),
82 context_instance = RequestContext(request))
c18af6bd 83
e427f068 84@login_required()
f0692c02 85def change_password(request):
e427f068 86 context_instance = RequestContext(request)
13ec4813 87 u = context_instance['user_chercheur']
e427f068
AJ
88 message = ""
89 if request.method == "POST":
90 form = NewPasswordForm(data=request.POST)
91 if form.is_valid():
92990258 92 u.set_password(form.cleaned_data['password'])
e427f068
AJ
93 u.save()
94 message = "Votre mot de passe a été modifié."
95 else:
96 form = NewPasswordForm()
97 variables = { 'form': form,
98 'message': message,
99 }
100 return render_to_response ("accounts/new_password.html", \
101 Context (variables),
102 context_instance = RequestContext(request))
103
f0692c02 104def chercheur_login(request):
c18af6bd 105 "Displays the login form and handles the login action."
c18af6bd
AJ
106 if request.method == "POST":
107 form = AuthenticationForm(data=request.POST)
108 if form.is_valid():
c18af6bd
AJ
109 from django.contrib.auth import login
110 login(request, form.get_user())
111 if request.session.test_cookie_worked():
112 request.session.delete_test_cookie()
0539c4e2 113 return HttpResponseRedirect(url('chercheurs.views.perso'))
c18af6bd
AJ
114 else:
115 form = AuthenticationForm(request)
116 request.session.set_test_cookie()
c1b134f8
EMS
117 return render_to_response('accounts/login.html', dict(form=form),
118 context_instance=RequestContext(request))
c18af6bd 119
f0692c02 120def index(request):
f8c16b3d 121 """Répertoire des chercheurs"""
f0692c02 122 search_form = RepertoireSearchForm(request.GET)
13ec4813 123 chercheurs = search_form.get_query_set().select_related('etablissement')
7020ea3d
EMS
124 sort = request.GET.get('tri')
125 if sort is not None and sort.endswith('_desc'):
126 sort = sort[:-5]
127 direction = '-'
128 else:
129 direction = ''
130 if sort == 'nom':
acd5cd8f 131 chercheurs = chercheurs.order_by_nom(direction)
7020ea3d 132 elif sort == 'etablissement':
acd5cd8f 133 chercheurs = chercheurs.order_by_etablissement(direction)
7020ea3d 134 elif sort == 'pays':
acd5cd8f 135 chercheurs = chercheurs.order_by_pays(direction)
7020ea3d
EMS
136 else:
137 chercheurs = chercheurs.order_by('-date_modification')
138
9f7c169e 139 nb_chercheurs = chercheurs.count()
3efbacbe 140 return render_to_response("chercheurs/index.html",
e9b0d630 141 dict(chercheurs=chercheurs, nb_chercheurs=nb_chercheurs, search_form=search_form),
3efbacbe 142 context_instance=RequestContext(request))
588d6b93 143
f0692c02 144def inscription(request):
932eef9a 145 if request.method == 'POST':
a7b16ec9
EMS
146 forms = ChercheurFormGroup(request.POST)
147 if forms.is_valid():
148 forms.save()
149 # login automatique
13ec4813
EMS
150 login(request, authenticate(username=forms.chercheur.cleaned_data['courriel'],
151 password=forms.chercheur.cleaned_data['password']))
0539c4e2 152 return HttpResponseRedirect(url('chercheurs.views.perso'))
932eef9a 153 else:
a7b16ec9 154 forms = ChercheurFormGroup()
932eef9a 155
a7b16ec9
EMS
156 return render_to_response("chercheurs/inscription.html",
157 dict(forms=forms),
158 context_instance=RequestContext(request))
9af73c99 159
0e9597af 160@login_required()
8baa2a56
EMS
161def desinscription(request):
162 """Désinscription du chercheur"""
163 try:
13ec4813 164 chercheur = Chercheur.objects.get(courriel=request.user.email, actif=True)
8baa2a56
EMS
165 except Chercheur.DoesNotExist:
166 return HttpResponseRedirect(url('chercheurs.views.chercheur_login'))
167 if request.method == 'POST':
168 if request.POST.get('confirmer'):
13ec4813
EMS
169 chercheur.actif = False
170 chercheur.save()
171 User.objects.filter(username=chercheur.courriel).delete()
8baa2a56
EMS
172 request.flash['message'] = "Vous avez été désinscrit du répertoire des chercheurs."
173 return HttpResponseRedirect(url('django.contrib.auth.views.logout'))
174 else:
175 request.flash['message'] = "Opération annulée."
176 return HttpResponseRedirect(url('chercheurs.views.perso'))
177 return render_to_response("chercheurs/desinscription.html", {},
178 context_instance=RequestContext(request))
179
180@login_required()
a7b16ec9 181@never_cache
f0692c02 182def edit(request):
b3e1079e
AJ
183 """Edition d'un chercheur"""
184 context_instance = RequestContext(request)
185 chercheur = context_instance['user_chercheur']
186 if request.method == 'POST':
a7b16ec9
EMS
187 forms = ChercheurFormGroup(request.POST, chercheur=chercheur)
188 if forms.is_valid():
189 forms.save()
0539c4e2 190 return HttpResponseRedirect(url('chercheurs.views.perso') + '?modification=1')
b3e1079e 191 else:
a7b16ec9 192 forms = ChercheurFormGroup(chercheur=chercheur)
b3e1079e 193
a7b16ec9
EMS
194 return render_to_response("chercheurs/edit.html",
195 dict(forms=forms, chercheur=chercheur),
595ab4d6 196 context_instance=RequestContext(request))
b3e1079e 197
0e9597af 198@login_required()
f0692c02 199def perso(request):
0d9d1c4d 200 """Espace chercheur (espace personnel du chercheur)"""
9af73c99
AJ
201 context_instance = RequestContext(request)
202 chercheur = context_instance['user_chercheur']
2a36714f 203 modification = request.GET.get('modification')
0d9d1c4d 204 if not chercheur:
0539c4e2 205 return HttpResponseRedirect(url('chercheurs.views.chercheur_login'))
e4d01d1d
EMS
206 return render_to_response("chercheurs/perso.html",
207 dict(chercheur=chercheur, modification=modification),
8baa2a56 208 context_instance=context_instance)
da091176 209
f0692c02 210def retrieve(request, id):
da091176 211 """Fiche du chercheur"""
3eb00212 212 chercheur = get_object_or_404(Chercheur, id=id)
e4d01d1d
EMS
213 return render_to_response("chercheurs/retrieve.html",
214 dict(chercheur=chercheur),
c1b134f8 215 context_instance=RequestContext(request))
d9885bf7 216
f0692c02 217def conversion(request):
c1b134f8
EMS
218 return render_to_response("chercheurs/conversion.html", {},
219 context_instance=RequestContext(request))
8baa2a56 220
e836f6f7 221def etablissements_autocomplete(request, pays=None):
219710da 222 term = request.GET.get('term')
e836f6f7
EMS
223 noms = Etablissement.objects.all()
224 for word in term.split():
225 noms = noms.filter(nom__icontains=word)
226 if pays:
227 noms = noms.filter(pays=pays)
228 noms = list(noms.values_list('nom', flat=True)[:20])
219710da
EMS
229 json = simplejson.dumps(noms)
230 return HttpResponse(json, mimetype='application/json')