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