c181d268dd51216bbb3335aa009a12c6ed7bdff9
[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.http import HttpResponse, HttpResponseRedirect
8 from django.shortcuts import render_to_response, redirect, get_object_or_404
9 from django.template import Context, RequestContext, Template
10 from django.core.mail import EmailMultiAlternatives
11
12 from forms import *
13 from models import *
14 from recrutement.workflow import grp_evaluateurs_recrutement
15
16 def index(request):
17 return render_to_response('recrutement/index.html', {},
18 RequestContext(request))
19
20 def selectionner_template(request):
21 candidat_ids = request.GET.get('ids')
22 if request.method == "POST":
23 form = CandidatCourrielTemplateForm(request.POST)
24 if form.is_valid():
25 form.save()
26 courriel_template_id = form.get_template()
27
28 return HttpResponseRedirect(reverse('envoyer_courriel_candidats')+
29 "?ids_cand=%s&id_temp=%s" % (candidat_ids, courriel_template_id))
30 else:
31 form = CandidatCourrielTemplateForm()
32
33 c = {'form' : form}
34 return render_to_response("recrutement/selectionner_template.html",
35 Context(c), context_instance = RequestContext(request))
36
37 def envoyer_courriel_candidats(request):
38 candidat_ids = request.GET.get('ids_cand').split(',')
39 candidats = Candidat.objects.filter(id__in=candidat_ids)
40 template_id = request.GET.get('id_temp')
41 template = CourrielTemplate.objects.get(id=template_id)
42
43 if request.method == "POST":
44 form = CandidatCourrielForm(request.POST, instance=template,
45 candidats=candidats, template=template)
46
47 if form.is_valid():
48 form.save()
49 courriel_template = CourrielTemplate()
50 courriel_template.nom_modele = template.nom_modele
51 courriel_template.sujet = form.data['sujet']
52 courriel_template.plain_text = form.data['plain_text']
53 courriel_template.html = form.data['html']
54 for cand in candidats:
55 send_templated_email(cand, courriel_template)
56 messages.add_message(request, messages.SUCCESS,
57 "Le email a été envoyé aux candidats.")
58 return redirect("admin:recrutement_candidat_changelist")
59 else:
60 form = CandidatCourrielForm(candidats=candidats, template=template,
61 initial={'sujet': template.sujet,
62 'plain_text': template.plain_text,
63 'html': template.html})
64
65 c = {'form' : form}
66 return render_to_response("recrutement/envoyer_courriel_candidats.html",
67 Context(c), context_instance = RequestContext(request))
68
69 def affecter_evaluateurs_offre_emploi(request):
70 offre_emploi_ids = request.GET.get('ids').split(',')
71 offres_emploi = OffreEmploi.objects.filter(id__in=offre_emploi_ids)
72 if request.method == "POST":
73 form = EvaluateurForm(request.POST, offres_emploi=offres_emploi)
74 if form.is_valid():
75 form.save()
76 messages.add_message(request, messages.SUCCESS,
77 "Les évaluateurs ont été affectés aux offres d'emploi.")
78 return redirect("admin:recrutement_offreemploi_changelist")
79 else:
80 form = EvaluateurForm(offres_emploi=offres_emploi)
81
82 c = {'form' : form}
83 return render_to_response("recrutement/affecter_evaluateurs.html",
84 Context(c), context_instance = RequestContext(request))
85
86 def send_templated_email(candidat, template):
87 from django.conf import settings
88 # Sujet
89 sujet_template = Template(template.sujet)
90 dict_sujet = {"offre_emploi": candidat.offre_emploi.nom,}
91 sujet = Context(dict_sujet)
92 # Plain text
93 texte_template = Template(template.plain_text)
94 dict_texte = {"nom_candidat": candidat.nom,
95 "prenom_candidat": candidat.prenom,
96 "offre_emploi": candidat.offre_emploi.nom,
97 "genre_candidat": "Monsieur" if candidat.genre == "M" \
98 else "Madame",
99 }
100 texte = Context(dict_texte)
101 # HTML text
102 html_template = Template(template.html)
103 texte_html = Context(dict_texte)
104 if settings.DEBUG:
105 dst_emails = ['developpeurs@ca.auf.org', ]
106 else:
107 dst_emails = [candidat.email, ]
108 msg = EmailMultiAlternatives(sujet_template.render(sujet),
109 texte_template.render(texte),
110 'recrutement@auf.org',
111 dst_emails,
112 )
113 msg.attach_alternative(texte_template.render(texte_html), "text/html")
114 msg.send()