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