7d8af53865bad94963263fad8d0d511ad2830212
[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 courriel_template = CourrielTemplate.objects.get(id=1)
83 send_templated_email(candidat, courriel_template)
84 return api_return(STATUS_OK, data)
85 else:
86 return api_return(STATUS_ERROR, form.errors)
87
88
89
90 def api_offre_emploi_liste(self):
91 offres_visibles = emploi.OffreEmploi.objects.filter(est_affiche=True, statut="AFFI", date_limite__gte=date.today())
92 data = serializers.serialize('json', offres_visibles)
93 return api_return(STATUS_OK, data);
94
95 def api_offre_emploi(self):
96 try:
97 id = self.request.GET.get('id')
98 offre = emploi.OffreEmploi.objects.get(id=id, statut="AFFI", date_limite__gte=date.today())
99 except emploi.OffreEmploi.DoesNotExist:
100 return api_return(STATUS_ERROR, "ID d'offre d'emploi invalide")
101
102 data = serializers.serialize('json', [offre])
103 return api_return(STATUS_OK, data)