fix for mailing
[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
30945e50 11import 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
NBV
21
22class API:
c2141437 23 def __init__(self, request, env):
f77f4b9b 24 self.request = request
c2141437 25 self.env = env
f77f4b9b 26
c2141437
OL
27 def offre_emploi_liste(self,):
28 url = settings.RECRUTEMENT_URLS[self.env] + "offre_emploi_liste/"
14e06ff6 29 r = req(url)
c2141437
OL
30 data = r.body_string()
31 offres = [offre.object for offre in serializers.deserialize("json", data)]
32 return offres
f77f4b9b 33
30945e50 34 def offre_emploi(self, offre_id, env):
c2141437 35 url = settings.RECRUTEMENT_URLS[self.env] + "offre_emploi/?id=%s"
30945e50 36 r = req(url % offre_id)
f77f4b9b 37 offre_json = r.body_string()
c2141437
OL
38 data = r.body_string()
39 offres = [o.object for o in serializers.deserialize("json", data)]
40 if len(offres) == 0:
41 return offres[0]
30945e50 42 else:
c2141437
OL
43 return offres
44
45 def candidat_add(self, offre_id,):
46 url = settings.RECRUTEMENT_URLS[self.env] + "candidat_add/%s/" % offre_id
47 r = Resource(url)
48 params = self.request.POST
49 opener = poster.streaminghttp.register_openers()
50 datagen, headers = poster.encode.multipart_encode(params)
51 request = urllib2.Request(url, datagen, headers)
52 resp = urllib2.urlopen(request)
53 if resp.code == STATUS_OK:
54 candidats = [c.object for c in serializers.deserialize("json", resp.read())]
55 if len(candidats) == 1:
56 return candidats[0]
3a542b85 57 return None
d46075cb 58