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