Merge branch 'refactoring' into test
[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 import datamaster_modeles.models as ref
13
14 from auf.django.emploi import models as emploi
15 from auf.django.emploi import forms as emploiForms
16
17 from project.recrutement.models import Evaluateur, CandidatEvaluation, \
18 CourrielTemplate
19 from project.recrutement.views import send_templated_email
20
21
22 STATUS_OK = 200
23 STATUS_ERROR = 400
24 STATUS_ERROR_NOT_FOUND = 404
25 STATUS_ERROR_PERMISSIONS = 403
26 STATUS_ERROR_BADMETHOD = 405
27
28 def api(request, method, *args, **kwargs):
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)
34
35 api = API(request)
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)()
43
44
45 def api_return(status, text='', json=False):
46 content_type = 'text/html'
47 if status == STATUS_OK and json:
48 content_type = 'text/json'
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
69 class API:
70 def __init__(self, request):
71 self.request = request
72
73 def api_candidat_add(self, offre_id):
74 vars = dict()
75 offre = emploi.OffreEmploi.objects.get(id=offre_id)
76
77 if self.request.method == "POST":
78 candidat = emploi.Candidat()
79 candidat.offre_emploi = offre
80 form = emploiForms.NoCaptchaPostulerOffreEmploiForm(self.request.POST, instance=candidat)
81 piecesForm = emploiForms.CandidatPieceForm(self.request.POST, self.request.FILES, instance=candidat)
82 if form.is_valid():
83 candidat = form.save()
84 piecesForm.save()
85 data = serializers.serialize('json', [candidat,])
86 courriel_template = CourrielTemplate.objects.get(id=1)
87 send_templated_email(candidat, courriel_template)
88 return api_return(STATUS_OK, data)
89 else:
90 return api_return(STATUS_ERROR, form.errors)
91
92
93
94 def api_offre_emploi_liste(self):
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);
98
99 def api_offre_emploi(self):
100 try:
101 id = self.request.GET.get('id')
102 offre = emploi.OffreEmploi.objects.get(id=id, statut="AFFI", date_limite__gte=date.today())
103 except emploi.OffreEmploi.DoesNotExist:
104 return api_return(STATUS_ERROR, "ID d'offre d'emploi invalide")
105
106 data = serializers.serialize('json', [offre])
107 return api_return(STATUS_OK, data)