fix DRH peut mettre toutes les implantations
[auf_rh_dae.git] / project / recrutement / views.py
1 # -*- encoding: utf-8 -*-
2
3 from auf.django.emploi import models as emploi
4 from django.contrib import messages
5 from django.core.mail import EmailMultiAlternatives
6 from django.core.urlresolvers import reverse
7 from django.http import HttpResponseRedirect
8 from django.shortcuts import render, redirect
9 from django.template import Context, Template
10 from django.views.static import serve
11
12 from project.decorators import redirect_interdiction
13 from project.recrutement import forms
14 from project.recrutement import models
15 from project.recrutement.groups import recrutement_groupes
16
17
18 # MEDIA PRIVE
19 def 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
26 for grp in [g.name for g in user.groups.all()]:
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)
35
36
37 def index(request):
38 return render(request, 'recrutement/index.html')
39
40
41 def selectionner_template(request):
42 candidat_ids = request.GET.get('ids')
43 if request.method == "POST":
44 form = forms.CandidatCourrielTemplateForm(request.POST)
45 if form.is_valid():
46 form.save()
47 courriel_template_id = form.get_template()
48
49 return HttpResponseRedirect(
50 reverse('envoyer_courriel_candidats') +
51 "?ids_cand=%s&id_temp=%s" %
52 (candidat_ids, courriel_template_id)
53 )
54 else:
55 form = forms.CandidatCourrielTemplateForm()
56
57 return render(request, "recrutement/selectionner_template.html", {
58 'form': form
59 })
60
61
62 def envoyer_courriel_candidats(request):
63 candidat_ids = request.GET.get('ids_cand').split(',')
64 candidats = models.Candidat.objects.filter(id__in=candidat_ids)
65 template_id = request.GET.get('id_temp')
66 template = models.CourrielTemplate.objects.get(id=template_id)
67
68 if request.method == "POST":
69 form = forms.CandidatCourrielForm(
70 request.POST, instance=template, candidats=candidats,
71 template=template
72 )
73
74 if form.is_valid():
75 form.save()
76 courriel_template = models.CourrielTemplate()
77 courriel_template.nom_modele = template.nom_modele
78 courriel_template.sujet = form.data['sujet']
79 courriel_template.plain_text = form.data['plain_text']
80 for cand in candidats:
81 send_templated_email(cand, courriel_template)
82 messages.add_message(
83 request, messages.SUCCESS,
84 "Le email a été envoyé aux candidats."
85 )
86 return redirect("admin:emploi_candidat_changelist")
87 else:
88 form = forms.CandidatCourrielForm(
89 candidats=candidats, template=template,
90 initial={
91 'sujet': template.sujet,
92 'plain_text': template.plain_text
93 }
94 )
95 return render(request, "recrutement/envoyer_courriel_candidats.html", {
96 'form': form
97 })
98
99
100 def affecter_evaluateurs_offre_emploi(request):
101 offre_emploi_ids = request.GET.get('ids').split(',')
102 offres_emploi = emploi.OffreEmploi.objects.filter(id__in=offre_emploi_ids)
103 if request.method == "POST":
104 form = forms.EvaluateurForm(request.POST, offres_emploi=offres_emploi)
105 if form.is_valid():
106 form.save()
107 messages.add_message(
108 request, messages.SUCCESS,
109 "Les évaluateurs ont été affectés aux offres d'emploi."
110 )
111 return redirect("admin:emploi_offreemploi_changelist")
112 else:
113 form = forms.EvaluateurForm(offres_emploi=offres_emploi)
114 return render(request, "recrutement/affecter_evaluateurs.html", {
115 'form': form
116 })
117
118
119 def send_templated_email(candidat, template):
120 from django.conf import settings
121 # Sujet
122 sujet_template = Template(template.sujet)
123 dict_sujet = {"offre_emploi": candidat.offre_emploi.nom}
124 sujet = Context(dict_sujet)
125 # Plain text
126 texte_template = Template(template.plain_text)
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 }
133 texte = Context(dict_texte)
134 if getattr(settings, 'MAILING_ACTIF', False):
135 dst_emails = [candidat.email]
136 else:
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 )
142 msg.send()
143
144
145 def candidat_pdf(request):
146 candidat_id = request.GET.get('id')
147 candidat = models.Candidat.objects.get(id=candidat_id)
148 return render(request, "recrutement/candidat_pdf.html", {
149 'candidat': candidat
150 })