Pièces jointes et 1852:Changer les adresse hardcodé dans l'API
[auf_django_emploi.git] / auf / django / emploi / api.py
CommitLineData
d691a5d3
NBV
1# -*- encoding: utf-8 -*
2from django.shortcuts import render_to_response, redirect, get_object_or_404
448f1dfa 3from django.template import Context, RequestContext
d691a5d3
NBV
4
5from django.utils import simplejson
6from auf.django.emploi import models as emploi
449e9e93 7from restkit import request as req
36b8eabb
NBV
8from restkit import Resource
9from httplib2 import Http
10from urllib import urlencode
448f1dfa 11import datamaster_modeles.models as ref
3d78c976
NBV
12from poster.encode import MultipartParam
13from poster.encode import multipart_encode
14import urllib2
7f2e1e11 15import settings
3d78c976
NBV
16
17
18STATUS_OK = '200 OK'
d691a5d3
NBV
19
20class API:
21 def __init__(self, request):
22 self.request = request
23
7f2e1e11
NBV
24 def offre_emploi_liste(self, env):
25
26 if hasattr(settings, "RECRUTEMENT_URL"):
27 url = getattr(settings, "RECRUTEMENT_URL")[env] + "offre_emploi_liste/"
28 else:
29 raise ImportError, "Could not import settings RECRUTEMENT_PATH"
449e9e93 30 r = req(url)
d691a5d3 31 liste_json = r.body_string()
1ff71303
NBV
32 try:
33 liste_offres = simplejson.loads(liste_json)
34 except:
35 return []
d691a5d3
NBV
36 obj_offres_emploi = []
37
38 for offre_dict in liste_offres:
39 offre = emploi.OffreEmploi()
8acb3cd3
NBV
40 offre.est_affiche = offre_dict['est_affiche']
41 offre.statut = offre_dict['statut']
d691a5d3
NBV
42 offre.nom = offre_dict['nom']
43 offre.resume = offre_dict['resume']
44 offre.description = offre_dict['description']
44accb6d 45 offre.poste_nom = offre_dict['poste_nom']
448f1dfa
NBV
46 offre.date_limite = offre_dict['date_limite']
47 offre.region = ref.Region.objects.get(id=offre_dict['region'])
48 offre.bureau = ref.Bureau.objects.get(id=offre_dict['bureau'])
d691a5d3
NBV
49 offre.duree_affectation = offre_dict['duree_affectation']
50 offre.renumeration = offre_dict['renumeration']
51 offre.debut_affectation = offre_dict['debut_affectation']
448f1dfa 52 offre.lieu_affectation = ref.Implantation.objects.get(id=offre_dict['lieu_affectation'])
d691a5d3
NBV
53 obj_offres_emploi.append(offre)
54 return obj_offres_emploi
55
7f2e1e11
NBV
56 def offre_emploi(self, offre_id, env):
57 if hasattr(settings, "RECRUTEMENT_URL"):
58 url = getattr(settings, "RECRUTEMENT_URL")[env] + "offre_emploi/?id=%s"
59 else:
60 raise ImportError, "Could not import settings RECRUTEMENT_PATH"
61 r = req(url % offre_id)
d691a5d3 62 offre_json = r.body_string()
1ff71303
NBV
63
64 try:
65 offre_dict = simplejson.loads(offre_json)
66 except:
67 return []
448f1dfa 68 obj_offres_emploi = []
d691a5d3 69
448f1dfa 70 offre = emploi.OffreEmploi()
8acb3cd3
NBV
71 offre.est_affiche = offre_dict['est_affiche']
72 offre.statut = offre_dict['statut']
448f1dfa
NBV
73 offre.nom = offre_dict['nom']
74 offre.resume = offre_dict['resume']
75 offre.description = offre_dict['description']
44accb6d 76 offre.poste_nom = offre_dict['poste_nom']
448f1dfa
NBV
77 offre.date_limite = offre_dict['date_limite']
78 offre.region = ref.Region.objects.get(id=offre_dict['region'])
79 offre.bureau = ref.Bureau.objects.get(id=offre_dict['bureau'])
80 offre.duree_affectation = offre_dict['duree_affectation']
81 offre.renumeration = offre_dict['renumeration']
82 offre.debut_affectation = offre_dict['debut_affectation']
83 offre.lieu_affectation = ref.Implantation.objects.get(id=offre_dict['lieu_affectation'])
84 obj_offres_emploi.append(offre)
85 return obj_offres_emploi
86
7f2e1e11
NBV
87 def candidat_add(self, offre_id, env):
88 if hasattr(settings, "RECRUTEMENT_URL"):
89 url = getattr(settings, "RECRUTEMENT_URL")[env] + "candidat_add/%s"
90 else:
91 raise ImportError, "Could not import settings RECRUTEMENT_PATH"
92 r = Resource(url % offre_id)
3d78c976 93 mp = MultipartParam.from_file('cv', self.request.POST.get('candidat_piece-0-nom'))
3d78c976 94 datagen, headers = multipart_encode([image_param])
3d78c976
NBV
95 resp = r.post(payload=self.request.FILES, params_dict=self.request.POST)
96 if resp.status == STATUS_OK:
97 candidat_id = simplejson.loads(resp.body_string())['candidat_id']
98 return candidat_id
99 return None
448f1dfa 100