Réparé le fil RSS de l'agenda
[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 121 search_form = RepertoireSearchForm(request.GET)
7020ea3d
EMS
122 chercheurs = search_form.get_query_set().select_related('personne', 'etablissement', 'etablissement__pays', 'etablissement_autre_pays')
123 sort = request.GET.get('tri')
124 if sort is not None and sort.endswith('_desc'):
125 sort = sort[:-5]
126 direction = '-'
127 else:
128 direction = ''
129 if sort == 'nom':
130 chercheurs = chercheurs.order_by(direction + 'personne__nom', 'personne__prenom', '-date_modification')
131 elif sort == 'etablissement':
132 chercheurs = chercheurs.extra(select=dict(nom_etablissement='IFNULL(ref_etablissement.nom, chercheurs_chercheur.etablissement_autre_nom)'),
133 order_by=[direction + 'nom_etablissement', '-date_modification'])
134 elif sort == 'pays':
135 chercheurs = chercheurs.extra(select=dict(pays_etablissement='IFNULL(ref_pays.nom, T5.nom)'),
136 order_by=[direction + 'pays_etablissement', '-date_modification'])
137 else:
138 chercheurs = chercheurs.order_by('-date_modification')
139
9f7c169e 140 nb_chercheurs = chercheurs.count()
3efbacbe 141 return render_to_response("chercheurs/index.html",
e9b0d630 142 dict(chercheurs=chercheurs, nb_chercheurs=nb_chercheurs, search_form=search_form),
3efbacbe 143 context_instance=RequestContext(request))
588d6b93 144
f0692c02 145def inscription(request):
932eef9a 146 if request.method == 'POST':
a7b16ec9
EMS
147 forms = ChercheurFormGroup(request.POST)
148 if forms.is_valid():
149 forms.save()
150 # login automatique
151 login(request, authenticate(username=forms.personne.cleaned_data['courriel'],
92990258 152 password=forms.personne.cleaned_data['password']))
0539c4e2 153 return HttpResponseRedirect(url('chercheurs.views.perso'))
932eef9a 154 else:
a7b16ec9 155 forms = ChercheurFormGroup()
932eef9a 156
a7b16ec9
EMS
157 return render_to_response("chercheurs/inscription.html",
158 dict(forms=forms),
159 context_instance=RequestContext(request))
9af73c99 160
0e9597af 161@login_required()
8baa2a56
EMS
162def desinscription(request):
163 """Désinscription du chercheur"""
164 try:
595ab4d6 165 chercheur = Chercheur.objects.get(personne__courriel=request.user.email, personne__actif=True)
8baa2a56
EMS
166 except Chercheur.DoesNotExist:
167 return HttpResponseRedirect(url('chercheurs.views.chercheur_login'))
168 if request.method == 'POST':
169 if request.POST.get('confirmer'):
170 chercheur.personne.actif = False
171 chercheur.personne.save()
172 User.objects.filter(username=chercheur.personne.courriel).delete()
173 request.flash['message'] = "Vous avez été désinscrit du répertoire des chercheurs."
174 return HttpResponseRedirect(url('django.contrib.auth.views.logout'))
175 else:
176 request.flash['message'] = "Opération annulée."
177 return HttpResponseRedirect(url('chercheurs.views.perso'))
178 return render_to_response("chercheurs/desinscription.html", {},
179 context_instance=RequestContext(request))
180
181@login_required()
a7b16ec9 182@never_cache
f0692c02 183def edit(request):
b3e1079e
AJ
184 """Edition d'un chercheur"""
185 context_instance = RequestContext(request)
186 chercheur = context_instance['user_chercheur']
187 if request.method == 'POST':
a7b16ec9
EMS
188 forms = ChercheurFormGroup(request.POST, chercheur=chercheur)
189 if forms.is_valid():
190 forms.save()
0539c4e2 191 return HttpResponseRedirect(url('chercheurs.views.perso') + '?modification=1')
b3e1079e 192 else:
a7b16ec9 193 forms = ChercheurFormGroup(chercheur=chercheur)
b3e1079e 194
a7b16ec9
EMS
195 return render_to_response("chercheurs/edit.html",
196 dict(forms=forms, chercheur=chercheur),
595ab4d6 197 context_instance=RequestContext(request))
b3e1079e 198
0e9597af 199@login_required()
f0692c02 200def perso(request):
0d9d1c4d 201 """Espace chercheur (espace personnel du chercheur)"""
9af73c99
AJ
202 context_instance = RequestContext(request)
203 chercheur = context_instance['user_chercheur']
2a36714f 204 modification = request.GET.get('modification')
0d9d1c4d 205 if not chercheur:
0539c4e2 206 return HttpResponseRedirect(url('chercheurs.views.chercheur_login'))
e4d01d1d
EMS
207 return render_to_response("chercheurs/perso.html",
208 dict(chercheur=chercheur, modification=modification),
8baa2a56 209 context_instance=context_instance)
da091176 210
f0692c02 211def retrieve(request, id):
da091176 212 """Fiche du chercheur"""
3eb00212 213 chercheur = get_object_or_404(Chercheur, id=id)
e4d01d1d
EMS
214 return render_to_response("chercheurs/retrieve.html",
215 dict(chercheur=chercheur),
c1b134f8 216 context_instance=RequestContext(request))
d9885bf7 217
f0692c02 218def conversion(request):
c1b134f8
EMS
219 return render_to_response("chercheurs/conversion.html", {},
220 context_instance=RequestContext(request))
8baa2a56 221