Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/load-info-media-inpn #325

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 159 additions & 7 deletions static/app/bib_nom/edit/bibNom-form-controler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ function($scope, $routeParams, $http, $uibModal, locationHistoryService, $locati
self.showMediaForm=false;
self.form = $scope.form;
self.userRightLevel = 0;
self.hideInpnButton = backendCfg.hideInpnButton;
self.inpnLoading = false;

//----------------------Gestion des droits---------------//
if (loginSrv.getCurrentUser()) {
Expand All @@ -27,9 +29,32 @@ function($scope, $routeParams, $http, $uibModal, locationHistoryService, $locati


var toasterMsg = {
'saveSuccess':{"title":"Taxon enregistré", "msg": "Le taxon a été enregistré avec succès"},
'saveError':{"title":"Erreur d'enregistrement"},
}
saveSuccess: {
title: "Taxon enregistré",
msg: "Le taxon a été enregistré avec succès",
},
saveError: { title: "Erreur d'enregistrement" },
mediaInserted: { title: "API INPN", msg: "Media inséré avec succès" },
infoInserted: {
title: "API INPN",
msg: "Information recupérée avec succès",
},
infoPresent: {
title: "API INPN",
msg: "Il y a déjà une description pour ce taxon, veuillez d'abord la supprimer",
},
inpnError: {
title: "API INPN",
msg: "Impossible d'accéder à l'API de l'INPN",
},
inpnNotFound: {
title: "API INPN",
msg: "Aucune description trouvée pour ce taxon sur l'API de l'INPN",
},
};

const inpnURL = "https://taxref.mnhn.fr/api/taxa/";

var getTaxonsInfo = function (cd_nom) {
$http.get(backendCfg.api_url + "bibnoms/taxoninfo/"+cd_nom+"?forcePath=True").then(function(response) {
if (response.data) {
Expand Down Expand Up @@ -114,11 +139,17 @@ function($scope, $routeParams, $http, $uibModal, locationHistoryService, $locati
}
});
//------------------------------ Sauvegarde du formulaire ----------------------------------/
self.submit = function() {
function postBibNom() {
var params = self.bibNom;
var url = backendCfg.api_url +"bibnoms/";
if(action == 'edit'){url=url+self.bibNom.id_nom;}
$http.post(url, params, { withCredentials: true })
var url = backendCfg.api_url + "bibnoms/";
if (action == "edit") {
url = url + self.bibNom.id_nom;
}
return $http.post(url, params, { withCredentials: true });
}

self.submit = function() {
postBibNom()
.then(function(response) {
var data = response.data
if (data.success == true) {
Expand Down Expand Up @@ -155,5 +186,126 @@ function($scope, $routeParams, $http, $uibModal, locationHistoryService, $locati
return response.data;
});
};
//------------------------------ Recupération des infos de l'inpn ----------------------------------/
function getAtlasDescription() {
return attribut = self.attributsDefList
?.map((item) => item.attributs)[0]
?.filter((item) => item.nom_attribut == "atlas_description")[0];
}

function setInfo(cd_nom) {
url = inpnURL + `${cd_nom}/factsheet`;
if (self.attributsDefList) {
const attribut = getAtlasDescription()
if (attribut !== undefined) {
$http
.get(url)
.then((response) => {
self.bibNom.attributs_values[attribut.id_attribut] =
response.data.text || "";
postBibNom()
.then(() =>
toaster.pop(
"success",
toasterMsg.saveSuccess.title,
toasterMsg.saveSuccess.msg,
5000,
"trustedHtml"
)
)
.catch(() =>
toaster.pop(
"error",
toasterMsg.saveError.title,
response.data.message,
5000,
"trustedHtml"
)
)
.finally(() => (self.inpnLoading = false));
})
.catch((error) => {
let title = toasterMsg.inpnError.title;
let msg = toasterMsg.inpnError.msg;
let toasterType = "error";

if (error.status == 404) {
title = toasterMsg.inpnNotFound.title;
msg = toasterMsg.inpnNotFound.msg;
toasterType = "warning";
}

toaster.pop(toasterType, title, msg, 5000, "trustedHtml");
})
.finally(() => (self.inpnLoading = false));
}
}
}

function setMedia(cd_nom) {
url = inpnURL + `${cd_nom}/media`;
has_error = false;
$http.get(url).then((response) => {
response.data?._embedded?.media.forEach((media) => {
const url = media?._links?.file?.href;
if (url && !self.bibNom.medias.map((item) => item.url).includes(url)) {
payload = {
cd_ref: cd_nom,
chemin: null,
id_type: 1,
is_public: true,
isFile: false,
titre: media?.title || "",
auteur: media?.copyright,
url: url,
};
$http
.post(backendCfg.api_url + "tmedias/", payload)
.then((response) => {
const newMedia = response.data?.media
if (newMedia !== undefined) {
self.bibNom.medias.push(newMedia)
}
toaster.pop(
"info",
toasterMsg.mediaInserted.title,
toasterMsg.mediaInserted.msg,
2000,
"trustedHtml"
);
})
.catch(() =>
toaster.pop(
"error",
toasterMsg.inpnError.title,
toasterMsg.inpnError.msg,
5000,
"trustedHtml"
)
)
.finally(() => (self.inpnLoading = false));
}
});
});
}

self.getINPNInfo = function () {
const cd_nom = self.bibNom.cd_nom;
self.inpnLoading = true;
const attribut = getAtlasDescription();
if (!self.bibNom.attributs_values[attribut.id_attribut]) {
setInfo(cd_nom);
} else {
self.inpnLoading = false;
toaster.pop(
"info",
toasterMsg.infoPresent.title,
toasterMsg.infoPresent.msg,
5000,
"trustedHtml"
);
}
setMedia(cd_nom);
};
}
]);
4 changes: 4 additions & 0 deletions static/app/bib_nom/edit/bibNom-form-tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<button ng-if="ctrl.bibNom.cd_nom" ng-click="ctrl.openTaxrefDetail(ctrl.bibNom.cd_ref)" tooltip="Voir toutes les informations disponibles concernant le taxon de référence de ce taxons" class="btn btn-success" >
<span class="glyphicon glyphicon-eye-open"></span>
</button>
<button ng-hide="ctrl.hideInpnButton" ng-disabled="ctrl.inpnLoading" ng-if="ctrl.bibNom.cd_nom" ng-click="ctrl.getINPNInfo()"
tooltip="Récupérer les informations et les médias depuis l'INPN" class="btn btn-success">
<span>Récupérer description et média INPN</span>
</button>
</div>
<div class="panel-body">
<form id="tx-form" class="form-horizontal" name="ctrl.form">
Expand Down
3 changes: 2 additions & 1 deletion static/app/constants.js.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ app.constant("backendCfg", {
"user_admin_privilege":6,
"user_high_privilege":4,
"user_medium_privilege":3,
"user_low_privilege":2
"user_low_privilege":2,
"hideInpnButton": false
})