9e9d815c91c28f4f267f4bda33a8f71ce83082e3
[auf_django_emploi.git] / auf / django / emploi / api.py
1 # -*- encoding: utf-8 -*
2 from django.shortcuts import render_to_response, redirect, get_object_or_404
3 from django.template import Context, RequestContext
4
5 from django.utils import simplejson
6 from auf.django.emploi import models as emploi
7 from restkit import request as req
8 from restkit import Resource
9 from httplib2 import Http
10 from urllib import urlencode
11 import datamaster_modeles.models as ref
12 from poster.encode import MultipartParam
13 from poster.encode import multipart_encode
14 import urllib2
15 import settings
16
17
18 STATUS_OK = '200 OK'
19
20 class API:
21 def __init__(self, request):
22 self.request = request
23
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"
30 r = req(url)
31 liste_json = r.body_string()
32 try:
33 liste_offres = simplejson.loads(liste_json)
34 except:
35 return []
36 obj_offres_emploi = []
37
38 for offre_dict in liste_offres:
39 offre = emploi.OffreEmploi()
40 offre.est_affiche = offre_dict['est_affiche']
41 offre.statut = offre_dict['statut']
42 offre.nom = offre_dict['nom']
43 offre.resume = offre_dict['resume']
44 offre.description = offre_dict['description']
45 offre.poste_nom = offre_dict['poste_nom']
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'])
49 offre.duree_affectation = offre_dict['duree_affectation']
50 offre.renumeration = offre_dict['renumeration']
51 offre.debut_affectation = offre_dict['debut_affectation']
52 offre.lieu_affectation = ref.Implantation.objects.get(id=offre_dict['lieu_affectation'])
53 obj_offres_emploi.append(offre)
54 return obj_offres_emploi
55
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)
62 offre_json = r.body_string()
63
64 try:
65 offre_dict = simplejson.loads(offre_json)
66 except:
67 return []
68 obj_offres_emploi = []
69
70 offre = emploi.OffreEmploi()
71 offre.est_affiche = offre_dict['est_affiche']
72 offre.statut = offre_dict['statut']
73 offre.nom = offre_dict['nom']
74 offre.resume = offre_dict['resume']
75 offre.description = offre_dict['description']
76 offre.poste_nom = offre_dict['poste_nom']
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
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)
93 mp = MultipartParam.from_file('cv', self.request.POST.get('candidat_piece-0-nom'))
94 datagen, headers = multipart_encode([image_param])
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
100