Correction à l'API
[auf_rh_dae.git] / project / recrutement / views.py
1 # -*- encoding: utf-8 -*-
2
3 from django.contrib import messages
4 from django.contrib.auth.models import User
5 from django.core.mail import send_mail
6 from django.core.urlresolvers import reverse
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 from auf.django.emploi import views as emp
16
17 def index(request):
18 return render_to_response('recrutement/index.html', {},
19 RequestContext(request))
20
21 def selectionner_template(request):
22 candidat_ids = request.GET.get('ids')
23 if request.method == "POST":
24 form = CandidatCourrielTemplateForm(request.POST)
25 if form.is_valid():
26 form.save()
27 courriel_template_id = form.get_template()
28
29 return HttpResponseRedirect(reverse('envoyer_courriel_candidats')+
30 "?ids_cand=%s&id_temp=%s" % (candidat_ids, courriel_template_id))
31 else:
32 form = CandidatCourrielTemplateForm()
33
34 c = {'form' : form}
35 return render_to_response("recrutement/selectionner_template.html",
36 Context(c), context_instance = RequestContext(request))
37
38 def envoyer_courriel_candidats(request):
39 candidat_ids = request.GET.get('ids_cand').split(',')
40 candidats = Candidat.objects.filter(id__in=candidat_ids)
41 template_id = request.GET.get('id_temp')
42 template = CourrielTemplate.objects.get(id=template_id)
43
44 if request.method == "POST":
45 form = CandidatCourrielForm(request.POST, instance=template,
46 candidats=candidats, template=template)
47
48 if form.is_valid():
49 form.save()
50 courriel_template = CourrielTemplate()
51 courriel_template.nom_modele = template.nom_modele
52 courriel_template.sujet = form.data['sujet']
53 courriel_template.plain_text = form.data['plain_text']
54 courriel_template.html = form.data['html']
55 for cand in candidats:
56 emp.send_templated_email(cand, courriel_template)
57 messages.add_message(request, messages.SUCCESS,
58 "Le email a été envoyé aux candidats.")
59 return redirect("admin:recrutement_candidat_changelist")
60 else:
61 form = CandidatCourrielForm(candidats=candidats, template=template,
62 initial={'sujet': template.sujet,
63 'plain_text': template.plain_text,
64 'html': template.html})
65
66 c = {'form' : form}
67 return render_to_response("recrutement/envoyer_courriel_candidats.html",
68 Context(c), context_instance = RequestContext(request))
69
70 def postuler_appel_offre(request):
71 vars = dict()
72 offre_emploi = get_object_or_404(OffreEmploi, id=request.GET.get('id'))
73 candidat = Candidat()
74 candidat.offre_emploi = offre_emploi
75
76 if request.method == "POST":
77 form = PostulerOffreEmploiForm(request.POST,
78 instance=candidat, offre_emploi=offre_emploi)
79 piecesForm = CandidatPieceForm(request.POST, request.FILES,
80 instance=candidat)
81 if form.is_valid() and piecesForm.is_valid():
82 offre = form.save()
83 piecesForm.instance = offre
84 piecesForm.save()
85
86 courriel_template = CourrielTemplate.objects.\
87 get(nom_modele='Confirmation postulation (automatique)')
88 send_templated_email(candidat, courriel_template)
89
90 messages.add_message(request, messages.SUCCESS,
91 "Votre application à l'appel d'offre d'emploi a \
92 été effectuée.")
93 return redirect("admin:recrutement_offreemploi_changelist")
94 else:
95 messages.add_message(request, messages.ERROR,
96 'Il y a des erreurs dans le formulaire.')
97 else:
98 form = PostulerOffreEmploiForm(instance=candidat,
99 offre_emploi=offre_emploi)
100 piecesForm = CandidatPieceForm(instance=candidat)
101
102 vars.update(dict(form=form, candidat=candidat, piecesForm=piecesForm, ))
103
104 return render_to_response('recrutement/postuler_appel_offre.html', vars,
105 RequestContext(request))
106
107 def affecter_evaluateurs_offre_emploi(request):
108 offre_emploi_ids = request.GET.get('ids').split(',')
109 offres_emploi = OffreEmploi.objects.filter(id__in=offre_emploi_ids)
110 if request.method == "POST":
111 form = EvaluateurForm(request.POST, offres_emploi=offres_emploi)
112 if form.is_valid():
113 form.save()
114 messages.add_message(request, messages.SUCCESS,
115 "Les évaluateurs ont été affectés aux offres d'emploi.")
116 return redirect("admin:recrutement_offreemploi_changelist")
117 else:
118 form = EvaluateurForm(offres_emploi=offres_emploi)
119
120 c = {'form' : form}
121 return render_to_response("recrutement/affecter_evaluateurs.html",
122 Context(c), context_instance = RequestContext(request))