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