X-Git-Url: http://git.auf.org/?p=auf_savoirs_en_partage_django.git;a=blobdiff_plain;f=auf_savoirs_en_partage%2Fchercheurs%2Fapi.py;h=4cf8d7f1e27c637965b520ec84c1f3178a5c754b;hp=a616aa5610ca26912eda220c5ab696f6885a7783;hb=776eadebd5d2be5972f4abb33202f59a64e2944a;hpb=5b3ac131f77900a2e86eb1dbfb449441c5ed1284 diff --git a/auf_savoirs_en_partage/chercheurs/api.py b/auf_savoirs_en_partage/chercheurs/api.py index a616aa5..4cf8d7f 100644 --- a/auf_savoirs_en_partage/chercheurs/api.py +++ b/auf_savoirs_en_partage/chercheurs/api.py @@ -3,7 +3,9 @@ from django.http import HttpResponse 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 @@ -11,12 +13,13 @@ STATUS_ERROR_PERMISSIONS = 403 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' @@ -45,12 +48,98 @@ class API: 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: @@ -58,5 +147,10 @@ class API: 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)