Merge branch 'refactoring' of git.auf.org:auf_rh_dae into refactoring
[auf_rh_dae.git] / project / recrutement / views.py
1 # -*- encoding: utf-8 -*-
2
3 from django.contrib import messages
4 from django.core.mail import EmailMultiAlternatives
5 from django.core.urlresolvers import reverse
6 from django.http import HttpResponseRedirect
7 from django.shortcuts import render_to_response, redirect
8 from django.template import Context, RequestContext, Template
9 from django.views.static import serve
10
11 from auf.django.emploi import models as emploi
12
13 from project.dae.decorators import redirect_interdiction
14 from project.recrutement import forms
15 from project.recrutement import models
16 from project.recrutement.workflow import recrutement_groupes
17
18
19 # MEDIA PRIVE
20
21 def mediaserve(request, path, document_root=None, show_indexes=False):
22 """
23 Sécuriser l'accès aux fichiers uploadés
24 """
25 grant_ok = False
26 user = request.user
27
28 for grp in user.groups.all():
29 if grp in recrutement_groupes:
30 grant_ok = True
31 break
32
33 if not grant_ok:
34 return redirect_interdiction(request)
35
36 return serve(request, path, document_root, show_indexes)
37
38
39 def index(request):
40 return render_to_response(
41 'recrutement/index.html', {}, RequestContext(request)
42 )
43
44
45 def selectionner_template(request):
46 candidat_ids = request.GET.get('ids')
47 if request.method == "POST":
48 form = forms.CandidatCourrielTemplateForm(request.POST)
49 if form.is_valid():
50 form.save()
51 courriel_template_id = form.get_template()
52
53 return HttpResponseRedirect(
54 reverse('envoyer_courriel_candidats') +
55 "?ids_cand=%s&id_temp=%s" %
56 (candidat_ids, courriel_template_id)
57 )
58 else:
59 form = forms.CandidatCourrielTemplateForm()
60
61 return render_to_response("recrutement/selectionner_template.html", {
62 'form': form
63 }, context_instance=RequestContext(request))
64
65
66 def envoyer_courriel_candidats(request):
67 candidat_ids = request.GET.get('ids_cand').split(',')
68 candidats = models.Candidat.objects.filter(id__in=candidat_ids)
69 template_id = request.GET.get('id_temp')
70 template = models.CourrielTemplate.objects.get(id=template_id)
71
72 if request.method == "POST":
73 form = forms.CandidatCourrielForm(
74 request.POST, instance=template, candidats=candidats,
75 template=template
76 )
77
78 if form.is_valid():
79 form.save()
80 courriel_template = models.CourrielTemplate()
81 courriel_template.nom_modele = template.nom_modele
82 courriel_template.sujet = form.data['sujet']
83 courriel_template.plain_text = form.data['plain_text']
84 for cand in candidats:
85 send_templated_email(cand, courriel_template)
86 messages.add_message(
87 request, messages.SUCCESS,
88 "Le email a été envoyé aux candidats."
89 )
90 return redirect("admin:recrutement_candidat_changelist")
91 else:
92 form = forms.CandidatCourrielForm(
93 candidats=candidats, template=template,
94 initial={
95 'sujet': template.sujet,
96 'plain_text': template.plain_text
97 }
98 )
99
100 return render_to_response("recrutement/envoyer_courriel_candidats.html", {
101 'form': form
102 }, context_instance=RequestContext(request))
103
104
105 def affecter_evaluateurs_offre_emploi(request):
106 offre_emploi_ids = request.GET.get('ids').split(',')
107 offres_emploi = emploi.OffreEmploi.objects.filter(id__in=offre_emploi_ids)
108 if request.method == "POST":
109 form = forms.EvaluateurForm(request.POST, offres_emploi=offres_emploi)
110 if form.is_valid():
111 form.save()
112 messages.add_message(
113 request, messages.SUCCESS,
114 "Les évaluateurs ont été affectés aux offres d'emploi."
115 )
116 return redirect("admin:recrutement_proxyoffreemploi_changelist")
117 else:
118 form = forms.EvaluateurForm(offres_emploi=offres_emploi)
119
120 return render_to_response("recrutement/affecter_evaluateurs.html", {
121 'form': form
122 }, context_instance=RequestContext(request))
123
124
125 def send_templated_email(candidat, template):
126 from django.conf import settings
127 # Sujet
128 sujet_template = Template(template.sujet)
129 dict_sujet = {"offre_emploi": candidat.offre_emploi.nom}
130 sujet = Context(dict_sujet)
131 # Plain text
132 texte_template = Template(template.plain_text)
133 dict_texte = {
134 "nom_candidat": candidat.nom,
135 "prenom_candidat": candidat.prenom,
136 "offre_emploi": candidat.offre_emploi.nom,
137 "genre_candidat": "Monsieur" if candidat.genre == "M" else "Madame",
138 }
139 texte = Context(dict_texte)
140 if getattr(settings, 'MAILING_ACTIF', False):
141 dst_emails = [candidat.email]
142 else:
143 dst_emails = ['developpeurs@ca.auf.org']
144 msg = EmailMultiAlternatives(
145 sujet_template.render(sujet), texte_template.render(texte),
146 'recrutement@auf.org', dst_emails
147 )
148 msg.send()
149
150
151 def candidat_pdf(request):
152 candidat_id = request.GET.get('id')
153 candidat = models.Candidat.objects.get(id=candidat_id)
154
155 return render_to_response("recrutement/candidat_pdf.html", {
156 'candidat': candidat
157 }, context_instance=RequestContext(request))