fix
[auf_rh_dae.git] / project / recrutement / views.py
CommitLineData
6f2d5d58 1# -*- encoding: utf-8 -*-
89e948de 2
32834000 3from django.core.urlresolvers import reverse
76b89254 4from django.contrib import messages
7f25c8a6 5from django.views.static import serve
940c9dd8
OL
6from django.http import HttpResponseRedirect
7from django.shortcuts import render_to_response, redirect
32834000
NBV
8from django.template import Context, RequestContext, Template
9from django.core.mail import EmailMultiAlternatives
6f2d5d58 10
362a3534 11from forms import *
12from models import *
940c9dd8 13from recrutement.workflow import recrutement_groupes
7f25c8a6
OL
14
15################################################################################
16# MEDIA PRIVE
17################################################################################
18
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
26 for grp 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)
6f2d5d58 35
36def index(request):
362a3534 37 return render_to_response('recrutement/index.html', {},
38 RequestContext(request))
39
32834000
NBV
40def selectionner_template(request):
41 candidat_ids = request.GET.get('ids')
42 if request.method == "POST":
43 form = CandidatCourrielTemplateForm(request.POST)
44 if form.is_valid():
45 form.save()
46 courriel_template_id = form.get_template()
47
48 return HttpResponseRedirect(reverse('envoyer_courriel_candidats')+
dc7faf2b 49 "?ids_cand=%s&id_temp=%s" % (candidat_ids, courriel_template_id))
32834000
NBV
50 else:
51 form = CandidatCourrielTemplateForm()
52
53 c = {'form' : form}
54 return render_to_response("recrutement/selectionner_template.html",
55 Context(c), context_instance = RequestContext(request))
56
d84c3a68 57def envoyer_courriel_candidats(request):
32834000 58 candidat_ids = request.GET.get('ids_cand').split(',')
d84c3a68 59 candidats = Candidat.objects.filter(id__in=candidat_ids)
32834000
NBV
60 template_id = request.GET.get('id_temp')
61 template = CourrielTemplate.objects.get(id=template_id)
4e8340cf 62
d84c3a68 63 if request.method == "POST":
32834000
NBV
64 form = CandidatCourrielForm(request.POST, instance=template,
65 candidats=candidats, template=template)
66
d84c3a68 67 if form.is_valid():
68 form.save()
32834000
NBV
69 courriel_template = CourrielTemplate()
70 courriel_template.nom_modele = template.nom_modele
71 courriel_template.sujet = form.data['sujet']
72 courriel_template.plain_text = form.data['plain_text']
32834000 73 for cand in candidats:
ba70513a 74 send_templated_email(cand, courriel_template)
d84c3a68 75 messages.add_message(request, messages.SUCCESS,
32834000 76 "Le email a été envoyé aux candidats.")
d84c3a68 77 return redirect("admin:recrutement_candidat_changelist")
78 else:
32834000
NBV
79 form = CandidatCourrielForm(candidats=candidats, template=template,
80 initial={'sujet': template.sujet,
fe6fe6bb 81 'plain_text': template.plain_text,})
d84c3a68 82
83 c = {'form' : form}
84 return render_to_response("recrutement/envoyer_courriel_candidats.html",
85 Context(c), context_instance = RequestContext(request))
86
514db699
NBV
87def affecter_evaluateurs_offre_emploi(request):
88 offre_emploi_ids = request.GET.get('ids').split(',')
89 offres_emploi = OffreEmploi.objects.filter(id__in=offre_emploi_ids)
514db699 90 if request.method == "POST":
540dfae4 91 form = EvaluateurForm(request.POST, offres_emploi=offres_emploi)
514db699
NBV
92 if form.is_valid():
93 form.save()
94 messages.add_message(request, messages.SUCCESS,
540dfae4 95 "Les évaluateurs ont été affectés aux offres d'emploi.")
940c9dd8 96 return redirect("admin:recrutement_proxyoffreemploi_changelist")
514db699 97 else:
540dfae4 98 form = EvaluateurForm(offres_emploi=offres_emploi)
514db699
NBV
99
100 c = {'form' : form}
101 return render_to_response("recrutement/affecter_evaluateurs.html",
102 Context(c), context_instance = RequestContext(request))
b8b74dee
NBV
103
104def send_templated_email(candidat, template):
76b89254 105 from django.conf import settings
b8b74dee
NBV
106 # Sujet
107 sujet_template = Template(template.sujet)
108 dict_sujet = {"offre_emploi": candidat.offre_emploi.nom,}
109 sujet = Context(dict_sujet)
110 # Plain text
111 texte_template = Template(template.plain_text)
112 dict_texte = {"nom_candidat": candidat.nom,
113 "prenom_candidat": candidat.prenom,
114 "offre_emploi": candidat.offre_emploi.nom,
115 "genre_candidat": "Monsieur" if candidat.genre == "M" \
116 else "Madame",
117 }
118 texte = Context(dict_texte)
c9cf286d 119 if getattr(settings, 'MAILING_ACTIF', False):
76b89254 120 dst_emails = [candidat.email, ]
c9cf286d
OL
121 else:
122 dst_emails = ['developpeurs@ca.auf.org', ]
b8b74dee
NBV
123 msg = EmailMultiAlternatives(sujet_template.render(sujet),
124 texte_template.render(texte),
125 'recrutement@auf.org',
76b89254
OL
126 dst_emails,
127 )
b8b74dee 128 msg.send()
32c22f96
NBV
129
130
131def candidat_pdf(request):
132 candidat_id = request.GET.get('id')
133 candidat = Candidat.objects.get(id=candidat_id)
134
135 return render_to_response("recrutement/candidat_pdf.html",
136 Context({'candidat' : candidat}), context_instance = RequestContext(request))