[#2658] Retrait du hack app_label() dans rh.models
[auf_rh_dae.git] / project / recrutement / views.py
CommitLineData
6f2d5d58 1# -*- encoding: utf-8 -*-
89e948de 2
4ba84959 3from auf.django.emploi import models as emploi
32834000 4from django.core.urlresolvers import reverse
76b89254 5from django.contrib import messages
7f25c8a6 6from django.views.static import serve
940c9dd8
OL
7from django.http import HttpResponseRedirect
8from django.shortcuts import render_to_response, redirect
32834000
NBV
9from django.template import Context, RequestContext, Template
10from django.core.mail import EmailMultiAlternatives
6f2d5d58 11
4ba84959
EMS
12from project.dae.decorators import redirect_interdiction
13from project.recrutement import forms
14from project.recrutement import models
15from project.recrutement.workflow import recrutement_groupes
16
7f25c8a6 17
7f25c8a6 18# MEDIA PRIVE
7f25c8a6
OL
19
20def mediaserve(request, path, document_root=None, show_indexes=False):
21 """
22 Sécuriser l'accès aux fichiers uploadés
23 """
24 grant_ok = False
25 user = request.user
26
27 for grp in user.groups.all():
28 if grp in recrutement_groupes:
29 grant_ok = True
30 break
31
32 if not grant_ok:
33 return redirect_interdiction(request)
34
35 return serve(request, path, document_root, show_indexes)
6f2d5d58 36
4ba84959 37
6f2d5d58 38def index(request):
4ba84959
EMS
39 return render_to_response(
40 'recrutement/index.html', {}, RequestContext(request)
41 )
42
362a3534 43
32834000
NBV
44def selectionner_template(request):
45 candidat_ids = request.GET.get('ids')
46 if request.method == "POST":
4ba84959 47 form = forms.CandidatCourrielTemplateForm(request.POST)
32834000
NBV
48 if form.is_valid():
49 form.save()
50 courriel_template_id = form.get_template()
4ba84959
EMS
51
52 return HttpResponseRedirect(
53 reverse('envoyer_courriel_candidats') +
54 "?ids_cand=%s&id_temp=%s" %
55 (candidat_ids, courriel_template_id)
56 )
32834000 57 else:
4ba84959
EMS
58 form = forms.CandidatCourrielTemplateForm()
59
60 return render_to_response("recrutement/selectionner_template.html", {
61 'form': form
62 }, context_instance=RequestContext(request))
32834000 63
32834000 64
d84c3a68 65def envoyer_courriel_candidats(request):
32834000 66 candidat_ids = request.GET.get('ids_cand').split(',')
4ba84959 67 candidats = models.Candidat.objects.filter(id__in=candidat_ids)
32834000 68 template_id = request.GET.get('id_temp')
4ba84959 69 template = models.CourrielTemplate.objects.get(id=template_id)
4e8340cf 70
d84c3a68 71 if request.method == "POST":
4ba84959
EMS
72 form = forms.CandidatCourrielForm(
73 request.POST, instance=template, candidats=candidats,
74 template=template
75 )
32834000 76
d84c3a68 77 if form.is_valid():
78 form.save()
4ba84959 79 courriel_template = models.CourrielTemplate()
32834000
NBV
80 courriel_template.nom_modele = template.nom_modele
81 courriel_template.sujet = form.data['sujet']
82 courriel_template.plain_text = form.data['plain_text']
4ba84959 83 for cand in candidats:
ba70513a 84 send_templated_email(cand, courriel_template)
4ba84959
EMS
85 messages.add_message(
86 request, messages.SUCCESS,
87 "Le email a été envoyé aux candidats."
88 )
d84c3a68 89 return redirect("admin:recrutement_candidat_changelist")
90 else:
4ba84959
EMS
91 form = forms.CandidatCourrielForm(
92 candidats=candidats, template=template,
93 initial={
94 'sujet': template.sujet,
95 'plain_text': template.plain_text
96 }
97 )
98
99 return render_to_response("recrutement/envoyer_courriel_candidats.html", {
100 'form': form
101 }, context_instance=RequestContext(request))
d84c3a68 102
d84c3a68 103
514db699
NBV
104def affecter_evaluateurs_offre_emploi(request):
105 offre_emploi_ids = request.GET.get('ids').split(',')
4ba84959 106 offres_emploi = emploi.OffreEmploi.objects.filter(id__in=offre_emploi_ids)
514db699 107 if request.method == "POST":
4ba84959 108 form = forms.EvaluateurForm(request.POST, offres_emploi=offres_emploi)
514db699
NBV
109 if form.is_valid():
110 form.save()
4ba84959
EMS
111 messages.add_message(
112 request, messages.SUCCESS,
113 "Les évaluateurs ont été affectés aux offres d'emploi."
114 )
940c9dd8 115 return redirect("admin:recrutement_proxyoffreemploi_changelist")
514db699 116 else:
4ba84959
EMS
117 form = forms.EvaluateurForm(offres_emploi=offres_emploi)
118
119 return render_to_response("recrutement/affecter_evaluateurs.html", {
120 'form': form
121 }, context_instance=RequestContext(request))
514db699 122
b8b74dee
NBV
123
124def send_templated_email(candidat, template):
76b89254 125 from django.conf import settings
b8b74dee
NBV
126 # Sujet
127 sujet_template = Template(template.sujet)
4ba84959 128 dict_sujet = {"offre_emploi": candidat.offre_emploi.nom}
b8b74dee
NBV
129 sujet = Context(dict_sujet)
130 # Plain text
131 texte_template = Template(template.plain_text)
4ba84959
EMS
132 dict_texte = {
133 "nom_candidat": candidat.nom,
134 "prenom_candidat": candidat.prenom,
135 "offre_emploi": candidat.offre_emploi.nom,
136 "genre_candidat": "Monsieur" if candidat.genre == "M" else "Madame",
137 }
b8b74dee 138 texte = Context(dict_texte)
c9cf286d 139 if getattr(settings, 'MAILING_ACTIF', False):
4ba84959 140 dst_emails = [candidat.email]
c9cf286d 141 else:
4ba84959
EMS
142 dst_emails = ['developpeurs@ca.auf.org']
143 msg = EmailMultiAlternatives(
144 sujet_template.render(sujet), texte_template.render(texte),
145 'recrutement@auf.org', dst_emails
146 )
b8b74dee 147 msg.send()
32c22f96
NBV
148
149
150def candidat_pdf(request):
151 candidat_id = request.GET.get('id')
4ba84959 152 candidat = models.Candidat.objects.get(id=candidat_id)
32c22f96 153
4ba84959
EMS
154 return render_to_response("recrutement/candidat_pdf.html", {
155 'candidat': candidat
156 }, context_instance=RequestContext(request))