chmod
[auf_rh_dae.git] / project / recrutement / views.py
1 # -*- encoding: utf-8 -*-
2
3 from django.core.mail import send_mail
4 from django.core.urlresolvers import reverse
5 from django.contrib import messages
6 from django.contrib.auth.models import User
7 from django.views.static import serve
8 from django.http import HttpResponse, HttpResponseRedirect
9 from django.shortcuts import render_to_response, redirect, get_object_or_404
10 from django.template import Context, RequestContext, Template
11 from django.core.mail import EmailMultiAlternatives
12
13 from forms import *
14 from models import *
15 from recrutement.workflow import grp_evaluateurs_recrutement, recrutement_groupes
16
17 ################################################################################
18 # MEDIA PRIVE
19 ################################################################################
20
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 def index(request):
39 return render_to_response('recrutement/index.html', {},
40 RequestContext(request))
41
42 def 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')+
51 "?ids_cand=%s&id_temp=%s" % (candidat_ids, courriel_template_id))
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
59 def envoyer_courriel_candidats(request):
60 candidat_ids = request.GET.get('ids_cand').split(',')
61 candidats = Candidat.objects.filter(id__in=candidat_ids)
62 template_id = request.GET.get('id_temp')
63 template = CourrielTemplate.objects.get(id=template_id)
64
65 if request.method == "POST":
66 form = CandidatCourrielForm(request.POST, instance=template,
67 candidats=candidats, template=template)
68
69 if form.is_valid():
70 form.save()
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:
77 send_templated_email(cand, courriel_template)
78 messages.add_message(request, messages.SUCCESS,
79 "Le email a été envoyé aux candidats.")
80 return redirect("admin:recrutement_candidat_changelist")
81 else:
82 form = CandidatCourrielForm(candidats=candidats, template=template,
83 initial={'sujet': template.sujet,
84 'plain_text': template.plain_text,
85 'html': template.html})
86
87 c = {'form' : form}
88 return render_to_response("recrutement/envoyer_courriel_candidats.html",
89 Context(c), context_instance = RequestContext(request))
90
91 def 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)
94 if request.method == "POST":
95 form = EvaluateurForm(request.POST, offres_emploi=offres_emploi)
96 if form.is_valid():
97 form.save()
98 messages.add_message(request, messages.SUCCESS,
99 "Les évaluateurs ont été affectés aux offres d'emploi.")
100 return redirect("admin:recrutement_offreemploi_changelist")
101 else:
102 form = EvaluateurForm(offres_emploi=offres_emploi)
103
104 c = {'form' : form}
105 return render_to_response("recrutement/affecter_evaluateurs.html",
106 Context(c), context_instance = RequestContext(request))
107
108 def send_templated_email(candidat, template):
109 from django.conf import settings
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)
126 if settings.DEBUG:
127 dst_emails = ['developpeurs@ca.auf.org', ]
128 else:
129 dst_emails = [candidat.email, ]
130 msg = EmailMultiAlternatives(sujet_template.render(sujet),
131 texte_template.render(texte),
132 'recrutement@auf.org',
133 dst_emails,
134 )
135 msg.attach_alternative(texte_template.render(texte_html), "text/html")
136 msg.send()
137
138
139 def 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))