1 # -*- encoding: utf-8 -*
2 from django
.http
import HttpResponse
3 from django
.core
import serializers
4 from django
.shortcuts
import get_object_or_404
6 from django
.utils
import simplejson
8 from chercheurs
.models
import Chercheur
, Personne
12 STATUS_ERROR_PERMISSIONS
= 403
13 STATUS_ERROR_NOT_FOUND
= 404
14 STATUS_ERROR_BADMETHOD
= 405
16 def api(request
, pays
=None, region
=None, chercheur_id
=None):
18 if chercheur_id
is not None:
19 return api
.api_chercheur(chercheur_id
)
21 filter_pays
= pays
.split(',')
22 return api
.api_chercheurs_liste(pays
=filter_pays
, region
=region
)
24 def api_return(status
, text
='', json
=False):
25 content_type
= 'text/html'
26 if status
== STATUS_OK
and json
:
27 content_type
= 'text/json'
29 if status
== STATUS_ERROR
:
31 elif status
== STATUS_ERROR_NOT_FOUND
:
32 text
= 'Resource Not Found'
33 elif status
== STATUS_ERROR_PERMISSIONS
:
34 text
= 'Invalid username or password'
35 elif status
== STATUS_ERROR_BADMETHOD
:
36 text
= 'Invalid request method'
37 elif status
== STATUS_OK
:
40 r
= HttpResponse(status
=status
, content
=text
, content_type
=content_type
)
42 if status
== STATUS_ERROR_BADMETHOD
:
48 def __init__(self
, request
):
49 self
.request
= request
51 def api_chercheur(self
, chercheur_id
):
52 chercheur
= get_object_or_404(Chercheur
, id=chercheur_id
)
53 # Domaines de recherche du chercheur
54 data
= serializers
.serialize('json', [chercheur
,])
55 #return api_return(STATUS_OK, data, True)
58 domaines_recherche
= []
59 for dr
in chercheur
.domaines_recherche
:
60 domaines_recherche
.append(dr
.nom
)
63 groupes_chercheur
= []
64 for gc
in chercheur
.groupes_chercheur
:
65 groupes_chercheur
.append(gc
.nom
)
69 for exp
in chercheur
.expertises
.all():
71 {"nom": "%s" % exp
.nom
,
72 "date": "%s" % exp
.date
,
73 "organisme_demandeur": "%s" % exp
.organisme_demandeur
,
74 "organisme_demandeur_visible": "%s" % exp
.organisme_demandeur_visible
})
78 for pub
in chercheur
.publications
.all():
80 {"auteurs": "%s" % pub
.auteurs
,
81 "titre": "%s" % pub
.titre
,
82 "revue": "%s" % pub
.revue
,
83 "annee": "%s" % pub
.annee
,
84 "editeur": "%s" % pub
.editeur
,
85 "lieu_edition": "%s" % pub
.lieu_edition
,
86 "nb_pages": "%s" % pub
.nb_pages
,
87 "url": "%s" % pub
.url
,
88 "publication_affichage": "%s" % pub
.publication_affichage
})
90 chercheur_details
= [{"id": "%s" % chercheur
.id,
91 "civilite": "%s" % chercheur
.civilite
,
92 "prenom": "%s" % chercheur
.prenom
,
93 "nom": "%s" % chercheur
.nom
,
94 "pays": "%s" % chercheur
.pays
,
95 "etablissement_display": "%s" % chercheur
.etablissement_display
,
96 "afficher_courriel": "%s" % chercheur
.afficher_courriel
,
97 "courriel_display": "%s" % chercheur
.courriel_display
,
98 "region": "%s" % chercheur
.region
.nom
,
99 "statut_display": "%s" % chercheur
.statut_display
,
100 "diplome": "%s" % chercheur
.diplome
,
101 "domaines_recherche": "%s" % domaines_recherche
,
102 "discipline": "%s" % chercheur
.discipline
,
103 "theme_recherche": "%s" % chercheur
.theme_recherche
,
104 "equipe_recherche": "%s" % chercheur
.equipe_recherche
,
105 "mots_cles": "%s" % chercheur
.mots_cles
,
106 "url_site_web": "%s" % chercheur
.url_site_web
,
107 "url_blog": "%s" % chercheur
.url_blog
,
108 "url_reseau_social": "%s" % chercheur
.url_reseau_social
,
109 "membre_instance_auf": "%s" % chercheur
.membre_instance_auf
,
110 "expert_oif": "%s" % chercheur
.expert_oif
,
111 "membre_association_francophone": "%s" % chercheur
.membre_association_francophone
,
112 "membre_reseau_institutionnel": "%s" % chercheur
.membre_reseau_institutionnel
,
113 "get_membre_instance_auf_nom_display": "%s" % chercheur
.get_membre_instance_auf_nom_display
,
114 "membre_instance_auf_fonction": "%s" % chercheur
.membre_instance_auf_fonction
,
115 "membre_instance_auf_dates": "%s" % chercheur
.membre_instance_auf_dates
,
116 "expert_oif_details": "%s" % chercheur
.expert_oif_details
,
117 "expert_oif_dates": "%s" % chercheur
.expert_oif_dates
,
118 "membre_association_francophone_details": "%s" % chercheur
.membre_association_francophone_details
,
119 "get_membre_reseau_institutionnel_nom_display": "%s" %
120 chercheur
.get_membre_reseau_institutionnel_nom_display
,
121 "membre_reseau_institutionnel_fonction": "%s" % chercheur
.membre_reseau_institutionnel_fonction
,
122 "membre_reseau_institionnel_dates": "%s" % chercheur
.membre_reseau_institutionnel_dates
,
123 "expertises": expertises
,
124 "expertises_auf": "%s" % chercheur
.expertises_auf
,
125 "publications": publications
}]
128 details_pop
= chercheur_details
.pop(0)
130 {"these_url": "%s" % chercheur
.these
.url
,
131 "these_titre": "%s" % chercheur
.these
.titre
,
132 "these_etablissement": "%s" % chercheur
.these
.etablissement
,
133 "these_annee": "%s" % chercheur
.these
.annee
,
134 "these_nb_pages": "%s" % chercheur
.these
.nb_pages
,
135 "these_directeur": "%s" % chercheur
.these
.directeur
,
137 chercheur_details
.append(details_pop
)
138 return api_return(STATUS_OK
, simplejson
.dumps(chercheur_details
), True)
140 def api_chercheurs_liste(self
, pays
=None, region
=None):
142 #chercheurs = Chercheur.objects.filter_pays(pays)
143 chercheurs
= Chercheur
.objects
.filter(etablissement__pays__in
=[pays
])|Chercheur
.objects
.filter(etablissement_autre_pays__in
=pays
)
145 elif region
is not None:
146 chercheurs
= Chercheur
.objects
.filter_region(region
)
148 return api_return(STATUS_ERROR
, "Erreur dans la requete de recherche de chercheurs")
150 return api_return(STATUS_OK
, simplejson
.dumps(
153 "prenom": "%s" % c
.prenom
,
154 "etablissement": "%s" % c
.etablissement_display
,
155 "pays": "%s" % c
.pays
}
156 for c
in chercheurs
]), json
=True)