Communauté: Afficher que les communauté dans la page d'index
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / views.py
CommitLineData
588d6b93 1# -*- encoding: utf-8 -*-
518d0b44 2from chercheurs.decorators import chercheur_required
fd6352ea 3from chercheurs.forms import ChercheurSearchForm, SetPasswordForm, ChercheurFormGroup, AuthenticationForm, GroupeSearchForm, MessageForm
544dec4f
PP
4from chercheurs.models import Chercheur, Groupe, Message, AdhesionGroupe, AuthLDAP
5from chercheurs.utils import get_django_user_for_email, create_ldap_hash
fdcf5874 6from datamaster_modeles.models import Etablissement, Region
fa6a2a07 7from django.conf import settings
932eef9a 8from django.shortcuts import render_to_response
fdcf5874 9from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, HttpResponseNotFound
932eef9a 10from django.template import Context, RequestContext
e427f068 11from django.template.loader import get_template
51515982 12from django.core.urlresolvers import reverse as url
0e9597af 13from django.core.mail import send_mail
fa6a2a07
EMS
14from django.contrib.auth import REDIRECT_FIELD_NAME
15from django.contrib.auth import login as auth_login
fdcf5874 16from django.contrib.auth.decorators import login_required
fa6a2a07 17from django.contrib.sites.models import RequestSite, Site
219710da 18from django.utils import simplejson
43ed73e7 19from django.utils.http import int_to_base36, base36_to_int
a7b16ec9 20from django.views.decorators.cache import never_cache
544dec4f 21from django.contrib.auth import authenticate
3eb00212 22from django.shortcuts import get_object_or_404
b0609188
PP
23from django.views.decorators.csrf import csrf_protect
24from django.contrib.auth.forms import PasswordChangeForm
fdcf5874 25from savoirs.models import PageStatique, Discipline
510b5321 26
9aa1d783 27
f0692c02 28def index(request):
f8c16b3d 29 """Répertoire des chercheurs"""
fdcf5874
EMS
30 search_form = ChercheurSearchForm(request.GET)
31 search = search_form.save(commit=False)
32 chercheurs = search.run().select_related('etablissement')
7020ea3d
EMS
33 sort = request.GET.get('tri')
34 if sort is not None and sort.endswith('_desc'):
35 sort = sort[:-5]
36 direction = '-'
37 else:
38 direction = ''
39 if sort == 'nom':
acd5cd8f 40 chercheurs = chercheurs.order_by_nom(direction)
7020ea3d 41 elif sort == 'etablissement':
acd5cd8f 42 chercheurs = chercheurs.order_by_etablissement(direction)
7020ea3d 43 elif sort == 'pays':
acd5cd8f 44 chercheurs = chercheurs.order_by_pays(direction)
7020ea3d
EMS
45 else:
46 chercheurs = chercheurs.order_by('-date_modification')
f09bc1c6
EMS
47
48 try:
49 p = PageStatique.objects.get(id='repertoire')
50 entete = p.contenu
51 except PageStatique.DoesNotExist:
52 entete = u'<h1>Répertoire des chercheurs</h1>'
7020ea3d 53
9f7c169e 54 nb_chercheurs = chercheurs.count()
fdcf5874 55
3efbacbe 56 return render_to_response("chercheurs/index.html",
f09bc1c6
EMS
57 dict(chercheurs=chercheurs, nb_chercheurs=nb_chercheurs,
58 search_form=search_form, entete=entete),
3efbacbe 59 context_instance=RequestContext(request))
588d6b93 60
f0692c02 61def inscription(request):
932eef9a 62 if request.method == 'POST':
a7b16ec9
EMS
63 forms = ChercheurFormGroup(request.POST)
64 if forms.is_valid():
43ed73e7
EMS
65 chercheur = forms.save()
66 id_base36 = int_to_base36(chercheur.id)
67 token = chercheur.activation_token()
68 template = get_template('chercheurs/activation_email.txt')
69 domain = RequestSite(request).domain
70 message = template.render(Context(dict(chercheur=chercheur, id_base36=id_base36, token=token, domain=domain)))
71 send_mail('Votre inscription à Savoirs en partage', message, None, [chercheur.courriel])
72 return HttpResponseRedirect(url('chercheurs-inscription-faite'))
932eef9a 73 else:
a7b16ec9 74 forms = ChercheurFormGroup()
932eef9a 75
a7b16ec9
EMS
76 return render_to_response("chercheurs/inscription.html",
77 dict(forms=forms),
78 context_instance=RequestContext(request))
9af73c99 79
43ed73e7
EMS
80def activation(request, id_base36, token):
81 """Activation d'un chercheur"""
82 id = base36_to_int(id_base36)
3e556e66 83 chercheur = get_object_or_404(Chercheur.all_objects, id=id)
43ed73e7
EMS
84 if token == chercheur.activation_token():
85 validlink = True
86 if request.method == 'POST':
87 form = SetPasswordForm(request.POST)
88 if form.is_valid():
89 password = form.cleaned_data['password']
90 email = chercheur.courriel
91 chercheur.actif = True
92 chercheur.save()
695930dd
EMS
93 user = get_django_user_for_email(email)
94 user.set_password(password)
95 user.save()
43ed73e7
EMS
96
97 # Auto-login
3e556e66 98 auth_login(request, authenticate(username=email, password=password))
43ed73e7
EMS
99 return HttpResponseRedirect(url('chercheurs.views.perso'))
100 else:
101 form = SetPasswordForm()
102 else:
103 form = None
104 validlink = False
105 return render_to_response('chercheurs/activation.html', dict(form=form, validlink=validlink),
106 context_instance=RequestContext(request))
107
b0609188
PP
108@csrf_protect
109@login_required
110def password_change(request, template_name='registration/password_change_form.html',
111 post_change_redirect=None, password_change_form=PasswordChangeForm):
112 if post_change_redirect is None:
113 post_change_redirect = url('django.contrib.auth.views.password_change_done')
114 if request.method == "POST":
115 form = password_change_form(user=request.user, data=request.POST)
116 if form.is_valid():
117 form.save()
118
119 # Mot de passe pour LDAP
120 username = request.user.email
121 authldap, created = AuthLDAP.objects.get_or_create(username=username)
122 password = form.cleaned_data.get('new_password1')
123 ldap_hash = create_ldap_hash(password)
124 authldap.ldap_hash = ldap_hash
125 authldap.save()
126
127 return HttpResponseRedirect(post_change_redirect)
128 else:
129 form = password_change_form(user=request.user)
130 return render_to_response(template_name, {
131 'form': form,
132 }, context_instance=RequestContext(request))
133
134
518d0b44 135@chercheur_required
8baa2a56
EMS
136def desinscription(request):
137 """Désinscription du chercheur"""
518d0b44 138 chercheur = request.chercheur
8baa2a56
EMS
139 if request.method == 'POST':
140 if request.POST.get('confirmer'):
13ec4813
EMS
141 chercheur.actif = False
142 chercheur.save()
8baa2a56
EMS
143 request.flash['message'] = "Vous avez été désinscrit du répertoire des chercheurs."
144 return HttpResponseRedirect(url('django.contrib.auth.views.logout'))
145 else:
146 request.flash['message'] = "Opération annulée."
147 return HttpResponseRedirect(url('chercheurs.views.perso'))
148 return render_to_response("chercheurs/desinscription.html", {},
149 context_instance=RequestContext(request))
150
518d0b44 151@chercheur_required
a7b16ec9 152@never_cache
f0692c02 153def edit(request):
b3e1079e 154 """Edition d'un chercheur"""
518d0b44 155 chercheur = request.chercheur
b3e1079e 156 if request.method == 'POST':
a7b16ec9
EMS
157 forms = ChercheurFormGroup(request.POST, chercheur=chercheur)
158 if forms.is_valid():
159 forms.save()
91112855
EMS
160 request.flash['message'] = "Votre fiche a bien été enregistrée."
161 return HttpResponseRedirect(url('chercheurs.views.perso'))
b3e1079e 162 else:
a7b16ec9 163 forms = ChercheurFormGroup(chercheur=chercheur)
b3e1079e 164
a7b16ec9
EMS
165 return render_to_response("chercheurs/edit.html",
166 dict(forms=forms, chercheur=chercheur),
595ab4d6 167 context_instance=RequestContext(request))
b3e1079e 168
518d0b44 169@chercheur_required
f0692c02 170def perso(request):
0d9d1c4d 171 """Espace chercheur (espace personnel du chercheur)"""
518d0b44 172 chercheur = request.chercheur
2a36714f 173 modification = request.GET.get('modification')
e4d01d1d
EMS
174 return render_to_response("chercheurs/perso.html",
175 dict(chercheur=chercheur, modification=modification),
518d0b44 176 context_instance=RequestContext(request))
da091176 177
f0692c02 178def retrieve(request, id):
da091176 179 """Fiche du chercheur"""
3eb00212 180 chercheur = get_object_or_404(Chercheur, id=id)
e4d01d1d
EMS
181 return render_to_response("chercheurs/retrieve.html",
182 dict(chercheur=chercheur),
c1b134f8 183 context_instance=RequestContext(request))
d9885bf7 184
f0692c02 185def conversion(request):
c1b134f8
EMS
186 return render_to_response("chercheurs/conversion.html", {},
187 context_instance=RequestContext(request))
8baa2a56 188
e836f6f7 189def etablissements_autocomplete(request, pays=None):
219710da 190 term = request.GET.get('term')
e76f8899 191 noms = Etablissement.objects.all().filter(membre=True, actif=True)
e836f6f7
EMS
192 for word in term.split():
193 noms = noms.filter(nom__icontains=word)
194 if pays:
195 noms = noms.filter(pays=pays)
196 noms = list(noms.values_list('nom', flat=True)[:20])
219710da
EMS
197 json = simplejson.dumps(noms)
198 return HttpResponse(json, mimetype='application/json')
fa6a2a07
EMS
199
200def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME):
201 "The Django login view, but using a custom form."
202 redirect_to = request.REQUEST.get(redirect_field_name, '')
203
204 if request.method == "POST":
205 form = AuthenticationForm(data=request.POST)
206 if form.is_valid():
207 # Light security check -- make sure redirect_to isn't garbage.
208 if not redirect_to or ' ' in redirect_to:
209 redirect_to = settings.LOGIN_REDIRECT_URL
210
211 # Heavier security check -- redirects to http://example.com should
212 # not be allowed, but things like /view/?param=http://example.com
213 # should be allowed. This regex checks if there is a '//' *before* a
214 # question mark.
215 elif '//' in redirect_to and re.match(r'[^\?]*//', redirect_to):
216 redirect_to = settings.LOGIN_REDIRECT_URL
217
544dec4f
PP
218 # Mot de passe pour LDAP
219 username = form.cleaned_data.get('username')
220 authldap, created = AuthLDAP.objects.get_or_create(username=username)
221 if created:
222 password = form.cleaned_data.get('password')
223 ldap_hash = create_ldap_hash(password)
224 authldap.ldap_hash = ldap_hash
225 authldap.save()
226
fa6a2a07
EMS
227 # Okay, security checks complete. Log the user in.
228 auth_login(request, form.get_user())
229
230 if request.session.test_cookie_worked():
231 request.session.delete_test_cookie()
232
233 return HttpResponseRedirect(redirect_to)
234
235 else:
236 form = AuthenticationForm(request)
237 request.session.set_test_cookie()
238
239 if Site._meta.installed:
240 current_site = Site.objects.get_current()
241 else:
242 current_site = RequestSite(request)
243
244 return render_to_response(template_name, {
245 'form': form,
246 redirect_field_name: redirect_to,
247 'site': current_site,
248 'site_name': current_site.name,
249 }, context_instance=RequestContext(request))
250login = never_cache(login)
cdaadee3
PP
251
252# groupes
253def groupe_index(request):
254 search_form = GroupeSearchForm(request.GET)
255 search = search_form.save(commit=False)
256 groupes = search.run()
257 nb_resultats = groupes.count()
258 try:
259 p = PageStatique.objects.get(id='groupes')
260 entete = p.contenu
261 except PageStatique.DoesNotExist:
381cc09a
PP
262 entete = '<h1>Liste des groupes</h1>'
263
96316da7 264 est_chercheur, mesgroupes, messages = False, None, None
381cc09a 265 if request.user.is_authenticated():
6115dba0
PP
266 try:
267 chercheur = Chercheur.objects.get(courriel=request.user.email)
775a0f93 268 mesgroupes = chercheur.groupes.filter(membership__statut='accepte').filter(groupe_chercheur=True)
96316da7
PP
269 messages = Message.objects.all().filter(groupe__in=mesgroupes)[:10]
270 est_chercheur = True
6115dba0 271 except Chercheur.DoesNotExist:
96316da7
PP
272 pass
273
274 return render_to_response("chercheurs/groupe_index.html", {
275 'search_form': search_form,
276 'groupes': groupes.order_by('nom'),
277 'nb_resultats': nb_resultats,
278 'entete': entete,
279 'mesgroupes': mesgroupes,
280 'messages': messages,
281 'est_chercheur': est_chercheur,
282 }, context_instance=RequestContext(request))
cdaadee3 283
0eb2476e
PP
284def groupe_adhesion(request, id):
285 try:
286 groupe = get_object_or_404(Groupe, id=id)
287 chercheur = Chercheur.objects.get(courriel=request.user.email)
18407f73 288 adhesion, created = AdhesionGroupe.objects.get_or_create(chercheur=chercheur, groupe=groupe)
0eb2476e 289 if created:
18407f73
PP
290 adhesion.actif = 0
291 adhesion.save()
0eb2476e
PP
292 except:
293 pass
294
295 return HttpResponseRedirect(url('groupe_retrieve', kwargs={'id': id}))
296
cdaadee3
PP
297def groupe_retrieve(request, id):
298 groupe = get_object_or_404(Groupe, id=id)
61c05759 299 membres = groupe.membership.all().filter(statut='accepte').order_by('-date_modification')
ddf7e62e
PP
300 plus_que_20 = True if membres.count() > 20 else False
301 membres_20 = membres[:20]
fd6352ea 302 messages = groupe.message_set.all()[:5]
0c0d997c 303
0eb2476e
PP
304 est_chercheur, est_membre, est_membre_actif = False, False, False
305 if request.user.is_authenticated():
306 try:
307 chercheur = Chercheur.objects.get(courriel=request.user.email)
308 est_chercheur = True
309 est_membre = chercheur in groupe.membres.all()
61c05759 310 est_membre_actif = bool(len(groupe.membership.filter(chercheur=chercheur, statut='accepte')))
0eb2476e
PP
311 except Chercheur.DoesNotExist:
312 pass
313
cdaadee3 314 return render_to_response(
0c0d997c
PP
315 "chercheurs/groupe_retrieve.html", {
316 'groupe': groupe,
ddf7e62e
PP
317 'membres': membres_20,
318 'plus_que_20': plus_que_20,
c8d6b979 319 'messages': messages,
0eb2476e
PP
320 'est_chercheur': est_chercheur,
321 'est_membre': est_membre,
322 'est_membre_actif': est_membre_actif,
0c0d997c 323 }, context_instance=RequestContext(request)
cdaadee3 324 )
c8d6b979 325
ddf7e62e
PP
326def groupe_membres(request, id):
327 groupe = get_object_or_404(Groupe, id=id)
61c05759 328 membres = groupe.membership.all().filter(statut='accepte').order_by('chercheur__nom')
ddf7e62e
PP
329
330 return render_to_response(
331 "chercheurs/groupe_membres.html", {
332 'groupe': groupe,
333 'membres': membres,
334 }, context_instance=RequestContext(request)
335 )
336
c8d6b979 337def groupe_messages(request, id):
fd6352ea
PP
338
339 groupe = get_object_or_404(Groupe, id=id)
340
9aa1d783
PP
341 est_chercheur, est_membre, est_membre_actif = False, False, False
342 if request.user.is_authenticated():
343 try:
344 chercheur = Chercheur.objects.get(courriel=request.user.email)
345 est_chercheur = True
346 est_membre = chercheur in groupe.membres.all()
61c05759 347 est_membre_actif = bool(len(groupe.membership.filter(chercheur=chercheur, statut='accepte')))
9aa1d783
PP
348 except Chercheur.DoesNotExist:
349 pass
350
351 if est_membre_actif and request.method == 'POST':
fd6352ea
PP
352 form = MessageForm(request.POST)
353 if form.is_valid():
fd6352ea
PP
354 message = form.save(commit=False)
355 message.groupe = groupe
356 message.chercheur = chercheur
357 message.save()
358
359 form = MessageForm()
360
361 else:
362 form = MessageForm()
363
364 messages = groupe.message_set.all()
365
366 return render_to_response(
367 "chercheurs/groupe_message.html", {
368 'groupe': groupe,
369 'messages': messages,
370 'form': form,
9aa1d783
PP
371 'est_chercheur': est_chercheur,
372 'est_membre': est_membre,
373 'est_membre_actif': est_membre_actif,
fd6352ea
PP
374 }, context_instance=RequestContext(request)
375 )