Recherche par discipline/région devient une recherche textuelle.
[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
c1b134f8 32def send_password(request, discipline=None, region=None):
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'])
37 code = hashlib.md5(u.courriel+u.password).hexdigest()
38 code = code[0:6]
39 link = "%saccounts/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
AJ
61def new_password(request, email, code):
62 u = Utilisateur.objects.get(courriel=email)
63 original_code = hashlib.md5(u.courriel+u.password).hexdigest()
64 original_code = original_code[0:6]
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():
70 new_password = form.cleaned_data['password']
71 u.password = hashlib.md5(new_password).hexdigest()
72 u.save()
73 message = "Votre mot de passe a été modifié."
74 else:
75 form = NewPasswordForm()
0e9597af
AJ
76 else:
77 return HttpResponseRedirect('/')
e427f068
AJ
78 variables = { 'form': form,
79 'message': message,
0e9597af
AJ
80 }
81 return render_to_response ("accounts/new_password.html", \
82 Context (variables),
83 context_instance = RequestContext(request))
c18af6bd 84
e427f068 85@login_required()
c1b134f8 86def change_password(request, discipline=None, region=None):
e427f068
AJ
87 context_instance = RequestContext(request)
88 u = context_instance['user_sep']
89 message = ""
90 if request.method == "POST":
91 form = NewPasswordForm(data=request.POST)
92 if form.is_valid():
93 new_password = form.cleaned_data['password']
94 u.password = hashlib.md5(new_password).hexdigest()
95 u.save()
96 message = "Votre mot de passe a été modifié."
97 else:
98 form = NewPasswordForm()
99 variables = { 'form': form,
100 'message': message,
101 }
102 return render_to_response ("accounts/new_password.html", \
103 Context (variables),
104 context_instance = RequestContext(request))
105
c1b134f8 106def chercheur_login(request, discipline=None, region=None):
c18af6bd 107 "Displays the login form and handles the login action."
c18af6bd
AJ
108 if request.method == "POST":
109 form = AuthenticationForm(data=request.POST)
110 if form.is_valid():
c18af6bd
AJ
111 from django.contrib.auth import login
112 login(request, form.get_user())
113 if request.session.test_cookie_worked():
114 request.session.delete_test_cookie()
c1b134f8
EMS
115 kwargs = {}
116 if discipline:
117 kwargs['discipline'] = discipline
118 if region:
119 kwargs['region'] = region
120 return HttpResponseRedirect(url('chercheurs.views.perso', kwargs=kwargs))
c18af6bd
AJ
121 else:
122 form = AuthenticationForm(request)
123 request.session.set_test_cookie()
c1b134f8
EMS
124 return render_to_response('accounts/login.html', dict(form=form),
125 context_instance=RequestContext(request))
c18af6bd 126
c1b134f8 127def index(request, discipline=None, region=None):
f8c16b3d 128 """Répertoire des chercheurs"""
c548d94a 129 search_form = RepertoireSearchForm(request.GET, region=region)
c1b134f8 130 chercheurs = search_form.get_query_set().filter_discipline(discipline).filter_region(region)
9f7c169e 131 nb_chercheurs = chercheurs.count()
3efbacbe 132 return render_to_response("chercheurs/index.html",
e9b0d630 133 dict(chercheurs=chercheurs, nb_chercheurs=nb_chercheurs, search_form=search_form),
3efbacbe 134 context_instance=RequestContext(request))
588d6b93 135
c1b134f8 136def inscription(request, discipline=None, region=None):
932eef9a 137 if request.method == 'POST':
a7b16ec9
EMS
138 forms = ChercheurFormGroup(request.POST)
139 if forms.is_valid():
140 forms.save()
141 # login automatique
142 login(request, authenticate(username=forms.personne.cleaned_data['courriel'],
143 password=forms.personne.cleaned_data['password']))
c1b134f8
EMS
144 kwargs = {}
145 if discipline:
146 kwargs['discipline'] = discipline
147 if region:
148 kwargs['region'] = region
149 return HttpResponseRedirect(url('chercheurs.views.perso', kwargs=kwargs))
932eef9a 150 else:
a7b16ec9 151 forms = ChercheurFormGroup()
932eef9a 152
a7b16ec9
EMS
153 return render_to_response("chercheurs/inscription.html",
154 dict(forms=forms),
155 context_instance=RequestContext(request))
9af73c99 156
0e9597af 157@login_required()
a7b16ec9 158@never_cache
c1b134f8 159def edit(request, discipline=None, region=None):
b3e1079e
AJ
160 """Edition d'un chercheur"""
161 context_instance = RequestContext(request)
162 chercheur = context_instance['user_chercheur']
163 if request.method == 'POST':
a7b16ec9
EMS
164 forms = ChercheurFormGroup(request.POST, chercheur=chercheur)
165 if forms.is_valid():
166 forms.save()
c1b134f8
EMS
167 kwargs = {}
168 if discipline:
169 kwargs['discipline'] = discipline
170 if region:
171 kwargs['region'] = region
172 return HttpResponseRedirect(url('chercheurs.views.perso', kwargs=kwargs) + '?modification=1')
b3e1079e 173 else:
a7b16ec9 174 forms = ChercheurFormGroup(chercheur=chercheur)
b3e1079e 175
a7b16ec9
EMS
176 return render_to_response("chercheurs/edit.html",
177 dict(forms=forms, chercheur=chercheur),
178 context_instance = RequestContext(request))
b3e1079e 179
0e9597af 180@login_required()
c1b134f8 181def perso(request, discipline=None, region=None):
0d9d1c4d 182 """Espace chercheur (espace personnel du chercheur)"""
9af73c99
AJ
183 context_instance = RequestContext(request)
184 chercheur = context_instance['user_chercheur']
2a36714f 185 modification = request.GET.get('modification')
0d9d1c4d 186 if not chercheur:
c1b134f8
EMS
187 kwargs = {}
188 if discipline:
189 kwargs['discipline'] = discipline
190 if region:
191 kwargs['region'] = region
192 return HttpResponseRedirect(url('chercheurs.views.chercheur_login', kwargs=kwargs))
e4d01d1d
EMS
193 return render_to_response("chercheurs/perso.html",
194 dict(chercheur=chercheur, modification=modification),
195 context_instance=RequestContext(request))
da091176 196
c1b134f8 197def retrieve(request, id, discipline=None, region=None):
da091176 198 """Fiche du chercheur"""
3eb00212 199 chercheur = get_object_or_404(Chercheur, id=id)
e4d01d1d
EMS
200 return render_to_response("chercheurs/retrieve.html",
201 dict(chercheur=chercheur),
c1b134f8 202 context_instance=RequestContext(request))
d9885bf7 203
c1b134f8
EMS
204def conversion(request, discipline=None, region=None):
205 return render_to_response("chercheurs/conversion.html", {},
206 context_instance=RequestContext(request))