Version bump 1.2
[auf_django_emploi.git] / auf / django / emploi / api.py
CommitLineData
d691a5d3 1# -*- encoding: utf-8 -*
77dc3c2f 2from django.core import serializers
77dc3c2f 3from restkit import request, Resource
77dc3c2f 4import poster
3d78c976 5import urllib2
c3add7d3 6from auf.django.emploi import settings
3d78c976
NBV
7
8
77dc3c2f
OL
9STATUS_OK = 200
10
11def req(*args, **kwargs):
12 timeout = 10
13 if not kwargs.has_key('timeout'):
14 kwargs['timeout'] = timeout
15 return request(*args, **kwargs)
d691a5d3 16
b11f2fe5 17
d5fe14d6 18def read_response(request):
b11f2fe5
OL
19 """
20 La prod en Python2.5 avec restkit 3.3.2 en mode SSL
21 semble capricieuse.
22 """
23 data = request._body.raw.readall()
24 if data == '':
25 data = request._body.raw.read()
26 return data
27
d5fe14d6 28
d691a5d3 29class API:
77dc3c2f 30 def __init__(self, request, env):
d691a5d3 31 self.request = request
77dc3c2f 32 self.env = env
d691a5d3 33
77dc3c2f
OL
34 def offre_emploi_liste(self,):
35 url = settings.RECRUTEMENT_URLS[self.env] + "offre_emploi_liste/"
449e9e93 36 r = req(url)
d5fe14d6 37 data = read_response(r)
77dc3c2f
OL
38 offres = [offre.object for offre in serializers.deserialize("json", data)]
39 return offres
d691a5d3 40
44a3d052 41 def offre_emploi(self, offre_id):
77dc3c2f 42 url = settings.RECRUTEMENT_URLS[self.env] + "offre_emploi/?id=%s"
7f2e1e11 43 r = req(url % offre_id)
d5fe14d6 44 data = read_response(r)
77dc3c2f
OL
45 offres = [o.object for o in serializers.deserialize("json", data)]
46 if len(offres) == 0:
47 return offres[0]
7f2e1e11 48 else:
77dc3c2f
OL
49 return offres
50
51 def candidat_add(self, offre_id,):
52 url = settings.RECRUTEMENT_URLS[self.env] + "candidat_add/%s/" % offre_id
53 r = Resource(url)
54 params = self.request.POST
c3add7d3
OL
55 for k, up_file in self.request.FILES.items():
56 params[k] = up_file
77dc3c2f
OL
57 opener = poster.streaminghttp.register_openers()
58 datagen, headers = poster.encode.multipart_encode(params)
59 request = urllib2.Request(url, datagen, headers)
60 resp = urllib2.urlopen(request)
61 if resp.code == STATUS_OK:
62 candidats = [c.object for c in serializers.deserialize("json", resp.read())]
63 if len(candidats) == 1:
64 return candidats[0]
3d78c976 65 return None