render_to_response -> render
[auf_rh_dae.git] / project / recrutement / views.py
CommitLineData
6f2d5d58 1# -*- encoding: utf-8 -*-
89e948de 2
76b89254 3from django.contrib import messages
75f0e87b
DB
4from django.core.mail import EmailMultiAlternatives
5from django.core.urlresolvers import reverse
940c9dd8 6from django.http import HttpResponseRedirect
890b80e7
DB
7from django.shortcuts import render, redirect
8from django.template import Context, Template
75f0e87b 9from django.views.static import serve
6f2d5d58 10
e949324b 11from auf.django.emploi import models as emploi
6f2d5d58 12
4ba84959
EMS
13from project.dae.decorators import redirect_interdiction
14from project.recrutement import forms
15from project.recrutement import models
16from project.recrutement.workflow import recrutement_groupes
17
7f25c8a6 18
7f25c8a6 19# MEDIA PRIVE
7f25c8a6
OL
20
21def 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)
6f2d5d58 37
4ba84959 38
6f2d5d58 39def index(request):
890b80e7
DB
40 c = {}
41 return render(request, 'recrutement/index.html', c)
4ba84959 42
362a3534 43
32834000
NBV
44def selectionner_template(request):
45 candidat_ids = request.GET.get('ids')
46 if request.method == "POST":
4ba84959 47 form = forms.CandidatCourrielTemplateForm(request.POST)
32834000
NBV
48 if form.is_valid():
49 form.save()
50 courriel_template_id = form.get_template()
4ba84959
EMS
51
52 return HttpResponseRedirect(
53 reverse('envoyer_courriel_candidats') +
54 "?ids_cand=%s&id_temp=%s" %
55 (candidat_ids, courriel_template_id)
56 )
32834000 57 else:
4ba84959
EMS
58 form = forms.CandidatCourrielTemplateForm()
59
890b80e7
DB
60 c = {
61 'form': form,
62 }
63 return render(request, "recrutement/selectionner_template.html", c)
32834000 64
32834000 65
d84c3a68 66def envoyer_courriel_candidats(request):
32834000 67 candidat_ids = request.GET.get('ids_cand').split(',')
4ba84959 68 candidats = models.Candidat.objects.filter(id__in=candidat_ids)
32834000 69 template_id = request.GET.get('id_temp')
4ba84959 70 template = models.CourrielTemplate.objects.get(id=template_id)
4e8340cf 71
d84c3a68 72 if request.method == "POST":
4ba84959
EMS
73 form = forms.CandidatCourrielForm(
74 request.POST, instance=template, candidats=candidats,
75 template=template
76 )
32834000 77
d84c3a68 78 if form.is_valid():
79 form.save()
4ba84959 80 courriel_template = models.CourrielTemplate()
32834000
NBV
81 courriel_template.nom_modele = template.nom_modele
82 courriel_template.sujet = form.data['sujet']
83 courriel_template.plain_text = form.data['plain_text']
4ba84959 84 for cand in candidats:
ba70513a 85 send_templated_email(cand, courriel_template)
4ba84959
EMS
86 messages.add_message(
87 request, messages.SUCCESS,
88 "Le email a été envoyé aux candidats."
89 )
d84c3a68 90 return redirect("admin:recrutement_candidat_changelist")
91 else:
4ba84959
EMS
92 form = forms.CandidatCourrielForm(
93 candidats=candidats, template=template,
94 initial={
95 'sujet': template.sujet,
96 'plain_text': template.plain_text
97 }
98 )
890b80e7
DB
99 c = {
100 'form': form,
101 }
102 return render(request, "recrutement/envoyer_courriel_candidats.html", c)
d84c3a68 103
d84c3a68 104
514db699
NBV
105def affecter_evaluateurs_offre_emploi(request):
106 offre_emploi_ids = request.GET.get('ids').split(',')
4ba84959 107 offres_emploi = emploi.OffreEmploi.objects.filter(id__in=offre_emploi_ids)
514db699 108 if request.method == "POST":
4ba84959 109 form = forms.EvaluateurForm(request.POST, offres_emploi=offres_emploi)
514db699
NBV
110 if form.is_valid():
111 form.save()
4ba84959
EMS
112 messages.add_message(
113 request, messages.SUCCESS,
114 "Les évaluateurs ont été affectés aux offres d'emploi."
115 )
940c9dd8 116 return redirect("admin:recrutement_proxyoffreemploi_changelist")
514db699 117 else:
4ba84959 118 form = forms.EvaluateurForm(offres_emploi=offres_emploi)
890b80e7
DB
119 c = {
120 'form': form,
121 }
122 return render(request, "recrutement/affecter_evaluateurs.html", c)
514db699 123
b8b74dee
NBV
124
125def send_templated_email(candidat, template):
76b89254 126 from django.conf import settings
b8b74dee
NBV
127 # Sujet
128 sujet_template = Template(template.sujet)
4ba84959 129 dict_sujet = {"offre_emploi": candidat.offre_emploi.nom}
b8b74dee
NBV
130 sujet = Context(dict_sujet)
131 # Plain text
132 texte_template = Template(template.plain_text)
4ba84959
EMS
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 }
b8b74dee 139 texte = Context(dict_texte)
c9cf286d 140 if getattr(settings, 'MAILING_ACTIF', False):
4ba84959 141 dst_emails = [candidat.email]
c9cf286d 142 else:
4ba84959
EMS
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 )
b8b74dee 148 msg.send()
32c22f96
NBV
149
150
151def candidat_pdf(request):
152 candidat_id = request.GET.get('id')
4ba84959 153 candidat = models.Candidat.objects.get(id=candidat_id)
890b80e7
DB
154 c = {
155 'candidat': candidat,
156 }
157 return render(request, "recrutement/candidat_pdf.html", c)