Merge branch 'refactoring' into test
[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
OL
6from django.http import HttpResponseRedirect
7from django.shortcuts import render_to_response, redirect
32834000 8from django.template import Context, RequestContext, 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):
4ba84959
EMS
40 return render_to_response(
41 'recrutement/index.html', {}, RequestContext(request)
42 )
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
61 return render_to_response("recrutement/selectionner_template.html", {
62 'form': form
63 }, context_instance=RequestContext(request))
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 )
99
100 return render_to_response("recrutement/envoyer_courriel_candidats.html", {
101 'form': form
102 }, context_instance=RequestContext(request))
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
EMS
118 form = forms.EvaluateurForm(offres_emploi=offres_emploi)
119
120 return render_to_response("recrutement/affecter_evaluateurs.html", {
121 'form': form
122 }, context_instance=RequestContext(request))
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)
32c22f96 154
4ba84959
EMS
155 return render_to_response("recrutement/candidat_pdf.html", {
156 'candidat': candidat
157 }, context_instance=RequestContext(request))