1e980534a9ee8b900fd0378dc807f8c0706e4750
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / views.py
1 # -*- encoding: utf-8 -*-
2 import hashlib
3 from chercheurs.decorators import chercheur_required
4 from django.shortcuts import render_to_response
5 from django.http import HttpResponseRedirect, HttpResponse
6 from django.template import Context, RequestContext
7 from django.template.loader import get_template
8 from django.core.urlresolvers import reverse as url
9 from django.core.mail import send_mail
10 from django.conf import settings
11 from django.utils import simplejson
12 from django.views.decorators.cache import never_cache
13
14 from forms import *
15 from django.forms.models import inlineformset_factory
16
17 from auf_references_client.models import Discipline, TypeImplantation
18 from models import Personne, Groupe, ChercheurGroupe
19
20 from django.contrib.auth import authenticate, login
21 from django.contrib.auth.decorators import login_required
22 from django.contrib.auth.models import User
23
24 from django.db.models import Q
25 from django.shortcuts import get_object_or_404
26
27 from django.utils.translation import ugettext_lazy as _
28
29 def index(request):
30 """Répertoire des chercheurs"""
31 search_form = RepertoireSearchForm(request.GET)
32 chercheurs = search_form.get_query_set().select_related('etablissement')
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':
40 chercheurs = chercheurs.order_by_nom(direction)
41 elif sort == 'etablissement':
42 chercheurs = chercheurs.order_by_etablissement(direction)
43 elif sort == 'pays':
44 chercheurs = chercheurs.order_by_pays(direction)
45 else:
46 chercheurs = chercheurs.order_by('-date_modification')
47
48 nb_chercheurs = chercheurs.count()
49 return render_to_response("chercheurs/index.html",
50 dict(chercheurs=chercheurs, nb_chercheurs=nb_chercheurs, search_form=search_form),
51 context_instance=RequestContext(request))
52
53 def inscription(request):
54 if request.method == 'POST':
55 forms = ChercheurFormGroup(request.POST)
56 if forms.is_valid():
57 forms.save()
58 # login automatique
59 login(request, authenticate(username=forms.chercheur.cleaned_data['courriel'],
60 password=forms.chercheur.cleaned_data['password']))
61 return HttpResponseRedirect(url('chercheurs.views.perso'))
62 else:
63 forms = ChercheurFormGroup()
64
65 return render_to_response("chercheurs/inscription.html",
66 dict(forms=forms),
67 context_instance=RequestContext(request))
68
69 @chercheur_required
70 def desinscription(request):
71 """Désinscription du chercheur"""
72 chercheur = request.chercheur
73 if request.method == 'POST':
74 if request.POST.get('confirmer'):
75 chercheur.actif = False
76 chercheur.save()
77 User.objects.filter(username=chercheur.courriel).delete()
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
86 @chercheur_required
87 @never_cache
88 def edit(request):
89 """Edition d'un chercheur"""
90 chercheur = request.chercheur
91 if request.method == 'POST':
92 forms = ChercheurFormGroup(request.POST, chercheur=chercheur)
93 if forms.is_valid():
94 forms.save()
95 return HttpResponseRedirect(url('chercheurs.views.perso') + '?modification=1')
96 else:
97 forms = ChercheurFormGroup(chercheur=chercheur)
98
99 return render_to_response("chercheurs/edit.html",
100 dict(forms=forms, chercheur=chercheur),
101 context_instance=RequestContext(request))
102
103 @chercheur_required
104 def perso(request):
105 """Espace chercheur (espace personnel du chercheur)"""
106 chercheur = request.chercheur
107 modification = request.GET.get('modification')
108 return render_to_response("chercheurs/perso.html",
109 dict(chercheur=chercheur, modification=modification),
110 context_instance=RequestContext(request))
111
112 def retrieve(request, id):
113 """Fiche du chercheur"""
114 chercheur = get_object_or_404(Chercheur, id=id)
115 return render_to_response("chercheurs/retrieve.html",
116 dict(chercheur=chercheur),
117 context_instance=RequestContext(request))
118
119 def conversion(request):
120 return render_to_response("chercheurs/conversion.html", {},
121 context_instance=RequestContext(request))
122
123 def etablissements_autocomplete(request, pays=None):
124 term = request.GET.get('term')
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])
131 json = simplejson.dumps(noms)
132 return HttpResponse(json, mimetype='application/json')