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