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