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