Commit | Line | Data |
---|---|---|
bf563475 NBV |
1 | # -*- encoding: utf-8 -* |
2 | from django.http import HttpResponse | |
3 | from django.core import serializers | |
5b3ac131 NBV |
4 | from django.shortcuts import get_object_or_404 |
5 | ||
776eadeb NBV |
6 | from django.utils import simplejson |
7 | ||
8 | from chercheurs.models import Chercheur, Personne | |
bf563475 NBV |
9 | |
10 | STATUS_OK = 200 | |
11 | STATUS_ERROR = 400 | |
12 | STATUS_ERROR_PERMISSIONS = 403 | |
13 | STATUS_ERROR_NOT_FOUND = 404 | |
14 | STATUS_ERROR_BADMETHOD = 405 | |
15 | ||
776eadeb | 16 | def api(request, pays=None, region=None, chercheur_id=None): |
bf563475 | 17 | api = API(request) |
776eadeb NBV |
18 | |
19 | if chercheur_id is not None: | |
20 | return api.api_chercheur(chercheur_id) | |
5b3ac131 | 21 | else: |
776eadeb | 22 | return api.api_chercheurs_liste(pays=pays, region=region) |
bf563475 NBV |
23 | |
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' | |
28 | if text is None: | |
29 | if status == STATUS_ERROR: | |
30 | text = '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: | |
38 | text = 'OK' | |
39 | ||
40 | r = HttpResponse(status=status, content=text, content_type=content_type) | |
41 | ||
42 | if status == STATUS_ERROR_BADMETHOD: | |
43 | r.Allow = 'POST' | |
44 | ||
45 | return r | |
46 | ||
47 | class API: | |
48 | def __init__(self, request): | |
49 | self.request = request | |
50 | ||
776eadeb NBV |
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, False) | |
56 | ||
57 | ||
58 | domaines_recherche = [] | |
59 | for dr in chercheur.domaines_recherche: | |
60 | domaines_recherche.append(dr.nom) | |
61 | ||
62 | # Groupes chercheur | |
63 | groupes_chercheur = [] | |
64 | for gc in chercheur.groupes_chercheur: | |
65 | groupes_chercheur.append(gc.nom) | |
66 | ||
67 | # Expertises | |
68 | expertises = [] | |
69 | for exp in chercheur.expertises.all(): | |
70 | expertises.append( | |
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}) | |
75 | ||
76 | # Publications | |
77 | publications = [] | |
78 | for pub in chercheur.publications.all(): | |
79 | publications.append( | |
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}) | |
89 | ||
90 | chercheur_details = [{"id": "%s" % chercheur.id, | |
91 | "civilite": "%s" % chercheur.civilite, | |
92 | "prenom": "%s" % chercheur.prenom, | |
93 | "nom": "%s" % chercheur.nom, | |
94 | "etablissement_display": "%s" % chercheur.etablissement_display, | |
95 | "afficher_courriel": "%s" % chercheur.afficher_courriel, | |
96 | "courriel_display": "%s" % chercheur.courriel_display, | |
97 | "region": "%s" % chercheur.region.nom, | |
98 | "statut_display": "%s" % chercheur.statut_display, | |
99 | "diplome": "%s" % chercheur.diplome, | |
100 | "domaines_recherche": "%s" % domaines_recherche, | |
101 | "discipline": "%s" % chercheur.discipline, | |
102 | "theme_recherche": "%s" % chercheur.theme_recherche, | |
103 | "equipe_recherche": "%s" % chercheur.equipe_recherche, | |
104 | "mots_cles": "%s" % chercheur.mots_cles, | |
105 | "url_site_web": "%s" % chercheur.url_site_web, | |
106 | "url_blog": "%s" % chercheur.url_blog, | |
107 | "url_reseau_social": "%s" % chercheur.url_reseau_social, | |
108 | "membre_instance_auf": "%s" % chercheur.membre_instance_auf, | |
109 | "expert_oif": "%s" % chercheur.expert_oif, | |
110 | "membre_association_francophone": "%s" % chercheur.membre_association_francophone, | |
111 | "membre_reseau_institutionnel": "%s" % chercheur.membre_reseau_institutionnel, | |
112 | "get_membre_instance_auf_nom_display": "%s" % chercheur.get_membre_instance_auf_nom_display, | |
113 | "membre_instance_auf_fonction": "%s" % chercheur.membre_instance_auf_fonction, | |
114 | "membre_instance_auf_dates": "%s" % chercheur.membre_instance_auf_dates, | |
115 | "expert_oif_details": "%s" % chercheur.expert_oif_details, | |
116 | "expert_oif_dates": "%s" % chercheur.expert_oif_dates, | |
117 | "membre_association_francophone_details": "%s" % chercheur.membre_association_francophone_details, | |
118 | "get_membre_reseau_institutionnel_nom_display": "%s" % | |
119 | chercheur.get_membre_reseau_institutionnel_nom_display, | |
120 | "membre_reseau_institutionnel_fonction": "%s" % chercheur.membre_reseau_institutionnel_fonction, | |
121 | "membre_reseau_institionnel_dates": "%s" % chercheur.membre_reseau_institutionnel_dates, | |
122 | "expertises": expertises, | |
123 | "expertises_auf": "%s" % chercheur.expertises_auf, | |
124 | "publications": publications}] | |
125 | ||
126 | if chercheur.these: | |
127 | details_pop = chercheur_details.pop(0) | |
128 | details_pop.update( | |
129 | {"these_url": "%s" % chercheur.these.url, | |
130 | "these_titre": "%s" % chercheur.these.titre, | |
131 | "these_etablissement": "%s" % chercheur.these.etablissement, | |
132 | "these_annee": "%s" % chercheur.these.annee, | |
133 | "these_nb_pages": "%s" % chercheur.these.nb_pages, | |
134 | "these_directeur": "%s" % chercheur.these.directeur, | |
135 | }) | |
136 | chercheur_details.append(details_pop) | |
137 | #"publications": chercheur.publications, | |
138 | ||
139 | ||
140 | return api_return(STATUS_OK, simplejson.dumps(chercheur_details), False) | |
5b3ac131 | 141 | |
776eadeb | 142 | def api_chercheurs_liste(self, pays=None, region=None): |
bf563475 NBV |
143 | if pays is not None: |
144 | chercheurs = Chercheur.objects.filter_pays(pays) | |
87567c72 | 145 | elif region is not None: |
ebde48e0 | 146 | chercheurs = Chercheur.objects.filter_region(region) |
bf563475 NBV |
147 | else: |
148 | return api_return(STATUS_ERROR, "Erreur dans la requete de recherche de chercheurs") | |
149 | ||
776eadeb NBV |
150 | return api_return(STATUS_OK, simplejson.dumps( |
151 | [{"id": "%s" % c.id, | |
152 | "nom": "%s" % c.nom, | |
153 | "prenom": "%s" % c.prenom, | |
154 | "etablissement": "%s" % c.etablissement_display, | |
155 | "pays": "%s" % c.pays} | |
156 | for c in chercheurs]), json=True) |