from django.core import serializers
from django.shortcuts import get_object_or_404
-from chercheurs.models import Chercheur
+from django.utils import simplejson
+
+from chercheurs.models import Chercheur, Personne
STATUS_OK = 200
STATUS_ERROR = 400
STATUS_ERROR_NOT_FOUND = 404
STATUS_ERROR_BADMETHOD = 405
-def api(request, pays=None, region=None, id=None):
+def api(request, pays=None, region=None, chercheur_id=None):
api = API(request)
- if id is not None:
- return api.api_chercheur(id)
+
+ if chercheur_id is not None:
+ return api.api_chercheur(chercheur_id)
else:
- return api.api_chercheurs_liste(pays=pays, region=region, id=id)
+ return api.api_chercheurs_liste(pays=pays, region=region)
def api_return(status, text='', json=False):
content_type = 'text/html'
def __init__(self, request):
self.request = request
- def api_chercheur(self, id):
- chercheur = get_object_or_404(Chercheur, id=id)
- data = serializers.serialize('json', [chercheur])
- return api_return(STATUS_OK, data, True)
+ def api_chercheur(self, chercheur_id):
+ chercheur = get_object_or_404(Chercheur, id=chercheur_id)
+ # Domaines de recherche du chercheur
+ data = serializers.serialize('json', [chercheur,])
+ return api_return(STATUS_OK, data, False)
+
+
+ domaines_recherche = []
+ for dr in chercheur.domaines_recherche:
+ domaines_recherche.append(dr.nom)
+
+ # Groupes chercheur
+ groupes_chercheur = []
+ for gc in chercheur.groupes_chercheur:
+ groupes_chercheur.append(gc.nom)
+
+ # Expertises
+ expertises = []
+ for exp in chercheur.expertises.all():
+ expertises.append(
+ {"nom": "%s" % exp.nom,
+ "date": "%s" % exp.date,
+ "organisme_demandeur": "%s" % exp.organisme_demandeur,
+ "organisme_demandeur_visible": "%s" % exp.organisme_demandeur_visible})
+
+ # Publications
+ publications = []
+ for pub in chercheur.publications.all():
+ publications.append(
+ {"auteurs": "%s" % pub.auteurs,
+ "titre": "%s" % pub.titre,
+ "revue": "%s" % pub.revue,
+ "annee": "%s" % pub.annee,
+ "editeur": "%s" % pub.editeur,
+ "lieu_edition": "%s" % pub.lieu_edition,
+ "nb_pages": "%s" % pub.nb_pages,
+ "url": "%s" % pub.url,
+ "publication_affichage": "%s" % pub.publication_affichage})
+
+ chercheur_details = [{"id": "%s" % chercheur.id,
+ "civilite": "%s" % chercheur.civilite,
+ "prenom": "%s" % chercheur.prenom,
+ "nom": "%s" % chercheur.nom,
+ "etablissement_display": "%s" % chercheur.etablissement_display,
+ "afficher_courriel": "%s" % chercheur.afficher_courriel,
+ "courriel_display": "%s" % chercheur.courriel_display,
+ "region": "%s" % chercheur.region.nom,
+ "statut_display": "%s" % chercheur.statut_display,
+ "diplome": "%s" % chercheur.diplome,
+ "domaines_recherche": "%s" % domaines_recherche,
+ "discipline": "%s" % chercheur.discipline,
+ "theme_recherche": "%s" % chercheur.theme_recherche,
+ "equipe_recherche": "%s" % chercheur.equipe_recherche,
+ "mots_cles": "%s" % chercheur.mots_cles,
+ "url_site_web": "%s" % chercheur.url_site_web,
+ "url_blog": "%s" % chercheur.url_blog,
+ "url_reseau_social": "%s" % chercheur.url_reseau_social,
+ "membre_instance_auf": "%s" % chercheur.membre_instance_auf,
+ "expert_oif": "%s" % chercheur.expert_oif,
+ "membre_association_francophone": "%s" % chercheur.membre_association_francophone,
+ "membre_reseau_institutionnel": "%s" % chercheur.membre_reseau_institutionnel,
+ "get_membre_instance_auf_nom_display": "%s" % chercheur.get_membre_instance_auf_nom_display,
+ "membre_instance_auf_fonction": "%s" % chercheur.membre_instance_auf_fonction,
+ "membre_instance_auf_dates": "%s" % chercheur.membre_instance_auf_dates,
+ "expert_oif_details": "%s" % chercheur.expert_oif_details,
+ "expert_oif_dates": "%s" % chercheur.expert_oif_dates,
+ "membre_association_francophone_details": "%s" % chercheur.membre_association_francophone_details,
+ "get_membre_reseau_institutionnel_nom_display": "%s" %
+ chercheur.get_membre_reseau_institutionnel_nom_display,
+ "membre_reseau_institutionnel_fonction": "%s" % chercheur.membre_reseau_institutionnel_fonction,
+ "membre_reseau_institionnel_dates": "%s" % chercheur.membre_reseau_institutionnel_dates,
+ "expertises": expertises,
+ "expertises_auf": "%s" % chercheur.expertises_auf,
+ "publications": publications}]
+
+ if chercheur.these:
+ details_pop = chercheur_details.pop(0)
+ details_pop.update(
+ {"these_url": "%s" % chercheur.these.url,
+ "these_titre": "%s" % chercheur.these.titre,
+ "these_etablissement": "%s" % chercheur.these.etablissement,
+ "these_annee": "%s" % chercheur.these.annee,
+ "these_nb_pages": "%s" % chercheur.these.nb_pages,
+ "these_directeur": "%s" % chercheur.these.directeur,
+ })
+ chercheur_details.append(details_pop)
+ #"publications": chercheur.publications,
+
+
+ return api_return(STATUS_OK, simplejson.dumps(chercheur_details), False)
- def api_chercheurs_liste(self, pays=None, region=None, id=None):
+ def api_chercheurs_liste(self, pays=None, region=None):
if pays is not None:
chercheurs = Chercheur.objects.filter_pays(pays)
elif region is not None:
else:
return api_return(STATUS_ERROR, "Erreur dans la requete de recherche de chercheurs")
- data = serializers.serialize('json', chercheurs)
- return api_return(STATUS_OK, data, True)
+ return api_return(STATUS_OK, simplejson.dumps(
+ [{"id": "%s" % c.id,
+ "nom": "%s" % c.nom,
+ "prenom": "%s" % c.prenom,
+ "etablissement": "%s" % c.etablissement_display,
+ "pays": "%s" % c.pays}
+ for c in chercheurs]), json=True)