Débug de pièces jointes
[auf_django_emploi.git] / auf / django / emploi / api.py
CommitLineData
d691a5d3
NBV
1# -*- encoding: utf-8 -*
2from django.shortcuts import render_to_response, redirect, get_object_or_404
448f1dfa 3from django.template import Context, RequestContext
d691a5d3
NBV
4
5from django.utils import simplejson
6from auf.django.emploi import models as emploi
449e9e93 7from restkit import request as req
36b8eabb
NBV
8from restkit import Resource
9from httplib2 import Http
10from urllib import urlencode
448f1dfa 11import datamaster_modeles.models as ref
3d78c976
NBV
12from poster.encode import MultipartParam
13from poster.encode import multipart_encode
14import urllib2
15
16
17STATUS_OK = '200 OK'
d691a5d3
NBV
18
19class API:
20 def __init__(self, request):
21 self.request = request
22
23 def offre_emploi_liste(self):
b7fb5786 24 url = "http://127.0.0.1:8000/api/offre_emploi_liste/"
449e9e93 25 r = req(url)
d691a5d3 26 liste_json = r.body_string()
1ff71303
NBV
27 try:
28 liste_offres = simplejson.loads(liste_json)
29 except:
30 return []
d691a5d3
NBV
31 obj_offres_emploi = []
32
33 for offre_dict in liste_offres:
34 offre = emploi.OffreEmploi()
8acb3cd3
NBV
35 offre.est_affiche = offre_dict['est_affiche']
36 offre.statut = offre_dict['statut']
d691a5d3
NBV
37 offre.nom = offre_dict['nom']
38 offre.resume = offre_dict['resume']
39 offre.description = offre_dict['description']
44accb6d 40 offre.poste_nom = offre_dict['poste_nom']
448f1dfa
NBV
41 offre.date_limite = offre_dict['date_limite']
42 offre.region = ref.Region.objects.get(id=offre_dict['region'])
43 offre.bureau = ref.Bureau.objects.get(id=offre_dict['bureau'])
d691a5d3
NBV
44 offre.duree_affectation = offre_dict['duree_affectation']
45 offre.renumeration = offre_dict['renumeration']
46 offre.debut_affectation = offre_dict['debut_affectation']
448f1dfa 47 offre.lieu_affectation = ref.Implantation.objects.get(id=offre_dict['lieu_affectation'])
d691a5d3
NBV
48 obj_offres_emploi.append(offre)
49 return obj_offres_emploi
50
51 def offre_emploi(self, offre_id):
b7fb5786 52 url = "http://127.0.0.1:8000/api/offre_emploi/?id=%s"
449e9e93 53 r = req(url % (offre_id))
d691a5d3 54 offre_json = r.body_string()
1ff71303
NBV
55
56 try:
57 offre_dict = simplejson.loads(offre_json)
58 except:
59 return []
448f1dfa 60 obj_offres_emploi = []
d691a5d3 61
448f1dfa 62 offre = emploi.OffreEmploi()
8acb3cd3
NBV
63 offre.est_affiche = offre_dict['est_affiche']
64 offre.statut = offre_dict['statut']
448f1dfa
NBV
65 offre.nom = offre_dict['nom']
66 offre.resume = offre_dict['resume']
67 offre.description = offre_dict['description']
44accb6d 68 offre.poste_nom = offre_dict['poste_nom']
448f1dfa
NBV
69 offre.date_limite = offre_dict['date_limite']
70 offre.region = ref.Region.objects.get(id=offre_dict['region'])
71 offre.bureau = ref.Bureau.objects.get(id=offre_dict['bureau'])
72 offre.duree_affectation = offre_dict['duree_affectation']
73 offre.renumeration = offre_dict['renumeration']
74 offre.debut_affectation = offre_dict['debut_affectation']
75 offre.lieu_affectation = ref.Implantation.objects.get(id=offre_dict['lieu_affectation'])
76 obj_offres_emploi.append(offre)
77 return obj_offres_emploi
78
79 def candidat_add(self, offre_id):
36b8eabb
NBV
80 url = "http://127.0.0.1:8000/api/candidat_add/%s" % offre_id
81 r = Resource(url)
3d78c976
NBV
82 import pdb;pdb.set_trace()
83 mp = MultipartParam.from_file('cv', self.request.POST.get('candidat_piece-0-nom'))
84 import pdb;pdb.set_trace()
85 datagen, headers = multipart_encode([image_param])
86 import pdb;pdb.set_trace()
87 resp = r.post(payload=self.request.FILES, params_dict=self.request.POST)
88 if resp.status == STATUS_OK:
89 candidat_id = simplejson.loads(resp.body_string())['candidat_id']
90 return candidat_id
91 return None
448f1dfa 92