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