fix for mailing
[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
32834000 7from django.http import HttpResponse, HttpResponseRedirect
89e948de 8from django.shortcuts import render_to_response, redirect, get_object_or_404
32834000
NBV
9from django.template import Context, RequestContext, Template
10from django.core.mail import EmailMultiAlternatives
6f2d5d58 11
362a3534 12from forms import *
13from models import *
f6724c20 14from recrutement.workflow import grp_evaluateurs_recrutement
6f2d5d58 15
16def index(request):
362a3534 17 return render_to_response('recrutement/index.html', {},
18 RequestContext(request))
19
32834000
NBV
20def 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')+
dc7faf2b 29 "?ids_cand=%s&id_temp=%s" % (candidat_ids, courriel_template_id))
32834000
NBV
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
d84c3a68 37def envoyer_courriel_candidats(request):
32834000 38 candidat_ids = request.GET.get('ids_cand').split(',')
d84c3a68 39 candidats = Candidat.objects.filter(id__in=candidat_ids)
32834000
NBV
40 template_id = request.GET.get('id_temp')
41 template = CourrielTemplate.objects.get(id=template_id)
4e8340cf 42
d84c3a68 43 if request.method == "POST":
32834000
NBV
44 form = CandidatCourrielForm(request.POST, instance=template,
45 candidats=candidats, template=template)
46
d84c3a68 47 if form.is_valid():
48 form.save()
32834000
NBV
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:
ba70513a 55 send_templated_email(cand, courriel_template)
d84c3a68 56 messages.add_message(request, messages.SUCCESS,
32834000 57 "Le email a été envoyé aux candidats.")
d84c3a68 58 return redirect("admin:recrutement_candidat_changelist")
59 else:
32834000
NBV
60 form = CandidatCourrielForm(candidats=candidats, template=template,
61 initial={'sujet': template.sujet,
62 'plain_text': template.plain_text,
63 'html': template.html})
d84c3a68 64
65 c = {'form' : form}
66 return render_to_response("recrutement/envoyer_courriel_candidats.html",
67 Context(c), context_instance = RequestContext(request))
68
514db699
NBV
69def 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)
514db699 72 if request.method == "POST":
540dfae4 73 form = EvaluateurForm(request.POST, offres_emploi=offres_emploi)
514db699
NBV
74 if form.is_valid():
75 form.save()
76 messages.add_message(request, messages.SUCCESS,
540dfae4 77 "Les évaluateurs ont été affectés aux offres d'emploi.")
514db699
NBV
78 return redirect("admin:recrutement_offreemploi_changelist")
79 else:
540dfae4 80 form = EvaluateurForm(offres_emploi=offres_emploi)
514db699
NBV
81
82 c = {'form' : form}
83 return render_to_response("recrutement/affecter_evaluateurs.html",
84 Context(c), context_instance = RequestContext(request))
b8b74dee
NBV
85
86def send_templated_email(candidat, template):
76b89254 87 from django.conf import settings
b8b74dee
NBV
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)
76b89254
OL
104 if settings.DEBUG:
105 dst_emails = ['developpeurs@ca.auf.org', ]
106 else:
107 dst_emails = [candidat.email, ]
b8b74dee
NBV
108 msg = EmailMultiAlternatives(sujet_template.render(sujet),
109 texte_template.render(texte),
110 'recrutement@auf.org',
76b89254
OL
111 dst_emails,
112 )
b8b74dee
NBV
113 msg.attach_alternative(texte_template.render(texte_html), "text/html")
114 msg.send()