PJ ofrre emploi
[auf_rh_dae.git] / project / recrutement / api.py
1 # -*- encoding: utf-8 -*
2 from django.core import serializers
3 from datetime import date
4 from django.http import HttpResponse
5 from django.template import RequestContext, Template
6 from django.shortcuts import render_to_response, redirect, get_object_or_404
7 from django.utils import simplejson
8 from django.contrib import messages
9
10 import datamaster_modeles.models as ref
11 from auf.django.emploi import models as emploi
12 from auf.django.emploi import forms as emploiForms
13 from project.recrutement.models import Evaluateur, CandidatEvaluation, \
14 CourrielTemplate
15 from project.recrutement.views import send_templated_email
16
17 STATUS_OK = 200
18
19 STATUS_ERROR = 400
20 STATUS_ERROR_NOT_FOUND = 404
21 STATUS_ERROR_PERMISSIONS = 403
22 STATUS_ERROR_BADMETHOD = 405
23
24 def api(request, method, *args, **kwargs):
25 # TODO: Sécurité :
26 # L'échange d'information doit être possible qu'avec les HOST désirés.
27
28 #if request.method != 'POST':
29 # return api_return(STATUS_ERROR_BADMETHOD)
30
31 api = API(request)
32 if not hasattr(api, 'api_%s' % method):
33 return api_return(STATUS_ERROR)
34 if kwargs.has_key('offre_id'):
35 offre_id = kwargs['offre_id']
36 return api.api_candidat_add(offre_id)
37 else:
38 return getattr(api, 'api_%s' % method)()
39
40
41 def api_return(status, text='', json=False):
42 content_type = 'text/html'
43 if status == STATUS_OK and json:
44 content_type = 'text/json'
45 if text is None:
46 if status == STATUS_ERROR:
47 text = 'Error'
48 elif status == STATUS_ERROR_NOT_FOUND:
49 text = 'Resource Not Found'
50 elif status == STATUS_ERROR_PERMISSIONS:
51 text = 'Invalid username or password'
52 elif status == STATUS_ERROR_BADMETHOD:
53 text = 'Invalid request method'
54 elif status == STATUS_OK:
55 text = 'OK'
56
57 r = HttpResponse(status=status, content=text, content_type=content_type)
58
59 if status == STATUS_ERROR_BADMETHOD:
60 r.Allow = 'POST'
61
62 return r
63
64
65 class API:
66 def __init__(self, request):
67 self.request = request
68
69 def api_candidat_add(self, offre_id):
70 vars = dict()
71 offre = emploi.OffreEmploi.objects.get(id=offre_id)
72
73 if self.request.method == "POST":
74 candidat = emploi.Candidat()
75 candidat.offre_emploi = offre
76 form = emploiForms.NoCaptchaPostulerOffreEmploiForm(self.request.POST, instance=candidat)
77 piecesForm = emploiForms.CandidatPieceForm(self.request.POST, self.request.FILES, instance=candidat)
78 if form.is_valid():
79 candidat = form.save()
80 piecesForm.save()
81 data = serializers.serialize('json', [candidat,])
82
83 evaluateurs = candidat.offre_emploi.evaluateurs.all()
84 for evaluateur in evaluateurs:
85 candidat_evaluation = CandidatEvaluation()
86 candidat_evaluation.candidat = candidat
87 candidat_evaluation.evaluateur = evaluateur
88 candidat_evaluation.save()
89
90 courriel_template = CourrielTemplate.objects.get(id=1)
91 send_templated_email(candidat, courriel_template)
92 return api_return(STATUS_OK, data)
93 else:
94 return api_return(STATUS_ERROR, form.errors)
95
96
97
98 def api_offre_emploi_liste(self):
99 offres_visibles = emploi.OffreEmploi.objects.filter(est_affiche=True, statut="AFFI", date_limite__gte=date.today())
100 data = serializers.serialize('json', offres_visibles)
101 return api_return(STATUS_OK, data);
102
103 def api_offre_emploi(self):
104 try:
105 id = self.request.GET.get('id')
106 offre = emploi.OffreEmploi.objects.get(id=id, statut="AFFI", date_limite__gte=date.today())
107 except emploi.OffreEmploi.DoesNotExist:
108 return api_return(STATUS_ERROR, "ID d'offre d'emploi invalide")
109
110 data = serializers.serialize('json', [offre])
111 return api_return(STATUS_OK, data)