[#2658] Retrait du hack app_label() dans rh.models
[auf_rh_dae.git] / project / recrutement / views.py
1 # -*- encoding: utf-8 -*-
2
3 from auf.django.emploi import models as emploi
4 from django.core.urlresolvers import reverse
5 from django.contrib import messages
6 from django.views.static import serve
7 from django.http import HttpResponseRedirect
8 from django.shortcuts import render_to_response, redirect
9 from django.template import Context, RequestContext, Template
10 from django.core.mail import EmailMultiAlternatives
11
12 from project.dae.decorators import redirect_interdiction
13 from project.recrutement import forms
14 from project.recrutement import models
15 from project.recrutement.workflow import recrutement_groupes
16
17
18 # MEDIA PRIVE
19
20 def 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)
36
37
38 def index(request):
39 return render_to_response(
40 'recrutement/index.html', {}, RequestContext(request)
41 )
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 return render_to_response("recrutement/selectionner_template.html", {
61 'form': form
62 }, context_instance=RequestContext(request))
63
64
65 def envoyer_courriel_candidats(request):
66 candidat_ids = request.GET.get('ids_cand').split(',')
67 candidats = models.Candidat.objects.filter(id__in=candidat_ids)
68 template_id = request.GET.get('id_temp')
69 template = models.CourrielTemplate.objects.get(id=template_id)
70
71 if request.method == "POST":
72 form = forms.CandidatCourrielForm(
73 request.POST, instance=template, candidats=candidats,
74 template=template
75 )
76
77 if form.is_valid():
78 form.save()
79 courriel_template = models.CourrielTemplate()
80 courriel_template.nom_modele = template.nom_modele
81 courriel_template.sujet = form.data['sujet']
82 courriel_template.plain_text = form.data['plain_text']
83 for cand in candidats:
84 send_templated_email(cand, courriel_template)
85 messages.add_message(
86 request, messages.SUCCESS,
87 "Le email a été envoyé aux candidats."
88 )
89 return redirect("admin:recrutement_candidat_changelist")
90 else:
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))
102
103
104 def affecter_evaluateurs_offre_emploi(request):
105 offre_emploi_ids = request.GET.get('ids').split(',')
106 offres_emploi = emploi.OffreEmploi.objects.filter(id__in=offre_emploi_ids)
107 if request.method == "POST":
108 form = forms.EvaluateurForm(request.POST, offres_emploi=offres_emploi)
109 if form.is_valid():
110 form.save()
111 messages.add_message(
112 request, messages.SUCCESS,
113 "Les évaluateurs ont été affectés aux offres d'emploi."
114 )
115 return redirect("admin:recrutement_proxyoffreemploi_changelist")
116 else:
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))
122
123
124 def send_templated_email(candidat, template):
125 from django.conf import settings
126 # Sujet
127 sujet_template = Template(template.sujet)
128 dict_sujet = {"offre_emploi": candidat.offre_emploi.nom}
129 sujet = Context(dict_sujet)
130 # Plain text
131 texte_template = Template(template.plain_text)
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 }
138 texte = Context(dict_texte)
139 if getattr(settings, 'MAILING_ACTIF', False):
140 dst_emails = [candidat.email]
141 else:
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 )
147 msg.send()
148
149
150 def candidat_pdf(request):
151 candidat_id = request.GET.get('id')
152 candidat = models.Candidat.objects.get(id=candidat_id)
153
154 return render_to_response("recrutement/candidat_pdf.html", {
155 'candidat': candidat
156 }, context_instance=RequestContext(request))