a6adbdfb609b285a1dd3b45b6444070b8d54a36f
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
)
70 if self
.request
.method
== "POST":
72 candidat
= emploi
.Candidat()
73 candidat
.offre_emploi
= offre
74 candidat
.nom
= self
.request
.POST
['nom']
75 candidat
.prenom
= self
.request
.POST
['prenom']
76 candidat
.genre
= self
.request
.POST
['genre']
77 candidat
.nationalite
= ref
.Pays
.objects
.get\
78 (id=self
.request
.POST
['nationalite'])
79 candidat
.situation_famille
= self
.request
.POST
['situation_famille']
80 candidat
.nombre_dependant
= self
.request
.POST
['nombre_dependant']
81 candidat
.niveau_diplome
= self
.request
.POST
['niveau_diplome']
82 candidat
.employeur_actuel
= self
.request
.POST
['employeur_actuel']
83 candidat
.poste_actuel
= self
.request
.POST
['poste_actuel']
84 candidat
.domaine_professionnel
= self
.request
.\
85 POST
['domaine_professionnel']
86 candidat
.telephone
= self
.request
.POST
['telephone']
87 candidat
.email
= self
.request
.POST
['email']
88 candidat
.adresse
= self
.request
.POST
['adresse']
89 candidat
.ville
= self
.request
.POST
['ville']
90 candidat
.etat_province
= self
.request
.POST
['etat_province']
91 candidat
.code_postal
= self
.request
.POST
['code_postal']
92 candidat
.pays
= ref
.Pays
.objects
.get(id=self
.request
.POST
['pays'])
95 evaluateurs
= candidat
.offre_emploi
.evaluateurs
.all()
96 for evaluateur
in evaluateurs
:
97 candidat_evaluation
= CandidatEvaluation()
98 candidat_evaluation
.candidat
= candidat
99 candidat_evaluation
.evaluateur
= evaluateur
100 candidat_evaluation
.save()
103 courriel_template
= CourrielTemplate
.objects
.get(id=1)
104 send_templated_email(candidat
, courriel_template
)
106 return api_return(STATUS_OK
)
108 return api_return(STATUS_ERROR
)
109 return api_return(STATUS_OK
)
110 return api_return(STATUS_ERROR_BADMETHOD
)
113 def api_offre_emploi_liste(self
):
115 for offre
in emploi
.OffreEmploi
.objects
.all():
116 if offre
.est_affiche
is True and \
117 offre
.statut
== "AFFI" and \
118 offre
.date_limite
>= date
.today():
119 offres_emploi
.append(offre
)
121 return api_return(STATUS_OK
, simplejson
.dumps(
122 [{"id": "%s" % offre
.id,
123 "est_affiche": "%s" % offre
.est_affiche
,
124 "statut": "%s" % offre
.statut
,
125 "nom": "%s" % offre
.nom
,
126 "resume": "%s" % offre
.resume
,
127 "description": "%s" % offre
.description
,
128 "poste_nom": "%s" % offre
.poste_nom
,
129 "region": "%s" % offre
.region
.id,
130 "bureau": "%s" % offre
.bureau
.id,
131 "date_limite": "%s" % offre
.date_limite
,
132 "duree_affectation": "%s" % offre
.duree_affectation
,
133 "renumeration": "%s" % offre
.renumeration
,
134 "debut_affectation": "%s" % offre
.debut_affectation
,
135 "lieu_affectation": "%s" % offre
.lieu_affectation
.id}
136 for offre
in offres_emploi
]), json
=True)
137 return api_return(STATUS_OK
)
139 def api_offre_emploi(self
):
141 offre
= emploi
.OffreEmploi
.objects
.get(id=self
.request
.GET
.get('id'))
142 except emploi
.OffreEmploi
.DoesNotExist
:
143 return api_return(STATUS_ERROR
, "ID d'offre d'emploi invalide")
145 if offre
.est_affiche
is True and \
146 offre
.statut
== "AFFI" and \
147 offre
.date_limite
>= date
.today():
148 return api_return(STATUS_OK
, simplejson
.dumps(
149 {"id": "%s" % offre
.id,
150 "est_affiche": "%s" % offre
.est_affiche
,
151 "statut": "%s" % offre
.statut
,
152 "nom": "%s" % offre
.nom
,
153 "resume": "%s" % offre
.resume
,
154 "description": "%s" % offre
.description
,
155 "poste_nom": "%s" % offre
.poste_nom
,
156 "region": "%s" % offre
.region
.id,
157 "bureau": "%s" % offre
.bureau
.id,
158 "date_limite": "%s" % offre
.date_limite
,
159 "duree_affectation": "%s" % offre
.duree_affectation
,
160 "renumeration": "%s" % offre
.renumeration
,
161 "debut_affectation": "%s" % offre
.debut_affectation
,
162 "lieu_affectation": "%s" % offre
.lieu_affectation
.id} ),
164 return api_return(STATUS_OK
)