Commit | Line | Data |
---|---|---|
f77f4b9b | 1 | # -*- encoding: utf-8 -* |
c2141437 | 2 | from django.core import serializers |
13b8a64e | 3 | from datetime import date |
e1c666fe | 4 | from django.http import HttpResponse |
d46075cb | 5 | from django.template import RequestContext, Template |
f77f4b9b | 6 | from django.shortcuts import render_to_response, redirect, get_object_or_404 |
e1c666fe | 7 | from django.utils import simplejson |
f77f4b9b NBV |
8 | from django.contrib import messages |
9 | ||
43198a9f | 10 | import datamaster_modeles.models as ref |
e1c666fe | 11 | from auf.django.emploi import models as emploi |
f77f4b9b | 12 | from auf.django.emploi import forms as emploiForms |
b8b74dee NBV |
13 | from project.recrutement.models import Evaluateur, CandidatEvaluation, \ |
14 | CourrielTemplate | |
11f7b571 | 15 | from project.recrutement.views import send_templated_email |
e1c666fe NBV |
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 | ||
c2141437 | 24 | def api(request, method, *args, **kwargs): |
f77f4b9b NBV |
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) | |
43198a9f | 30 | |
f77f4b9b | 31 | api = API(request) |
c2141437 OL |
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)() | |
14e06ff6 | 39 | |
f77f4b9b | 40 | |
e1c666fe | 41 | def api_return(status, text='', json=False): |
d46075cb | 42 | content_type = 'text/html' |
e1c666fe NBV |
43 | if status == STATUS_OK and json: |
44 | content_type = 'text/json' | |
e1c666fe NBV |
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: | |
e1c666fe NBV |
66 | def __init__(self, request): |
67 | self.request = request | |
68 | ||
43198a9f | 69 | def api_candidat_add(self, offre_id): |
f77f4b9b | 70 | vars = dict() |
43198a9f | 71 | offre = emploi.OffreEmploi.objects.get(id=offre_id) |
3a542b85 | 72 | |
f77f4b9b | 73 | if self.request.method == "POST": |
c2141437 OL |
74 | candidat = emploi.Candidat() |
75 | candidat.offre_emploi = offre | |
76 | form = emploiForms.NoCaptchaPostulerOffreEmploiForm(self.request.POST, instance=candidat) | |
7f25c8a6 | 77 | piecesForm = emploiForms.CandidatPieceForm(self.request.POST, self.request.FILES, instance=candidat) |
c2141437 OL |
78 | if form.is_valid(): |
79 | candidat = form.save() | |
7f25c8a6 | 80 | piecesForm.save() |
c2141437 | 81 | data = serializers.serialize('json', [candidat,]) |
76b89254 OL |
82 | courriel_template = CourrielTemplate.objects.get(id=1) |
83 | send_templated_email(candidat, courriel_template) | |
c2141437 OL |
84 | return api_return(STATUS_OK, data) |
85 | else: | |
86 | return api_return(STATUS_ERROR, form.errors) | |
87 | ||
43198a9f | 88 | |
f77f4b9b NBV |
89 | |
90 | def api_offre_emploi_liste(self): | |
c2141437 OL |
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); | |
f77f4b9b NBV |
94 | |
95 | def api_offre_emploi(self): | |
96 | try: | |
c2141437 OL |
97 | id = self.request.GET.get('id') |
98 | offre = emploi.OffreEmploi.objects.get(id=id, statut="AFFI", date_limite__gte=date.today()) | |
f77f4b9b NBV |
99 | except emploi.OffreEmploi.DoesNotExist: |
100 | return api_return(STATUS_ERROR, "ID d'offre d'emploi invalide") | |
13b8a64e | 101 | |
c2141437 OL |
102 | data = serializers.serialize('json', [offre]) |
103 | return api_return(STATUS_OK, data) |