1 # -*- encoding: utf-8 -*
2 from django
.http
import HttpResponse
3 from django
.template
import RequestContext
, Template
4 from django
.shortcuts
import render_to_response
, redirect
, get_object_or_404
5 from django
.utils
import simplejson
6 from django
.contrib
import messages
8 from auf
.django
.emploi
import models
as emploi
9 from auf
.django
.emploi
import forms
as emploiForms
10 from project
.recrutement
.models
import Evaluateur
, CandidatEvaluation
, \
12 from project
.recrutement
.views
import *
17 STATUS_ERROR_NOT_FOUND
= 404
18 STATUS_ERROR_PERMISSIONS
= 403
19 STATUS_ERROR_BADMETHOD
= 405
21 def api(request
, method
):
23 # L'échange d'information doit être possible qu'avec les HOST désirés.
25 #if request.method != 'POST':
26 # return api_return(STATUS_ERROR_BADMETHOD)
29 if hasattr(api
, 'api_%s' % method
):
30 return getattr(api
, 'api_%s' % method
)()
32 return api_return(STATUS_ERROR
)
34 def api_return(status
, text
='', json
=False):
35 content_type
= 'text/html'
36 if status
== STATUS_OK
and json
:
37 content_type
= 'text/json'
39 if status
== STATUS_ERROR
:
41 elif status
== STATUS_ERROR_NOT_FOUND
:
42 text
= 'Resource Not Found'
43 elif status
== STATUS_ERROR_PERMISSIONS
:
44 text
= 'Invalid username or password'
45 elif status
== STATUS_ERROR_BADMETHOD
:
46 text
= 'Invalid request method'
47 elif status
== STATUS_OK
:
50 r
= HttpResponse(status
=status
, content
=text
, content_type
=content_type
)
52 if status
== STATUS_ERROR_BADMETHOD
:
59 def __init__(self
, request
):
60 self
.request
= request
62 def api_candidat_add(self
):
64 offre_emploi
= get_object_or_404(emploi
.OffreEmploi
, id=self
.request
.GET
.get('id'))
65 cand
= emploi
.Candidat()
66 cand
.offre_emploi
= offre_emploi
68 if self
.request
.method
== "POST":
69 form
= emploiForms
.PostulerOffreEmploiForm(self
.request
.POST
,
70 instance
=cand
, offre_emploi
=offre_emploi
)
71 piecesForm
= emploiForms
.CandidatPieceForm(self
.request
.POST
, self
.request
.FILES
,
74 if form
.is_valid() and piecesForm
.is_valid():
76 piecesForm
.instance
= offre
79 """courriel_template = CourrielTemplate.objects.\
81 send_templated_email(cand, courriel_template)
83 evaluateurs
= offre_emploi
.evaluateurs
.all()
84 for evaluateur
in evaluateurs
:
85 candidat_evaluation
= CandidatEvaluation()
86 candidat_evaluation
.candidat
= cand
87 candidat_evaluation
.evaluateur
= evaluateur
88 candidat_evaluation
.save()
90 return api_return(STATUS_OK
)
92 messages
.add_message(self
.request
, messages
.ERROR
,
93 'Il y a des erreurs dans le formulaire.')
95 form
= emploiForms
.PostulerOffreEmploiForm(instance
=cand
,
96 offre_emploi
=offre_emploi
)
97 piecesForm
= emploiForms
.CandidatPieceForm(instance
=cand
)
99 vars.update(dict(form
=form
, candidat
=cand
, piecesForm
=piecesForm
, ))
101 return render_to_response('recrutement/postuler_appel_offre.html', vars,
102 RequestContext(self
.request
))
105 def api_offre_emploi_liste(self
):
106 return api_return(STATUS_OK
, simplejson
.dumps(
107 [{"id": "%s" % offre
.id,
108 "est_affiche": "%s" % offre
.est_affiche
,
109 "statut": "%s" % offre
.statut
,
110 "nom": "%s" % offre
.nom
,
111 "resume": "%s" % offre
.resume
,
112 "description": "%s" % offre
.description
,
113 "poste_nom": "%s" % offre
.poste_nom
,
114 "region": "%s" % offre
.region
.id,
115 "bureau": "%s" % offre
.bureau
.id,
116 "date_limite": "%s" % offre
.date_limite
,
117 "duree_affectation": "%s" % offre
.duree_affectation
,
118 "renumeration": "%s" % offre
.renumeration
,
119 "debut_affectation": "%s" % offre
.debut_affectation
,
120 "lieu_affectation": "%s" % offre
.lieu_affectation
.id}
121 for offre
in emploi
.OffreEmploi
.objects
.all()]), json
=True)
123 def api_offre_emploi(self
):
125 offre
= emploi
.OffreEmploi
.objects
.get(id=self
.request
.GET
.get('id'))
126 except emploi
.OffreEmploi
.DoesNotExist
:
127 return api_return(STATUS_ERROR
, "ID d'offre d'emploi invalide")
128 return api_return(STATUS_OK
, simplejson
.dumps(
129 {"id": "%s" % offre
.id,
130 "est_affiche": "%s" % offre
.est_affiche
,
131 "statut": "%s" % offre
.statut
,
132 "nom": "%s" % offre
.nom
,
133 "resume": "%s" % offre
.resume
,
134 "description": "%s" % offre
.description
,
135 "poste_nom": "%s" % offre
.poste_nom
,
136 "region": "%s" % offre
.region
.id,
137 "bureau": "%s" % offre
.bureau
.id,
138 "date_limite": "%s" % offre
.date_limite
,
139 "duree_affectation": "%s" % offre
.duree_affectation
,
140 "renumeration": "%s" % offre
.renumeration
,
141 "debut_affectation": "%s" % offre
.debut_affectation
,
142 "lieu_affectation": "%s" % offre
.lieu_affectation
.id} ), json
=True)