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