Routeur: Ajout temporaire des modules workflow et dae en écriture
[auf_rh_dae.git] / src / auf.django.emploi / auf / django / emploi / api.py
CommitLineData
f77f4b9b 1# -*- encoding: utf-8 -*
c2141437 2from django.core import serializers
f77f4b9b 3from django.shortcuts import render_to_response, redirect, get_object_or_404
d46075cb 4from django.template import Context, RequestContext
f77f4b9b
NBV
5from django.utils import simplejson
6from auf.django.emploi import models as emploi
c2141437 7from restkit import request, Resource
d46075cb 8import datamaster_modeles.models as ref
c2141437 9import poster
3a542b85 10import urllib2
7f25c8a6 11from auf.django.emploi import settings
3a542b85
NBV
12
13
c2141437
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)
f77f4b9b 21
48e23fba 22
26d2f6b0 23def read_response(request):
48e23fba
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
26d2f6b0 33
f77f4b9b 34class API:
c2141437 35 def __init__(self, request, env):
f77f4b9b 36 self.request = request
c2141437 37 self.env = env
f77f4b9b 38
c2141437
OL
39 def offre_emploi_liste(self,):
40 url = settings.RECRUTEMENT_URLS[self.env] + "offre_emploi_liste/"
14e06ff6 41 r = req(url)
26d2f6b0 42 data = read_response(r)
c2141437
OL
43 offres = [offre.object for offre in serializers.deserialize("json", data)]
44 return offres
f77f4b9b 45
1c1f99d0 46 def offre_emploi(self, offre_id):
c2141437 47 url = settings.RECRUTEMENT_URLS[self.env] + "offre_emploi/?id=%s"
30945e50 48 r = req(url % offre_id)
26d2f6b0 49 data = read_response(r)
c2141437
OL
50 offres = [o.object for o in serializers.deserialize("json", data)]
51 if len(offres) == 0:
52 return offres[0]
30945e50 53 else:
c2141437
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
7f25c8a6
OL
60 for k, up_file in self.request.FILES.items():
61 params[k] = up_file
c2141437
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]
3a542b85 70 return None