Typo
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / views.py
CommitLineData
588d6b93 1# -*- encoding: utf-8 -*-
5ecd9e43 2import hashlib
518d0b44 3from chercheurs.decorators import chercheur_required
932eef9a 4from django.shortcuts import render_to_response
0e9597af 5from django.http import HttpResponseRedirect, HttpResponse
932eef9a 6from django.template import Context, RequestContext
e427f068 7from django.template.loader import get_template
51515982 8from django.core.urlresolvers import reverse as url
0e9597af
AJ
9from django.core.mail import send_mail
10from django.conf import settings
219710da 11from django.utils import simplejson
a7b16ec9 12from django.views.decorators.cache import never_cache
e8e9e4fd 13
932eef9a 14from forms import *
e8e9e4fd 15from django.forms.models import inlineformset_factory
932eef9a
AJ
16
17from auf_references_client.models import Discipline, TypeImplantation
13ec4813 18from models import Personne, Groupe, ChercheurGroupe
932eef9a 19
72923b8e 20from django.contrib.auth import authenticate, login
9af73c99 21from django.contrib.auth.decorators import login_required
8baa2a56 22from django.contrib.auth.models import User
13146d99 23
510b5321 24from django.db.models import Q
3eb00212 25from django.shortcuts import get_object_or_404
510b5321 26
c18af6bd 27from django.utils.translation import ugettext_lazy as _
c18af6bd 28
f0692c02 29def index(request):
f8c16b3d 30 """Répertoire des chercheurs"""
f0692c02 31 search_form = RepertoireSearchForm(request.GET)
13ec4813 32 chercheurs = search_form.get_query_set().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')
47
9f7c169e 48 nb_chercheurs = chercheurs.count()
3efbacbe 49 return render_to_response("chercheurs/index.html",
e9b0d630 50 dict(chercheurs=chercheurs, nb_chercheurs=nb_chercheurs, search_form=search_form),
3efbacbe 51 context_instance=RequestContext(request))
588d6b93 52
f0692c02 53def inscription(request):
932eef9a 54 if request.method == 'POST':
a7b16ec9
EMS
55 forms = ChercheurFormGroup(request.POST)
56 if forms.is_valid():
57 forms.save()
58 # login automatique
13ec4813
EMS
59 login(request, authenticate(username=forms.chercheur.cleaned_data['courriel'],
60 password=forms.chercheur.cleaned_data['password']))
0539c4e2 61 return HttpResponseRedirect(url('chercheurs.views.perso'))
932eef9a 62 else:
a7b16ec9 63 forms = ChercheurFormGroup()
932eef9a 64
a7b16ec9
EMS
65 return render_to_response("chercheurs/inscription.html",
66 dict(forms=forms),
67 context_instance=RequestContext(request))
9af73c99 68
518d0b44 69@chercheur_required
8baa2a56
EMS
70def desinscription(request):
71 """Désinscription du chercheur"""
518d0b44 72 chercheur = request.chercheur
8baa2a56
EMS
73 if request.method == 'POST':
74 if request.POST.get('confirmer'):
13ec4813
EMS
75 chercheur.actif = False
76 chercheur.save()
77 User.objects.filter(username=chercheur.courriel).delete()
8baa2a56
EMS
78 request.flash['message'] = "Vous avez été désinscrit du répertoire des chercheurs."
79 return HttpResponseRedirect(url('django.contrib.auth.views.logout'))
80 else:
81 request.flash['message'] = "Opération annulée."
82 return HttpResponseRedirect(url('chercheurs.views.perso'))
83 return render_to_response("chercheurs/desinscription.html", {},
84 context_instance=RequestContext(request))
85
518d0b44 86@chercheur_required
a7b16ec9 87@never_cache
f0692c02 88def edit(request):
b3e1079e 89 """Edition d'un chercheur"""
518d0b44 90 chercheur = request.chercheur
b3e1079e 91 if request.method == 'POST':
a7b16ec9
EMS
92 forms = ChercheurFormGroup(request.POST, chercheur=chercheur)
93 if forms.is_valid():
94 forms.save()
0539c4e2 95 return HttpResponseRedirect(url('chercheurs.views.perso') + '?modification=1')
b3e1079e 96 else:
a7b16ec9 97 forms = ChercheurFormGroup(chercheur=chercheur)
b3e1079e 98
a7b16ec9
EMS
99 return render_to_response("chercheurs/edit.html",
100 dict(forms=forms, chercheur=chercheur),
595ab4d6 101 context_instance=RequestContext(request))
b3e1079e 102
518d0b44 103@chercheur_required
f0692c02 104def perso(request):
0d9d1c4d 105 """Espace chercheur (espace personnel du chercheur)"""
518d0b44 106 chercheur = request.chercheur
2a36714f 107 modification = request.GET.get('modification')
e4d01d1d
EMS
108 return render_to_response("chercheurs/perso.html",
109 dict(chercheur=chercheur, modification=modification),
518d0b44 110 context_instance=RequestContext(request))
da091176 111
f0692c02 112def retrieve(request, id):
da091176 113 """Fiche du chercheur"""
3eb00212 114 chercheur = get_object_or_404(Chercheur, id=id)
e4d01d1d
EMS
115 return render_to_response("chercheurs/retrieve.html",
116 dict(chercheur=chercheur),
c1b134f8 117 context_instance=RequestContext(request))
d9885bf7 118
f0692c02 119def conversion(request):
c1b134f8
EMS
120 return render_to_response("chercheurs/conversion.html", {},
121 context_instance=RequestContext(request))
8baa2a56 122
e836f6f7 123def etablissements_autocomplete(request, pays=None):
219710da 124 term = request.GET.get('term')
e836f6f7
EMS
125 noms = Etablissement.objects.all()
126 for word in term.split():
127 noms = noms.filter(nom__icontains=word)
128 if pays:
129 noms = noms.filter(pays=pays)
130 noms = list(noms.values_list('nom', flat=True)[:20])
219710da
EMS
131 json = simplejson.dumps(noms)
132 return HttpResponse(json, mimetype='application/json')