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