1 # -*- encoding: utf-8 -*
2 from datetime
import date
3 from django
.http
import HttpResponse
4 from django
.template
import RequestContext
, Template
5 from django
.shortcuts
import render_to_response
, redirect
, get_object_or_404
6 from django
.utils
import simplejson
7 from django
.contrib
import messages
9 import datamaster_modeles
.models
as ref
10 from auf
.django
.emploi
import models
as emploi
11 from auf
.django
.emploi
import forms
as emploiForms
12 from project
.recrutement
.models
import Evaluateur
, CandidatEvaluation
, \
14 from project
.recrutement
.views
import send_templated_email
19 STATUS_ERROR_NOT_FOUND
= 404
20 STATUS_ERROR_PERMISSIONS
= 403
21 STATUS_ERROR_BADMETHOD
= 405
23 def api(request
, method
, offre_id
=None):
25 # L'échange d'information doit être possible qu'avec les HOST désirés.
27 #if request.method != 'POST':
28 # return api_return(STATUS_ERROR_BADMETHOD)
31 if hasattr(api
, 'api_%s' % method
):
33 return getattr(api
, 'api_%s' % method
)()
35 return api
.api_candidat_add(offre_id
)
37 return api_return(STATUS_ERROR
)
39 def api_return(status
, text
='', json
=False):
40 content_type
= 'text/html'
41 if status
== STATUS_OK
and json
:
42 content_type
= 'text/json'
44 if status
== STATUS_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
:
55 r
= HttpResponse(status
=status
, content
=text
, content_type
=content_type
)
57 if status
== STATUS_ERROR_BADMETHOD
:
64 def __init__(self
, request
):
65 self
.request
= request
67 def api_candidat_add(self
, offre_id
):
69 offre
= emploi
.OffreEmploi
.objects
.get(id=offre_id
)
71 if self
.request
.method
== "POST":
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']
78 candidat
.nationalite
= ref
.Pays
.objects
.get\
79 (id=self
.request
.POST
['nationalite'])
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']
85 candidat
.domaine_professionnel
= self
.request
.\
86 POST
['domaine_professionnel']
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'])
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 piece
.nom
= self
.request
.POST
['candidat_piece-' + str(i
) + '-nom']
101 piece
.path
= self
.request
.META
['QUERY_STRING']
104 evaluateurs
= candidat
.offre_emploi
.evaluateurs
.all()
105 for evaluateur
in evaluateurs
:
106 candidat_evaluation
= CandidatEvaluation()
107 candidat_evaluation
.candidat
= candidat
108 candidat_evaluation
.evaluateur
= evaluateur
109 candidat_evaluation
.save()
112 courriel_template
= CourrielTemplate
.objects
.get(id=1)
113 send_templated_email(candidat
, courriel_template
)
115 return api_return(STATUS_OK
, simplejson
.dumps(
116 {'candidat_id': candidat
.id}), json
=True)
118 return api_return(STATUS_ERROR
)
119 return api_return(STATUS_OK
, simplejson
.dumps(
120 {'candidat_id': candidat
.id}), json
=True)
121 return api_return(STATUS_ERROR_BADMETHOD
)
124 def api_offre_emploi_liste(self
):
126 for offre
in emploi
.OffreEmploi
.objects
.all():
127 if offre
.est_affiche
is True and \
128 offre
.statut
== "AFFI" and \
129 offre
.date_limite
>= date
.today():
130 offres_emploi
.append(offre
)
132 return api_return(STATUS_OK
, simplejson
.dumps(
133 [{"id": "%s" % offre
.id,
134 "est_affiche": "%s" % offre
.est_affiche
,
135 "statut": "%s" % offre
.statut
,
136 "nom": "%s" % offre
.nom
,
137 "resume": "%s" % offre
.resume
,
138 "description": "%s" % offre
.description
,
139 "poste_nom": "%s" % offre
.poste_nom
,
140 "region": "%s" % offre
.region
.id,
141 "bureau": "%s" % offre
.bureau
.id,
142 "date_limite": "%s" % offre
.date_limite
,
143 "duree_affectation": "%s" % offre
.duree_affectation
,
144 "renumeration": "%s" % offre
.renumeration
,
145 "debut_affectation": "%s" % offre
.debut_affectation
,
146 "lieu_affectation": "%s" % offre
.lieu_affectation
.id}
147 for offre
in offres_emploi
]), json
=True)
148 return api_return(STATUS_OK
)
150 def api_offre_emploi(self
):
152 offre
= emploi
.OffreEmploi
.objects
.get(id=self
.request
.GET
.get('id'))
153 except emploi
.OffreEmploi
.DoesNotExist
:
154 return api_return(STATUS_ERROR
, "ID d'offre d'emploi invalide")
156 if offre
.est_affiche
is True and \
157 offre
.statut
== "AFFI" and \
158 offre
.date_limite
>= date
.today():
159 return api_return(STATUS_OK
, simplejson
.dumps(
160 {"id": "%s" % offre
.id,
161 "est_affiche": "%s" % offre
.est_affiche
,
162 "statut": "%s" % offre
.statut
,
163 "nom": "%s" % offre
.nom
,
164 "resume": "%s" % offre
.resume
,
165 "description": "%s" % offre
.description
,
166 "poste_nom": "%s" % offre
.poste_nom
,
167 "region": "%s" % offre
.region
.id,
168 "bureau": "%s" % offre
.bureau
.id,
169 "date_limite": "%s" % offre
.date_limite
,
170 "duree_affectation": "%s" % offre
.duree_affectation
,
171 "renumeration": "%s" % offre
.renumeration
,
172 "debut_affectation": "%s" % offre
.debut_affectation
,
173 "lieu_affectation": "%s" % offre
.lieu_affectation
.id}),
175 return api_return(STATUS_OK
)