release 0.5 emploi
[auf_django_emploi.git] / auf / django / emploi / api.py
... / ...
CommitLineData
1# -*- encoding: utf-8 -*
2from django.core import serializers
3from django.shortcuts import render_to_response, redirect, get_object_or_404
4from django.template import Context, RequestContext
5from django.utils import simplejson
6from auf.django.emploi import models as emploi
7from restkit import request, Resource
8import datamaster_modeles.models as ref
9import poster
10import urllib2
11from auf.django.emploi import settings
12
13
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)
21
22class API:
23 def __init__(self, request, env):
24 self.request = request
25 self.env = env
26
27 def offre_emploi_liste(self,):
28 url = settings.RECRUTEMENT_URLS[self.env] + "offre_emploi_liste/"
29 r = req(url)
30 data = r.body_string()
31 offres = [offre.object for offre in serializers.deserialize("json", data)]
32 return offres
33
34 def offre_emploi(self, offre_id):
35 url = settings.RECRUTEMENT_URLS[self.env] + "offre_emploi/?id=%s"
36 r = req(url % offre_id)
37 data = r.body_string()
38 offres = [o.object for o in serializers.deserialize("json", data)]
39 if len(offres) == 0:
40 return offres[0]
41 else:
42 return offres
43
44 def candidat_add(self, offre_id,):
45 url = settings.RECRUTEMENT_URLS[self.env] + "candidat_add/%s/" % offre_id
46 r = Resource(url)
47 params = self.request.POST
48 for k, up_file in self.request.FILES.items():
49 params[k] = up_file
50 opener = poster.streaminghttp.register_openers()
51 datagen, headers = poster.encode.multipart_encode(params)
52 request = urllib2.Request(url, datagen, headers)
53 resp = urllib2.urlopen(request)
54 if resp.code == STATUS_OK:
55 candidats = [c.object for c in serializers.deserialize("json", resp.read())]
56 if len(candidats) == 1:
57 return candidats[0]
58 return None