b8fc891cd4520690a886074b769c1b3ee1845c74
[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, HttpResponse
5 from django.template import Context, RequestContext
6 from django.template.loader import get_template
7 from django.core.urlresolvers import reverse as url
8 from django.core.mail import send_mail
9 from django.conf import settings
10 from django.views.decorators.cache import never_cache
11
12 from forms import *
13 from django.forms.models import inlineformset_factory
14
15 from auf_references_client.models import Discipline, TypeImplantation
16 from models import Personne, Utilisateur, Groupe, ChercheurGroupe
17
18 from django.contrib.auth import authenticate, login
19 from django.contrib.auth.decorators import login_required
20 from django.contrib.auth.forms import AuthenticationForm as OriginalAuthenticationForm
21 from django.contrib.auth.models import User
22
23 from django.db.models import Q
24 from django.shortcuts import get_object_or_404
25
26 from django.utils.translation import ugettext_lazy as _
27
28 #TODO: Migrer tout ce qui a rapport aux users dans une nouvelle app
29
30 class AuthenticationForm(OriginalAuthenticationForm):
31 username = forms.CharField(label='Adresse électronique', max_length=255)
32
33 def send_password(request):
34 if request.method == "POST":
35 form = SendPasswordForm(data=request.POST)
36 if form.is_valid():
37 u = Utilisateur.objects.get(courriel=form.cleaned_data['email'])
38 code = u.get_new_password_code()
39 link = "%s/accounts/new_password/%s/%s/" % (settings.SITE_ROOT_URL, u.courriel, code)
40
41 variables = { 'user': u,
42 'link': link,
43 'SITE_ROOT_URL': settings.SITE_ROOT_URL,
44 'CONTACT_EMAIL': settings.CONTACT_EMAIL,
45 }
46 t = get_template('accounts/email_password.html')
47 content = t.render(Context(variables))
48
49 send_mail('Savoirs en partage: changement de mot de passe',
50 content, settings.CONTACT_EMAIL,
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))
60
61 def new_password(request, email, code):
62 u = Utilisateur.objects.get(courriel=email)
63 original_code = u.get_new_password_code()
64 message=""
65 if(code == original_code):
66 if request.method == "POST":
67 form = NewPasswordForm(data=request.POST)
68 if form.is_valid():
69 u.set_password(form.cleaned_data['password'])
70 u.save()
71 message = "Votre mot de passe a été modifié."
72 else:
73 form = NewPasswordForm()
74 else:
75 return HttpResponseRedirect('/')
76 variables = { 'form': form,
77 'message': message,
78 }
79 return render_to_response ("accounts/new_password.html", \
80 Context (variables),
81 context_instance = RequestContext(request))
82
83 @login_required()
84 def change_password(request):
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():
91 u.set_password(form.cleaned_data['password'])
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
103 def chercheur_login(request):
104 "Displays the login form and handles the login action."
105 if request.method == "POST":
106 form = AuthenticationForm(data=request.POST)
107 if form.is_valid():
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()
112 return HttpResponseRedirect(url('chercheurs.views.perso'))
113 else:
114 form = AuthenticationForm(request)
115 request.session.set_test_cookie()
116 return render_to_response('accounts/login.html', dict(form=form),
117 context_instance=RequestContext(request))
118
119 def index(request):
120 """Répertoire des chercheurs"""
121 search_form = RepertoireSearchForm(request.GET)
122 chercheurs = search_form.get_query_set()
123 nb_chercheurs = chercheurs.count()
124 return render_to_response("chercheurs/index.html",
125 dict(chercheurs=chercheurs, nb_chercheurs=nb_chercheurs, search_form=search_form),
126 context_instance=RequestContext(request))
127
128 def inscription(request):
129 if request.method == 'POST':
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'],
135 password=forms.personne.cleaned_data['password']))
136 return HttpResponseRedirect(url('chercheurs.views.perso'))
137 else:
138 forms = ChercheurFormGroup()
139
140 return render_to_response("chercheurs/inscription.html",
141 dict(forms=forms),
142 context_instance=RequestContext(request))
143
144 @login_required()
145 def desinscription(request):
146 """Désinscription du chercheur"""
147 try:
148 chercheur = Chercheur.objects.get(personne__courriel=request.user.email)
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()
165 @never_cache
166 def edit(request):
167 """Edition d'un chercheur"""
168 context_instance = RequestContext(request)
169 chercheur = context_instance['user_chercheur']
170 if request.method == 'POST':
171 forms = ChercheurFormGroup(request.POST, chercheur=chercheur)
172 if forms.is_valid():
173 forms.save()
174 return HttpResponseRedirect(url('chercheurs.views.perso') + '?modification=1')
175 else:
176 forms = ChercheurFormGroup(chercheur=chercheur)
177
178 return render_to_response("chercheurs/edit.html",
179 dict(forms=forms, chercheur=chercheur),
180 context_instance = RequestContext(request))
181
182 @login_required()
183 def perso(request):
184 """Espace chercheur (espace personnel du chercheur)"""
185 context_instance = RequestContext(request)
186 chercheur = context_instance['user_chercheur']
187 modification = request.GET.get('modification')
188 if not chercheur:
189 return HttpResponseRedirect(url('chercheurs.views.chercheur_login'))
190 return render_to_response("chercheurs/perso.html",
191 dict(chercheur=chercheur, modification=modification),
192 context_instance=context_instance)
193
194 def retrieve(request, id):
195 """Fiche du chercheur"""
196 chercheur = get_object_or_404(Chercheur, id=id)
197 return render_to_response("chercheurs/retrieve.html",
198 dict(chercheur=chercheur),
199 context_instance=RequestContext(request))
200
201 def conversion(request):
202 return render_to_response("chercheurs/conversion.html", {},
203 context_instance=RequestContext(request))
204