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)
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/plain'
33 if status
== STATUS_OK
and json
:
34 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 #import pdb;pdb.set_trace()
68 form
= emploiForms
.PostulerOffreEmploiForm(self
.request
.POST
,
69 instance
=cand
, offre_emploi
=offre_emploi
)
70 piecesForm
= emploiForms
.CandidatPieceForm(self
.request
.POST
, self
.request
.FILES
,
72 if form
.is_valid() and piecesForm
.is_valid():
74 piecesForm
.instance
= offre
77 """courriel_template = CourrielTemplate.objects.\
78 get(nom_modele='Confirmation postulation (automatique)')
79 send_templated_email(candidat, courriel_template)
81 messages
.add_message(self
.request
, messages
.SUCCESS
,
82 "Votre application à l'appel d'offre d'emploi a \
84 return api_return(STATUS_OK
)
86 messages
.add_message(self
.request
, messages
.ERROR
,
87 'Il y a des erreurs dans le formulaire.')
89 #import pdb;pdb.set_trace()
90 form
= emploiForms
.PostulerOffreEmploiForm(instance
=cand
,
91 offre_emploi
=offre_emploi
)
92 piecesForm
= emploiForms
.CandidatPieceForm(instance
=cand
)
94 vars.update(dict(form
=form
, candidat
=cand
, piecesForm
=piecesForm
, ))
96 return render_to_response('recrutement/postuler_appel_offre.html', vars,
97 RequestContext(self
.request
))
100 def api_offre_emploi_liste(self
):
101 return api_return(STATUS_OK
, simplejson
.dumps(
102 [{"id": "%s" % offre
.id,
103 "nom": "%s" % offre
.nom
,
104 "resume": "%s" % offre
.resume
,
105 "description": "%s" % offre
.description
,
106 "date_limite": "%s" % offre
.date_limite
,
107 "duree_affectation": "%s" % offre
.duree_affectation
,
108 "renumeration": "%s" % offre
.renumeration
,
109 "debut_affectation": "%s" % offre
.debut_affectation
,
110 "lieu_affectation": "%s" % offre
.lieu_affectation
}
111 for offre
in emploi
.OffreEmploi
.objects
.all()]), json
=True)
113 def api_offre_emploi(self
):
115 offre
= emploi
.OffreEmploi
.objects
.get(id=self
.request
.GET
.get('id'))
116 except emploi
.OffreEmploi
.DoesNotExist
:
117 return api_return(STATUS_ERROR
, "ID d'offre d'emploi invalide")
118 return api_return(STATUS_OK
, simplejson
.dumps(
119 {"id": "%s" % offre
.id,
120 "nom": "%s" % offre
.nom
,
121 "resume": "%s" % offre
.resume
,
122 "description": "%s" % offre
.description
,
123 "date_limite": "%s" % offre
.date_limite
,
124 "duree_affectation": "%s" % offre
.duree_affectation
,
125 "renumeration": "%s" % offre
.renumeration
,
126 "debut_affectation": "%s" % offre
.debut_affectation
,
127 "lieu_affectation": "%s" % offre
.lieu_affectation
}), json
=True)