Commit | Line | Data |
---|---|---|
f77f4b9b | 1 | # -*- encoding: utf-8 -* |
e1c666fe | 2 | from django.http import HttpResponse |
f77f4b9b NBV |
3 | from django.template import Context, RequestContext, Template |
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) | |
24 | ||
25 | api = API(request) | |
26 | if hasattr(api, 'api_%s' % method): | |
27 | return getattr(api, 'api_%s' % method)() | |
28 | ||
29 | return api_return(STATUS_ERROR) | |
30 | ||
e1c666fe NBV |
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' | |
35 | ||
36 | if text is None: | |
37 | if status == STATUS_ERROR: | |
38 | text = '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: | |
46 | text = 'OK' | |
47 | ||
48 | r = HttpResponse(status=status, content=text, content_type=content_type) | |
49 | ||
50 | if status == STATUS_ERROR_BADMETHOD: | |
51 | r.Allow = 'POST' | |
52 | ||
53 | return r | |
54 | ||
55 | ||
56 | class API: | |
e1c666fe NBV |
57 | def __init__(self, request): |
58 | self.request = request | |
59 | ||
f77f4b9b NBV |
60 | def api_candidat_add(self): |
61 | vars = dict() | |
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 | |
65 | ||
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, | |
71 | instance=cand) | |
72 | if form.is_valid() and piecesForm.is_valid(): | |
73 | offre = form.save() | |
74 | piecesForm.instance = offre | |
75 | piecesForm.save() | |
76 | ||
77 | """courriel_template = CourrielTemplate.objects.\ | |
78 | get(nom_modele='Confirmation postulation (automatique)') | |
79 | send_templated_email(candidat, courriel_template) | |
80 | """ | |
81 | messages.add_message(self.request, messages.SUCCESS, | |
82 | "Votre application à l'appel d'offre d'emploi a \ | |
83 | été effectuée.") | |
84 | return api_return(STATUS_OK) | |
85 | else: | |
86 | messages.add_message(self.request, messages.ERROR, | |
87 | 'Il y a des erreurs dans le formulaire.') | |
88 | else: | |
89 | #import pdb;pdb.set_trace() | |
90 | form = emploiForms.PostulerOffreEmploiForm(instance=cand, | |
91 | offre_emploi=offre_emploi) | |
92 | piecesForm = emploiForms.CandidatPieceForm(instance=cand) | |
93 | ||
94 | vars.update(dict(form=form, candidat=cand, piecesForm=piecesForm, )) | |
95 | ||
96 | return render_to_response('recrutement/postuler_appel_offre.html', vars, | |
97 | RequestContext(self.request)) | |
98 | ||
99 | ||
100 | def api_offre_emploi_liste(self): | |
101 | return api_return(STATUS_OK, simplejson.dumps( | |
e1c666fe NBV |
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) | |
f77f4b9b NBV |
112 | |
113 | def api_offre_emploi(self): | |
114 | try: | |
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) | |
e1c666fe | 128 |