Commit | Line | Data |
---|---|---|
f77f4b9b | 1 | # -*- encoding: utf-8 -* |
e1c666fe | 2 | from django.http import HttpResponse |
d46075cb | 3 | from django.template import RequestContext, Template |
f77f4b9b | 4 | from django.shortcuts import render_to_response, redirect, get_object_or_404 |
e1c666fe | 5 | from django.utils import simplejson |
f77f4b9b NBV |
6 | from django.contrib import messages |
7 | ||
e1c666fe | 8 | from auf.django.emploi import models as emploi |
f77f4b9b | 9 | from auf.django.emploi import forms as emploiForms |
e1c666fe NBV |
10 | |
11 | STATUS_OK = 200 | |
12 | ||
13 | STATUS_ERROR = 400 | |
14 | STATUS_ERROR_NOT_FOUND = 404 | |
15 | STATUS_ERROR_PERMISSIONS = 403 | |
16 | STATUS_ERROR_BADMETHOD = 405 | |
17 | ||
f77f4b9b NBV |
18 | def api(request, method): |
19 | # TODO: Sécurité : | |
20 | # L'échange d'information doit être possible qu'avec les HOST désirés. | |
21 | ||
22 | #if request.method != 'POST': | |
23 | # return api_return(STATUS_ERROR_BADMETHOD) | |
14e06ff6 | 24 | |
f77f4b9b NBV |
25 | api = API(request) |
26 | if hasattr(api, 'api_%s' % method): | |
27 | return getattr(api, 'api_%s' % method)() | |
14e06ff6 | 28 | |
f77f4b9b NBV |
29 | return api_return(STATUS_ERROR) |
30 | ||
e1c666fe | 31 | def api_return(status, text='', json=False): |
d46075cb | 32 | content_type = 'text/html' |
e1c666fe NBV |
33 | if status == STATUS_OK and json: |
34 | content_type = 'text/json' | |
e1c666fe NBV |
35 | if text is None: |
36 | if status == STATUS_ERROR: | |
37 | text = '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: | |
45 | text = 'OK' | |
46 | ||
47 | r = HttpResponse(status=status, content=text, content_type=content_type) | |
48 | ||
49 | if status == STATUS_ERROR_BADMETHOD: | |
50 | r.Allow = 'POST' | |
51 | ||
52 | return r | |
53 | ||
54 | ||
55 | class API: | |
e1c666fe NBV |
56 | def __init__(self, request): |
57 | self.request = request | |
58 | ||
f77f4b9b NBV |
59 | def api_candidat_add(self): |
60 | vars = dict() | |
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 | |
64 | ||
65 | if self.request.method == "POST": | |
f77f4b9b NBV |
66 | form = emploiForms.PostulerOffreEmploiForm(self.request.POST, |
67 | instance=cand, offre_emploi=offre_emploi) | |
8e0d552d NBV |
68 | piecesForm = emploiForms.CandidatPieceForm(self.request.POST, self.request.FILES, |
69 | instance=cand) | |
70 | if form.is_valid() and piecesForm.is_valid(): | |
f77f4b9b | 71 | offre = form.save() |
8e0d552d NBV |
72 | piecesForm.instance = offre |
73 | piecesForm.save() | |
f77f4b9b NBV |
74 | |
75 | """courriel_template = CourrielTemplate.objects.\ | |
76 | get(nom_modele='Confirmation postulation (automatique)') | |
77 | send_templated_email(candidat, courriel_template) | |
78 | """ | |
f77f4b9b NBV |
79 | return api_return(STATUS_OK) |
80 | else: | |
81 | messages.add_message(self.request, messages.ERROR, | |
82 | 'Il y a des erreurs dans le formulaire.') | |
83 | else: | |
f77f4b9b NBV |
84 | form = emploiForms.PostulerOffreEmploiForm(instance=cand, |
85 | offre_emploi=offre_emploi) | |
8e0d552d | 86 | piecesForm = emploiForms.CandidatPieceForm(instance=cand) |
f77f4b9b | 87 | |
8e0d552d | 88 | vars.update(dict(form=form, candidat=cand, piecesForm=piecesForm, )) |
f77f4b9b NBV |
89 | |
90 | return render_to_response('recrutement/postuler_appel_offre.html', vars, | |
91 | RequestContext(self.request)) | |
92 | ||
93 | ||
94 | def api_offre_emploi_liste(self): | |
95 | return api_return(STATUS_OK, simplejson.dumps( | |
e1c666fe | 96 | [{"id": "%s" % offre.id, |
50fe3590 NBV |
97 | "est_affiche": "%s" % offre.est_affiche, |
98 | "statut": "%s" % offre.statut, | |
e1c666fe NBV |
99 | "nom": "%s" % offre.nom, |
100 | "resume": "%s" % offre.resume, | |
101 | "description": "%s" % offre.description, | |
a029f641 | 102 | # "poste": "%s [%s]" % |
d46075cb NBV |
103 | "region": "%s" % offre.region.id, |
104 | "bureau": "%s" % offre.bureau.id, | |
e1c666fe NBV |
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, | |
d46075cb | 109 | "lieu_affectation": "%s" % offre.lieu_affectation.id} |
e1c666fe | 110 | for offre in emploi.OffreEmploi.objects.all()]), json=True) |
f77f4b9b NBV |
111 | |
112 | def api_offre_emploi(self): | |
113 | try: | |
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( | |
50fe3590 NBV |
118 | {"id": "%s" % offre.id, |
119 | "est_affiche": "%s" % offre.est_affiche, | |
120 | "statut": "%s" % offre.statut, | |
f77f4b9b NBV |
121 | "nom": "%s" % offre.nom, |
122 | "resume": "%s" % offre.resume, | |
123 | "description": "%s" % offre.description, | |
d46075cb NBV |
124 | "region": "%s" % offre.region.id, |
125 | "bureau": "%s" % offre.bureau.id, | |
f77f4b9b NBV |
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, | |
d46075cb | 130 | "lieu_affectation": "%s" % offre.lieu_affectation.id} ), json=True) |
e1c666fe | 131 |