recrutement, dae : groups et workflow
[auf_rh_dae.git] / project / recrutement / api.py
1 # -*- encoding: utf-8 -*
2
3 from datetime import date
4
5 from django.contrib import messages
6 from django.core import serializers
7 from django.http import HttpResponse
8 from django.shortcuts import render_to_response, redirect, get_object_or_404
9 from django.template import RequestContext, Template
10 from django.utils import simplejson
11
12 from auf.django.emploi import models as emploi
13 from auf.django.emploi import forms as emploiForms
14
15 from project.recrutement.models import Evaluateur, CandidatEvaluation, \
16 CourrielTemplate
17 from project.recrutement.views import send_templated_email
18
19
20 STATUS_OK = 200
21 STATUS_ERROR = 400
22 STATUS_ERROR_NOT_FOUND = 404
23 STATUS_ERROR_PERMISSIONS = 403
24 STATUS_ERROR_BADMETHOD = 405
25
26 def api(request, method, *args, **kwargs):
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)
32
33 api = API(request)
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)()
41
42
43 def api_return(status, text='', json=False):
44 content_type = 'text/html'
45 if status == STATUS_OK and json:
46 content_type = 'text/json'
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
67 class API:
68 def __init__(self, request):
69 self.request = request
70
71 def api_candidat_add(self, offre_id):
72 vars = dict()
73 offre = emploi.OffreEmploi.objects.get(id=offre_id)
74
75 if self.request.method == "POST":
76 candidat = emploi.Candidat()
77 candidat.offre_emploi = offre
78 form = emploiForms.NoCaptchaPostulerOffreEmploiForm(self.request.POST, instance=candidat)
79 piecesForm = emploiForms.CandidatPieceForm(self.request.POST, self.request.FILES, instance=candidat)
80 if form.is_valid():
81 candidat = form.save()
82 piecesForm.save()
83 data = serializers.serialize('json', [candidat,])
84 courriel_template = CourrielTemplate.objects.get(id=1)
85 send_templated_email(candidat, courriel_template)
86 return api_return(STATUS_OK, data)
87 else:
88 return api_return(STATUS_ERROR, form.errors)
89
90
91
92 def api_offre_emploi_liste(self):
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);
96
97 def api_offre_emploi(self):
98 try:
99 id = self.request.GET.get('id')
100 offre = emploi.OffreEmploi.objects.get(id=id, statut="AFFI", date_limite__gte=date.today())
101 except emploi.OffreEmploi.DoesNotExist:
102 return api_return(STATUS_ERROR, "ID d'offre d'emploi invalide")
103
104 data = serializers.serialize('json', [offre])
105 return api_return(STATUS_OK, data)