recrutement : debug permissions.py
[auf_rh_dae.git] / project / recrutement / views.py
1 # -*- encoding: utf-8 -*-
2
3 from django.core.urlresolvers import reverse
4 from django.contrib import messages
5 from django.views.static import serve
6 from django.http import HttpResponseRedirect
7 from django.shortcuts import render_to_response, redirect
8 from django.template import Context, RequestContext, Template
9 from django.core.mail import EmailMultiAlternatives
10
11 from forms import *
12 from models import *
13 from recrutement.workflow import recrutement_groupes
14
15 ################################################################################
16 # MEDIA PRIVE
17 ################################################################################
18
19 def mediaserve(request, path, document_root=None, show_indexes=False):
20 """
21 Sécuriser l'accès aux fichiers uploadés
22 """
23 grant_ok = False
24 user = request.user
25
26 for grp in user.groups.all():
27 if grp in recrutement_groupes:
28 grant_ok = True
29 break
30
31 if not grant_ok:
32 return redirect_interdiction(request)
33
34 return serve(request, path, document_root, show_indexes)
35
36 def index(request):
37 return render_to_response('recrutement/index.html', {},
38 RequestContext(request))
39
40 def selectionner_template(request):
41 candidat_ids = request.GET.get('ids')
42 if request.method == "POST":
43 form = CandidatCourrielTemplateForm(request.POST)
44 if form.is_valid():
45 form.save()
46 courriel_template_id = form.get_template()
47
48 return HttpResponseRedirect(reverse('envoyer_courriel_candidats')+
49 "?ids_cand=%s&id_temp=%s" % (candidat_ids, courriel_template_id))
50 else:
51 form = CandidatCourrielTemplateForm()
52
53 c = {'form' : form}
54 return render_to_response("recrutement/selectionner_template.html",
55 Context(c), context_instance = RequestContext(request))
56
57 def envoyer_courriel_candidats(request):
58 candidat_ids = request.GET.get('ids_cand').split(',')
59 candidats = Candidat.objects.filter(id__in=candidat_ids)
60 template_id = request.GET.get('id_temp')
61 template = CourrielTemplate.objects.get(id=template_id)
62
63 if request.method == "POST":
64 form = CandidatCourrielForm(request.POST, instance=template,
65 candidats=candidats, template=template)
66
67 if form.is_valid():
68 form.save()
69 courriel_template = CourrielTemplate()
70 courriel_template.nom_modele = template.nom_modele
71 courriel_template.sujet = form.data['sujet']
72 courriel_template.plain_text = form.data['plain_text']
73 for cand in candidats:
74 send_templated_email(cand, courriel_template)
75 messages.add_message(request, messages.SUCCESS,
76 "Le email a été envoyé aux candidats.")
77 return redirect("admin:recrutement_candidat_changelist")
78 else:
79 form = CandidatCourrielForm(candidats=candidats, template=template,
80 initial={'sujet': template.sujet,
81 'plain_text': template.plain_text,})
82
83 c = {'form' : form}
84 return render_to_response("recrutement/envoyer_courriel_candidats.html",
85 Context(c), context_instance = RequestContext(request))
86
87 def affecter_evaluateurs_offre_emploi(request):
88 offre_emploi_ids = request.GET.get('ids').split(',')
89 offres_emploi = OffreEmploi.objects.filter(id__in=offre_emploi_ids)
90 if request.method == "POST":
91 form = EvaluateurForm(request.POST, offres_emploi=offres_emploi)
92 if form.is_valid():
93 form.save()
94 messages.add_message(request, messages.SUCCESS,
95 "Les évaluateurs ont été affectés aux offres d'emploi.")
96 return redirect("admin:recrutement_proxyoffreemploi_changelist")
97 else:
98 form = EvaluateurForm(offres_emploi=offres_emploi)
99
100 c = {'form' : form}
101 return render_to_response("recrutement/affecter_evaluateurs.html",
102 Context(c), context_instance = RequestContext(request))
103
104 def send_templated_email(candidat, template):
105 from django.conf import settings
106 # Sujet
107 sujet_template = Template(template.sujet)
108 dict_sujet = {"offre_emploi": candidat.offre_emploi.nom,}
109 sujet = Context(dict_sujet)
110 # Plain text
111 texte_template = Template(template.plain_text)
112 dict_texte = {"nom_candidat": candidat.nom,
113 "prenom_candidat": candidat.prenom,
114 "offre_emploi": candidat.offre_emploi.nom,
115 "genre_candidat": "Monsieur" if candidat.genre == "M" \
116 else "Madame",
117 }
118 texte = Context(dict_texte)
119 if getattr(settings, 'MAILING_ACTIF', False):
120 dst_emails = [candidat.email, ]
121 else:
122 dst_emails = ['developpeurs@ca.auf.org', ]
123 msg = EmailMultiAlternatives(sujet_template.render(sujet),
124 texte_template.render(texte),
125 'recrutement@auf.org',
126 dst_emails,
127 )
128 msg.send()
129
130
131 def candidat_pdf(request):
132 candidat_id = request.GET.get('id')
133 candidat = Candidat.objects.get(id=candidat_id)
134
135 return render_to_response("recrutement/candidat_pdf.html",
136 Context({'candidat' : candidat}), context_instance = RequestContext(request))