| 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 |
| 7 | |
| 8 | from auf.django.emploi import models as emploi |
| 9 | from auf.django.emploi import forms as emploiForms |
| 10 | from project.recrutement.models import Evaluateur, CandidatEvaluation, \ |
| 11 | CourrielTemplate |
| 12 | from project.recrutement.views import * |
| 13 | |
| 14 | STATUS_OK = 200 |
| 15 | |
| 16 | STATUS_ERROR = 400 |
| 17 | STATUS_ERROR_NOT_FOUND = 404 |
| 18 | STATUS_ERROR_PERMISSIONS = 403 |
| 19 | STATUS_ERROR_BADMETHOD = 405 |
| 20 | |
| 21 | def api(request, method): |
| 22 | # TODO: Sécurité : |
| 23 | # L'échange d'information doit être possible qu'avec les HOST désirés. |
| 24 | |
| 25 | #if request.method != 'POST': |
| 26 | # return api_return(STATUS_ERROR_BADMETHOD) |
| 27 | |
| 28 | api = API(request) |
| 29 | if hasattr(api, 'api_%s' % method): |
| 30 | return getattr(api, 'api_%s' % method)() |
| 31 | |
| 32 | return api_return(STATUS_ERROR) |
| 33 | |
| 34 | def api_return(status, text='', json=False): |
| 35 | content_type = 'text/html' |
| 36 | if status == STATUS_OK and json: |
| 37 | content_type = 'text/json' |
| 38 | if text is None: |
| 39 | if status == STATUS_ERROR: |
| 40 | text = 'Error' |
| 41 | elif status == STATUS_ERROR_NOT_FOUND: |
| 42 | text = 'Resource Not Found' |
| 43 | elif status == STATUS_ERROR_PERMISSIONS: |
| 44 | text = 'Invalid username or password' |
| 45 | elif status == STATUS_ERROR_BADMETHOD: |
| 46 | text = 'Invalid request method' |
| 47 | elif status == STATUS_OK: |
| 48 | text = 'OK' |
| 49 | |
| 50 | r = HttpResponse(status=status, content=text, content_type=content_type) |
| 51 | |
| 52 | if status == STATUS_ERROR_BADMETHOD: |
| 53 | r.Allow = 'POST' |
| 54 | |
| 55 | return r |
| 56 | |
| 57 | |
| 58 | class API: |
| 59 | def __init__(self, request): |
| 60 | self.request = request |
| 61 | |
| 62 | def api_candidat_add(self): |
| 63 | vars = dict() |
| 64 | offre_emploi = get_object_or_404(emploi.OffreEmploi, id=self.request.GET.get('id')) |
| 65 | cand = emploi.Candidat() |
| 66 | cand.offre_emploi = offre_emploi |
| 67 | |
| 68 | if self.request.method == "POST": |
| 69 | form = emploiForms.PostulerOffreEmploiForm(self.request.POST, |
| 70 | instance=cand, offre_emploi=offre_emploi) |
| 71 | piecesForm = emploiForms.CandidatPieceForm(self.request.POST, self.request.FILES, |
| 72 | instance=cand) |
| 73 | |
| 74 | if form.is_valid() and piecesForm.is_valid(): |
| 75 | offre = form.save() |
| 76 | piecesForm.instance = offre |
| 77 | piecesForm.save() |
| 78 | |
| 79 | """courriel_template = CourrielTemplate.objects.\ |
| 80 | get(id=1) |
| 81 | send_templated_email(cand, courriel_template) |
| 82 | """ |
| 83 | evaluateurs = offre_emploi.evaluateurs.all() |
| 84 | for evaluateur in evaluateurs: |
| 85 | candidat_evaluation = CandidatEvaluation() |
| 86 | candidat_evaluation.candidat = cand |
| 87 | candidat_evaluation.evaluateur = evaluateur |
| 88 | candidat_evaluation.save() |
| 89 | |
| 90 | return api_return(STATUS_OK) |
| 91 | else: |
| 92 | messages.add_message(self.request, messages.ERROR, |
| 93 | 'Il y a des erreurs dans le formulaire.') |
| 94 | else: |
| 95 | form = emploiForms.PostulerOffreEmploiForm(instance=cand, |
| 96 | offre_emploi=offre_emploi) |
| 97 | piecesForm = emploiForms.CandidatPieceForm(instance=cand) |
| 98 | |
| 99 | vars.update(dict(form=form, candidat=cand, piecesForm=piecesForm, )) |
| 100 | |
| 101 | return render_to_response('recrutement/postuler_appel_offre.html', vars, |
| 102 | RequestContext(self.request)) |
| 103 | |
| 104 | |
| 105 | def api_offre_emploi_liste(self): |
| 106 | return api_return(STATUS_OK, simplejson.dumps( |
| 107 | [{"id": "%s" % offre.id, |
| 108 | "est_affiche": "%s" % offre.est_affiche, |
| 109 | "statut": "%s" % offre.statut, |
| 110 | "nom": "%s" % offre.nom, |
| 111 | "resume": "%s" % offre.resume, |
| 112 | "description": "%s" % offre.description, |
| 113 | # "poste": "%s [%s]" % |
| 114 | "region": "%s" % offre.region.id, |
| 115 | "bureau": "%s" % offre.bureau.id, |
| 116 | "date_limite": "%s" % offre.date_limite, |
| 117 | "duree_affectation": "%s" % offre.duree_affectation, |
| 118 | "renumeration": "%s" % offre.renumeration, |
| 119 | "debut_affectation": "%s" % offre.debut_affectation, |
| 120 | "lieu_affectation": "%s" % offre.lieu_affectation.id} |
| 121 | for offre in emploi.OffreEmploi.objects.all()]), json=True) |
| 122 | |
| 123 | def api_offre_emploi(self): |
| 124 | try: |
| 125 | offre = emploi.OffreEmploi.objects.get(id=self.request.GET.get('id')) |
| 126 | except emploi.OffreEmploi.DoesNotExist: |
| 127 | return api_return(STATUS_ERROR, "ID d'offre d'emploi invalide") |
| 128 | return api_return(STATUS_OK, simplejson.dumps( |
| 129 | {"id": "%s" % offre.id, |
| 130 | "est_affiche": "%s" % offre.est_affiche, |
| 131 | "statut": "%s" % offre.statut, |
| 132 | "nom": "%s" % offre.nom, |
| 133 | "resume": "%s" % offre.resume, |
| 134 | "description": "%s" % offre.description, |
| 135 | "region": "%s" % offre.region.id, |
| 136 | "bureau": "%s" % offre.bureau.id, |
| 137 | "date_limite": "%s" % offre.date_limite, |
| 138 | "duree_affectation": "%s" % offre.duree_affectation, |
| 139 | "renumeration": "%s" % offre.renumeration, |
| 140 | "debut_affectation": "%s" % offre.debut_affectation, |
| 141 | "lieu_affectation": "%s" % offre.lieu_affectation.id} ), json=True) |
| 142 | |