release emploi
[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
b11f2fe5 22
d5fe14d6 23def read_response(request):
b11f2fe5
OL
24 """
25 La prod en Python2.5 avec restkit 3.3.2 en mode SSL
26 semble capricieuse.
27 """
28 data = request._body.raw.readall()
29 if data == '':
30 data = request._body.raw.read()
31 return data
32
d5fe14d6 33
d691a5d3 34class API:
77dc3c2f 35 def __init__(self, request, env):
d691a5d3 36 self.request = request
77dc3c2f 37 self.env = env
d691a5d3 38
77dc3c2f
OL
39 def offre_emploi_liste(self,):
40 url = settings.RECRUTEMENT_URLS[self.env] + "offre_emploi_liste/"
449e9e93 41 r = req(url)
d5fe14d6 42 data = read_response(r)
77dc3c2f
OL
43 offres = [offre.object for offre in serializers.deserialize("json", data)]
44 return offres
d691a5d3 45
44a3d052 46 def offre_emploi(self, offre_id):
77dc3c2f 47 url = settings.RECRUTEMENT_URLS[self.env] + "offre_emploi/?id=%s"
7f2e1e11 48 r = req(url % offre_id)
d5fe14d6 49 data = read_response(r)
77dc3c2f
OL
50 offres = [o.object for o in serializers.deserialize("json", data)]
51 if len(offres) == 0:
52 return offres[0]
7f2e1e11 53 else:
77dc3c2f
OL
54 return offres
55
56 def candidat_add(self, offre_id,):
57 url = settings.RECRUTEMENT_URLS[self.env] + "candidat_add/%s/" % offre_id
58 r = Resource(url)
59 params = self.request.POST
c3add7d3
OL
60 for k, up_file in self.request.FILES.items():
61 params[k] = up_file
77dc3c2f
OL
62 opener = poster.streaminghttp.register_openers()
63 datagen, headers = poster.encode.multipart_encode(params)
64 request = urllib2.Request(url, datagen, headers)
65 resp = urllib2.urlopen(request)
66 if resp.code == STATUS_OK:
67 candidats = [c.object for c in serializers.deserialize("json", resp.read())]
68 if len(candidats) == 1:
69 return candidats[0]
3d78c976 70 return None