041beaaac5cab396be622144721e4b0cde8b814d
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
14 STATUS_ERROR_NOT_FOUND
= 404
15 STATUS_ERROR_PERMISSIONS
= 403
16 STATUS_ERROR_BADMETHOD
= 405
18 def api(request
, method
):
20 # L'échange d'information doit être possible qu'avec les HOST désirés.
22 #if request.method != 'POST':
23 # return api_return(STATUS_ERROR_BADMETHOD)
26 if hasattr(api
, 'api_%s' % method
):
27 return getattr(api
, 'api_%s' % method
)()
29 return api_return(STATUS_ERROR
)
31 def api_return(status
, text
='', json
=False):
32 content_type
= 'text/html'
33 if status
== STATUS_OK
and json
:
34 content_type
= 'text/json'
36 if status
== STATUS_ERROR
:
38 elif status
== STATUS_ERROR_NOT_FOUND
:
39 text
= 'Resource Not Found'
40 elif status
== STATUS_ERROR_PERMISSIONS
:
41 text
= 'Invalid username or password'
42 elif status
== STATUS_ERROR_BADMETHOD
:
43 text
= 'Invalid request method'
44 elif status
== STATUS_OK
:
47 r
= HttpResponse(status
=status
, content
=text
, content_type
=content_type
)
49 if status
== STATUS_ERROR_BADMETHOD
:
56 def __init__(self
, request
):
57 self
.request
= request
59 def api_candidat_add(self
):
61 offre_emploi
= get_object_or_404(emploi
.OffreEmploi
, id=self
.request
.GET
.get('id'))
62 cand
= emploi
.Candidat()
63 cand
.offre_emploi
= offre_emploi
65 if self
.request
.method
== "POST":
66 form
= emploiForms
.PostulerOffreEmploiForm(self
.request
.POST
,
67 instance
=cand
, offre_emploi
=offre_emploi
)
68 piecesForm
= emploiForms
.CandidatPieceForm(self
.request
.POST
, self
.request
.FILES
,
70 if form
.is_valid() and piecesForm
.is_valid():
72 piecesForm
.instance
= offre
75 """courriel_template = CourrielTemplate.objects.\
76 get(nom_modele='Confirmation postulation (automatique)')
77 send_templated_email(candidat, courriel_template)
79 return api_return(STATUS_OK
)
81 messages
.add_message(self
.request
, messages
.ERROR
,
82 'Il y a des erreurs dans le formulaire.')
84 form
= emploiForms
.PostulerOffreEmploiForm(instance
=cand
,
85 offre_emploi
=offre_emploi
)
86 piecesForm
= emploiForms
.CandidatPieceForm(instance
=cand
)
88 vars.update(dict(form
=form
, candidat
=cand
, piecesForm
=piecesForm
, ))
90 return render_to_response('recrutement/postuler_appel_offre.html', vars,
91 RequestContext(self
.request
))
94 def api_offre_emploi_liste(self
):
95 return api_return(STATUS_OK
, simplejson
.dumps(
96 [{"id": "%s" % offre
.id,
97 "est_affiche": "%s" % offre
.est_affiche
,
98 "statut": "%s" % offre
.statut
,
99 "nom": "%s" % offre
.nom
,
100 "resume": "%s" % offre
.resume
,
101 "description": "%s" % offre
.description
,
102 # "poste": "%s [%s]" %
103 "region": "%s" % offre
.region
.id,
104 "bureau": "%s" % offre
.bureau
.id,
105 "date_limite": "%s" % offre
.date_limite
,
106 "duree_affectation": "%s" % offre
.duree_affectation
,
107 "renumeration": "%s" % offre
.renumeration
,
108 "debut_affectation": "%s" % offre
.debut_affectation
,
109 "lieu_affectation": "%s" % offre
.lieu_affectation
.id}
110 for offre
in emploi
.OffreEmploi
.objects
.all()]), json
=True)
112 def api_offre_emploi(self
):
114 offre
= emploi
.OffreEmploi
.objects
.get(id=self
.request
.GET
.get('id'))
115 except emploi
.OffreEmploi
.DoesNotExist
:
116 return api_return(STATUS_ERROR
, "ID d'offre d'emploi invalide")
117 return api_return(STATUS_OK
, simplejson
.dumps(
118 {"id": "%s" % offre
.id,
119 "est_affiche": "%s" % offre
.est_affiche
,
120 "statut": "%s" % offre
.statut
,
121 "nom": "%s" % offre
.nom
,
122 "resume": "%s" % offre
.resume
,
123 "description": "%s" % offre
.description
,
124 "region": "%s" % offre
.region
.id,
125 "bureau": "%s" % offre
.bureau
.id,
126 "date_limite": "%s" % offre
.date_limite
,
127 "duree_affectation": "%s" % offre
.duree_affectation
,
128 "renumeration": "%s" % offre
.renumeration
,
129 "debut_affectation": "%s" % offre
.debut_affectation
,
130 "lieu_affectation": "%s" % offre
.lieu_affectation
.id} ), json
=True)