b4c8bc760ca752e3245606c9be82623d01046334
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / chercheurs / views.py
1 # -*- encoding: utf-8 -*-
2 from chercheurs.decorators import chercheur_required
3 from chercheurs.forms import RepertoireSearchForm, SetPasswordForm, ChercheurFormGroup
4 from chercheurs.models import Chercheur
5 from chercheurs.utils import get_django_user_for_email
6 from datamaster_modeles.models import Etablissement
7 from django.shortcuts import render_to_response
8 from django.http import HttpResponseRedirect, HttpResponse
9 from django.template import Context, RequestContext
10 from django.template.loader import get_template
11 from django.core.urlresolvers import reverse as url
12 from django.core.mail import send_mail
13 from django.contrib.sites.models import RequestSite
14 from django.utils import simplejson
15 from django.utils.http import int_to_base36, base36_to_int
16 from django.views.decorators.cache import never_cache
17 from django.contrib.auth import authenticate, login
18 from django.shortcuts import get_object_or_404
19 from savoirs.models import PageStatique
20
21 def index(request):
22 """Répertoire des chercheurs"""
23 search_form = RepertoireSearchForm(request.GET)
24 chercheurs = search_form.get_query_set().select_related('etablissement')
25 sort = request.GET.get('tri')
26 if sort is not None and sort.endswith('_desc'):
27 sort = sort[:-5]
28 direction = '-'
29 else:
30 direction = ''
31 if sort == 'nom':
32 chercheurs = chercheurs.order_by_nom(direction)
33 elif sort == 'etablissement':
34 chercheurs = chercheurs.order_by_etablissement(direction)
35 elif sort == 'pays':
36 chercheurs = chercheurs.order_by_pays(direction)
37 else:
38 chercheurs = chercheurs.order_by('-date_modification')
39
40 try:
41 p = PageStatique.objects.get(id='repertoire')
42 entete = p.contenu
43 except PageStatique.DoesNotExist:
44 entete = u'<h1>Répertoire des chercheurs</h1>'
45
46 nb_chercheurs = chercheurs.count()
47 return render_to_response("chercheurs/index.html",
48 dict(chercheurs=chercheurs, nb_chercheurs=nb_chercheurs,
49 search_form=search_form, entete=entete),
50 context_instance=RequestContext(request))
51
52 def inscription(request):
53 if request.method == 'POST':
54 forms = ChercheurFormGroup(request.POST)
55 if forms.is_valid():
56 chercheur = forms.save()
57 id_base36 = int_to_base36(chercheur.id)
58 token = chercheur.activation_token()
59 template = get_template('chercheurs/activation_email.txt')
60 domain = RequestSite(request).domain
61 message = template.render(Context(dict(chercheur=chercheur, id_base36=id_base36, token=token, domain=domain)))
62 send_mail('Votre inscription à Savoirs en partage', message, None, [chercheur.courriel])
63 return HttpResponseRedirect(url('chercheurs-inscription-faite'))
64 else:
65 forms = ChercheurFormGroup()
66
67 return render_to_response("chercheurs/inscription.html",
68 dict(forms=forms),
69 context_instance=RequestContext(request))
70
71 def activation(request, id_base36, token):
72 """Activation d'un chercheur"""
73 id = base36_to_int(id_base36)
74 chercheur = get_object_or_404(Chercheur, id=id)
75 if token == chercheur.activation_token():
76 validlink = True
77 if request.method == 'POST':
78 form = SetPasswordForm(request.POST)
79 if form.is_valid():
80 password = form.cleaned_data['password']
81 email = chercheur.courriel
82 chercheur.actif = True
83 chercheur.save()
84 user = get_django_user_for_email(email)
85 user.set_password(password)
86 user.save()
87
88 # Auto-login
89 login(request, authenticate(username=email, password=password))
90 return HttpResponseRedirect(url('chercheurs.views.perso'))
91 else:
92 form = SetPasswordForm()
93 else:
94 form = None
95 validlink = False
96 return render_to_response('chercheurs/activation.html', dict(form=form, validlink=validlink),
97 context_instance=RequestContext(request))
98
99 @chercheur_required
100 def desinscription(request):
101 """Désinscription du chercheur"""
102 chercheur = request.chercheur
103 if request.method == 'POST':
104 if request.POST.get('confirmer'):
105 chercheur.actif = False
106 chercheur.save()
107 request.flash['message'] = "Vous avez été désinscrit du répertoire des chercheurs."
108 return HttpResponseRedirect(url('django.contrib.auth.views.logout'))
109 else:
110 request.flash['message'] = "Opération annulée."
111 return HttpResponseRedirect(url('chercheurs.views.perso'))
112 return render_to_response("chercheurs/desinscription.html", {},
113 context_instance=RequestContext(request))
114
115 @chercheur_required
116 @never_cache
117 def edit(request):
118 """Edition d'un chercheur"""
119 chercheur = request.chercheur
120 if request.method == 'POST':
121 forms = ChercheurFormGroup(request.POST, chercheur=chercheur)
122 if forms.is_valid():
123 forms.save()
124 request.flash['message'] = "Votre fiche a bien été enregistrée."
125 return HttpResponseRedirect(url('chercheurs.views.perso'))
126 else:
127 forms = ChercheurFormGroup(chercheur=chercheur)
128
129 return render_to_response("chercheurs/edit.html",
130 dict(forms=forms, chercheur=chercheur),
131 context_instance=RequestContext(request))
132
133 @chercheur_required
134 def perso(request):
135 """Espace chercheur (espace personnel du chercheur)"""
136 chercheur = request.chercheur
137 modification = request.GET.get('modification')
138 return render_to_response("chercheurs/perso.html",
139 dict(chercheur=chercheur, modification=modification),
140 context_instance=RequestContext(request))
141
142 def retrieve(request, id):
143 """Fiche du chercheur"""
144 chercheur = get_object_or_404(Chercheur, id=id)
145 return render_to_response("chercheurs/retrieve.html",
146 dict(chercheur=chercheur),
147 context_instance=RequestContext(request))
148
149 def conversion(request):
150 return render_to_response("chercheurs/conversion.html", {},
151 context_instance=RequestContext(request))
152
153 def etablissements_autocomplete(request, pays=None):
154 term = request.GET.get('term')
155 noms = Etablissement.objects.all().filter(membre=True, actif=True)
156 for word in term.split():
157 noms = noms.filter(nom__icontains=word)
158 if pays:
159 noms = noms.filter(pays=pays)
160 noms = list(noms.values_list('nom', flat=True)[:20])
161 json = simplejson.dumps(noms)
162 return HttpResponse(json, mimetype='application/json')