Commit | Line | Data |
---|---|---|
f77f4b9b | 1 | # -*- encoding: utf-8 -* |
13b8a64e | 2 | from datetime import date |
e1c666fe | 3 | from django.http import HttpResponse |
d46075cb | 4 | from django.template import RequestContext, Template |
f77f4b9b | 5 | from django.shortcuts import render_to_response, redirect, get_object_or_404 |
e1c666fe | 6 | from django.utils import simplejson |
f77f4b9b NBV |
7 | from django.contrib import messages |
8 | ||
43198a9f | 9 | import datamaster_modeles.models as ref |
e1c666fe | 10 | from auf.django.emploi import models as emploi |
f77f4b9b | 11 | from auf.django.emploi import forms as emploiForms |
b8b74dee NBV |
12 | from project.recrutement.models import Evaluateur, CandidatEvaluation, \ |
13 | CourrielTemplate | |
11f7b571 | 14 | from project.recrutement.views import send_templated_email |
e1c666fe NBV |
15 | |
16 | STATUS_OK = 200 | |
17 | ||
18 | STATUS_ERROR = 400 | |
19 | STATUS_ERROR_NOT_FOUND = 404 | |
20 | STATUS_ERROR_PERMISSIONS = 403 | |
21 | STATUS_ERROR_BADMETHOD = 405 | |
22 | ||
43198a9f | 23 | def api(request, method, offre_id=None): |
f77f4b9b NBV |
24 | # TODO: Sécurité : |
25 | # L'échange d'information doit être possible qu'avec les HOST désirés. | |
26 | ||
27 | #if request.method != 'POST': | |
28 | # return api_return(STATUS_ERROR_BADMETHOD) | |
43198a9f | 29 | |
f77f4b9b NBV |
30 | api = API(request) |
31 | if hasattr(api, 'api_%s' % method): | |
43198a9f NBV |
32 | if offre_id is None: |
33 | return getattr(api, 'api_%s' % method)() | |
34 | else: | |
35 | return api.api_candidat_add(offre_id) | |
14e06ff6 | 36 | |
f77f4b9b NBV |
37 | return api_return(STATUS_ERROR) |
38 | ||
e1c666fe | 39 | def api_return(status, text='', json=False): |
d46075cb | 40 | content_type = 'text/html' |
e1c666fe NBV |
41 | if status == STATUS_OK and json: |
42 | content_type = 'text/json' | |
e1c666fe NBV |
43 | if text is None: |
44 | if status == STATUS_ERROR: | |
45 | text = 'Error' | |
46 | elif status == STATUS_ERROR_NOT_FOUND: | |
47 | text = 'Resource Not Found' | |
48 | elif status == STATUS_ERROR_PERMISSIONS: | |
49 | text = 'Invalid username or password' | |
50 | elif status == STATUS_ERROR_BADMETHOD: | |
51 | text = 'Invalid request method' | |
52 | elif status == STATUS_OK: | |
53 | text = 'OK' | |
54 | ||
55 | r = HttpResponse(status=status, content=text, content_type=content_type) | |
56 | ||
57 | if status == STATUS_ERROR_BADMETHOD: | |
58 | r.Allow = 'POST' | |
59 | ||
60 | return r | |
61 | ||
62 | ||
63 | class API: | |
e1c666fe NBV |
64 | def __init__(self, request): |
65 | self.request = request | |
66 | ||
43198a9f | 67 | def api_candidat_add(self, offre_id): |
f77f4b9b | 68 | vars = dict() |
43198a9f | 69 | offre = emploi.OffreEmploi.objects.get(id=offre_id) |
3a542b85 | 70 | |
f77f4b9b | 71 | if self.request.method == "POST": |
43198a9f | 72 | try: |
43198a9f NBV |
73 | candidat = emploi.Candidat() |
74 | candidat.offre_emploi = offre | |
75 | candidat.nom = self.request.POST['nom'] | |
76 | candidat.prenom = self.request.POST['prenom'] | |
77 | candidat.genre = self.request.POST['genre'] | |
464e5825 NBV |
78 | candidat.nationalite = ref.Pays.objects.get\ |
79 | (id=self.request.POST['nationalite']) | |
43198a9f NBV |
80 | candidat.situation_famille = self.request.POST['situation_famille'] |
81 | candidat.nombre_dependant = self.request.POST['nombre_dependant'] | |
82 | candidat.niveau_diplome = self.request.POST['niveau_diplome'] | |
83 | candidat.employeur_actuel = self.request.POST['employeur_actuel'] | |
84 | candidat.poste_actuel = self.request.POST['poste_actuel'] | |
464e5825 NBV |
85 | candidat.domaine_professionnel = self.request.\ |
86 | POST['domaine_professionnel'] | |
43198a9f NBV |
87 | candidat.telephone = self.request.POST['telephone'] |
88 | candidat.email = self.request.POST['email'] | |
89 | candidat.adresse = self.request.POST['adresse'] | |
90 | candidat.ville = self.request.POST['ville'] | |
91 | candidat.etat_province = self.request.POST['etat_province'] | |
92 | candidat.code_postal = self.request.POST['code_postal'] | |
93 | candidat.pays = ref.Pays.objects.get(id=self.request.POST['pays']) | |
94 | candidat.save() | |
95 | ||
3a542b85 NBV |
96 | for i in range(0, int(self.request.POST['candidat_piece-TOTAL_FORMS'])-1): |
97 | if self.request.POST['candidat_piece-' + str(i) + '-nom'] is not None: | |
98 | piece = emploi.CandidatPiece() | |
99 | piece.candidat = candidat | |
100 | import pdb;pdb.set_trace() | |
101 | piece.nom = self.request.POST['candidat_piece-' + str(i) + '-nom'] | |
102 | import pdb;pdb.set_trace() | |
103 | piece.path = self.request.META['QUERY_STRING'] | |
104 | import pdb;pdb.set_trace() | |
105 | piece.save() | |
106 | ||
43198a9f | 107 | evaluateurs = candidat.offre_emploi.evaluateurs.all() |
b8b74dee NBV |
108 | for evaluateur in evaluateurs: |
109 | candidat_evaluation = CandidatEvaluation() | |
43198a9f | 110 | candidat_evaluation.candidat = candidat |
b8b74dee NBV |
111 | candidat_evaluation.evaluateur = evaluateur |
112 | candidat_evaluation.save() | |
113 | ||
43198a9f NBV |
114 | try: |
115 | courriel_template = CourrielTemplate.objects.get(id=1) | |
116 | send_templated_email(candidat, courriel_template) | |
117 | except: | |
3a542b85 NBV |
118 | return api_return(STATUS_OK, simplejson.dumps( |
119 | {'candidat_id': candidat.id}), json=True) | |
43198a9f NBV |
120 | except: |
121 | return api_return(STATUS_ERROR) | |
3a542b85 NBV |
122 | return api_return(STATUS_OK, simplejson.dumps( |
123 | {'candidat_id': candidat.id}), json=True) | |
43198a9f NBV |
124 | return api_return(STATUS_ERROR_BADMETHOD) |
125 | ||
f77f4b9b NBV |
126 | |
127 | def api_offre_emploi_liste(self): | |
13b8a64e NBV |
128 | offres_emploi = [] |
129 | for offre in emploi.OffreEmploi.objects.all(): | |
130 | if offre.est_affiche is True and \ | |
131 | offre.statut == "AFFI" and \ | |
132 | offre.date_limite >= date.today(): | |
133 | offres_emploi.append(offre) | |
134 | if offres_emploi: | |
135 | return api_return(STATUS_OK, simplejson.dumps( | |
464e5825 NBV |
136 | [{"id": "%s" % offre.id, |
137 | "est_affiche": "%s" % offre.est_affiche, | |
138 | "statut": "%s" % offre.statut, | |
139 | "nom": "%s" % offre.nom, | |
140 | "resume": "%s" % offre.resume, | |
141 | "description": "%s" % offre.description, | |
142 | "poste_nom": "%s" % offre.poste_nom, | |
143 | "region": "%s" % offre.region.id, | |
144 | "bureau": "%s" % offre.bureau.id, | |
145 | "date_limite": "%s" % offre.date_limite, | |
146 | "duree_affectation": "%s" % offre.duree_affectation, | |
147 | "renumeration": "%s" % offre.renumeration, | |
148 | "debut_affectation": "%s" % offre.debut_affectation, | |
149 | "lieu_affectation": "%s" % offre.lieu_affectation.id} | |
13b8a64e NBV |
150 | for offre in offres_emploi]), json=True) |
151 | return api_return(STATUS_OK) | |
f77f4b9b NBV |
152 | |
153 | def api_offre_emploi(self): | |
154 | try: | |
155 | offre = emploi.OffreEmploi.objects.get(id=self.request.GET.get('id')) | |
156 | except emploi.OffreEmploi.DoesNotExist: | |
157 | return api_return(STATUS_ERROR, "ID d'offre d'emploi invalide") | |
13b8a64e NBV |
158 | |
159 | if offre.est_affiche is True and \ | |
160 | offre.statut == "AFFI" and \ | |
161 | offre.date_limite >= date.today(): | |
162 | return api_return(STATUS_OK, simplejson.dumps( | |
163 | {"id": "%s" % offre.id, | |
164 | "est_affiche": "%s" % offre.est_affiche, | |
165 | "statut": "%s" % offre.statut, | |
166 | "nom": "%s" % offre.nom, | |
167 | "resume": "%s" % offre.resume, | |
168 | "description": "%s" % offre.description, | |
169 | "poste_nom": "%s" % offre.poste_nom, | |
170 | "region": "%s" % offre.region.id, | |
171 | "bureau": "%s" % offre.bureau.id, | |
172 | "date_limite": "%s" % offre.date_limite, | |
173 | "duree_affectation": "%s" % offre.duree_affectation, | |
174 | "renumeration": "%s" % offre.renumeration, | |
175 | "debut_affectation": "%s" % offre.debut_affectation, | |
3a542b85 | 176 | "lieu_affectation": "%s" % offre.lieu_affectation.id}), |
464e5825 | 177 | json=True) |
13b8a64e | 178 | return api_return(STATUS_OK) |
e1c666fe | 179 |