655f22fdd10035c1b79fb2e96c1268a27db8dd20
1 # -*- encoding: utf-8 -*
2 from django
.http
import HttpResponse
3 from django
.template
import Context
, 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)
27 if hasattr(api
, 'api_%s' % method
):
28 return getattr(api
, 'api_%s' % method
)()
30 return api_return(STATUS_ERROR
)
32 def api_return(status
, text
='', json
=False):
33 content_type
= 'text/plain'
34 if status
== STATUS_OK
and json
:
35 content_type
= 'text/json'
37 if status
== STATUS_ERROR
:
39 elif status
== STATUS_ERROR_NOT_FOUND
:
40 text
= 'Resource Not Found'
41 elif status
== STATUS_ERROR_PERMISSIONS
:
42 text
= 'Invalid username or password'
43 elif status
== STATUS_ERROR_BADMETHOD
:
44 text
= 'Invalid request method'
45 elif status
== STATUS_OK
:
48 r
= HttpResponse(status
=status
, content
=text
, content_type
=content_type
)
50 if status
== STATUS_ERROR_BADMETHOD
:
57 def __init__(self
, request
):
58 self
.request
= request
60 def api_candidat_add(self
):
62 offre_emploi
= get_object_or_404(emploi
.OffreEmploi
, id=self
.request
.GET
.get('id'))
63 cand
= emploi
.Candidat()
64 cand
.offre_emploi
= offre_emploi
66 if self
.request
.method
== "POST":
67 form
= emploiForms
.PostulerOffreEmploiForm(self
.request
.POST
,
68 instance
=cand
, offre_emploi
=offre_emploi
)
69 piecesForm
= emploiForms
.CandidatPieceForm(self
.request
.POST
, self
.request
.FILES
,
71 if form
.is_valid() and piecesForm
.is_valid():
73 piecesForm
.instance
= offre
76 """courriel_template = CourrielTemplate.objects.\
77 get(nom_modele='Confirmation postulation (automatique)')
78 send_templated_email(candidat, courriel_template)
80 messages
.add_message(self
.request
, messages
.SUCCESS
,
81 "Votre application à l'appel d'offre d'emploi a \
83 return api_return(STATUS_OK
)
85 messages
.add_message(self
.request
, messages
.ERROR
,
86 'Il y a des erreurs dans le formulaire.')
88 form
= emploiForms
.PostulerOffreEmploiForm(instance
=cand
,
89 offre_emploi
=offre_emploi
)
90 piecesForm
= emploiForms
.CandidatPieceForm(instance
=cand
)
92 vars.update(dict(form
=form
, candidat
=cand
, piecesForm
=piecesForm
, ))
94 return render_to_response('recrutement/postuler_appel_offre.html', vars,
95 RequestContext(self
.request
))
98 def api_offre_emploi_liste(self
):
99 return api_return(STATUS_OK
, simplejson
.dumps(
100 [{"id": "%s" % offre
.id,
101 "nom": "%s" % offre
.nom
,
102 "resume": "%s" % offre
.resume
,
103 "description": "%s" % offre
.description
,
104 "date_limite": "%s" % offre
.date_limite
,
105 "duree_affectation": "%s" % offre
.duree_affectation
,
106 "renumeration": "%s" % offre
.renumeration
,
107 "debut_affectation": "%s" % offre
.debut_affectation
,
108 "lieu_affectation": "%s" % offre
.lieu_affectation
}
109 for offre
in emploi
.OffreEmploi
.objects
.all()]), json
=True)
111 def api_offre_emploi(self
):
113 offre
= emploi
.OffreEmploi
.objects
.get(id=self
.request
.GET
.get('id'))
114 except emploi
.OffreEmploi
.DoesNotExist
:
115 return api_return(STATUS_ERROR
, "ID d'offre d'emploi invalide")
116 return api_return(STATUS_OK
, simplejson
.dumps(
117 {"id": "%s" % offre
.id,
118 "nom": "%s" % offre
.nom
,
119 "resume": "%s" % offre
.resume
,
120 "description": "%s" % offre
.description
,
121 "date_limite": "%s" % offre
.date_limite
,
122 "duree_affectation": "%s" % offre
.duree_affectation
,
123 "renumeration": "%s" % offre
.renumeration
,
124 "debut_affectation": "%s" % offre
.debut_affectation
,
125 "lieu_affectation": "%s" % offre
.lieu_affectation
}), json
=True)