Débug de pièces jointes
[auf_django_emploi.git] / auf / django / emploi / api.py
1 # -*- encoding: utf-8 -*
2 from django.shortcuts import render_to_response, redirect, get_object_or_404
3 from django.template import Context, RequestContext
4
5 from django.utils import simplejson
6 from auf.django.emploi import models as emploi
7 from restkit import request as req
8 from restkit import Resource
9 from httplib2 import Http
10 from urllib import urlencode
11 import datamaster_modeles.models as ref
12 from poster.encode import MultipartParam
13 from poster.encode import multipart_encode
14 import urllib2
15
16
17 STATUS_OK = '200 OK'
18
19 class API:
20 def __init__(self, request):
21 self.request = request
22
23 def offre_emploi_liste(self):
24 url = "http://127.0.0.1:8000/api/offre_emploi_liste/"
25 r = req(url)
26 liste_json = r.body_string()
27 try:
28 liste_offres = simplejson.loads(liste_json)
29 except:
30 return []
31 obj_offres_emploi = []
32
33 for offre_dict in liste_offres:
34 offre = emploi.OffreEmploi()
35 offre.est_affiche = offre_dict['est_affiche']
36 offre.statut = offre_dict['statut']
37 offre.nom = offre_dict['nom']
38 offre.resume = offre_dict['resume']
39 offre.description = offre_dict['description']
40 offre.poste_nom = offre_dict['poste_nom']
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'])
44 offre.duree_affectation = offre_dict['duree_affectation']
45 offre.renumeration = offre_dict['renumeration']
46 offre.debut_affectation = offre_dict['debut_affectation']
47 offre.lieu_affectation = ref.Implantation.objects.get(id=offre_dict['lieu_affectation'])
48 obj_offres_emploi.append(offre)
49 return obj_offres_emploi
50
51 def offre_emploi(self, offre_id):
52 url = "http://127.0.0.1:8000/api/offre_emploi/?id=%s"
53 r = req(url % (offre_id))
54 offre_json = r.body_string()
55
56 try:
57 offre_dict = simplejson.loads(offre_json)
58 except:
59 return []
60 obj_offres_emploi = []
61
62 offre = emploi.OffreEmploi()
63 offre.est_affiche = offre_dict['est_affiche']
64 offre.statut = offre_dict['statut']
65 offre.nom = offre_dict['nom']
66 offre.resume = offre_dict['resume']
67 offre.description = offre_dict['description']
68 offre.poste_nom = offre_dict['poste_nom']
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):
80 url = "http://127.0.0.1:8000/api/candidat_add/%s" % offre_id
81 r = Resource(url)
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
92