Skip to content

Commit

Permalink
Ajout de la fonctionnalité import média inpn sous la forme d'une comm…
Browse files Browse the repository at this point in the history
…ande
  • Loading branch information
amandine-sahl committed Sep 8, 2023
1 parent 6c1def3 commit 666f02c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 262 deletions.
39 changes: 39 additions & 0 deletions apptax/taxonomie/commands/taxref.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import click
import csv

from flask.cli import with_appcontext
from sqlalchemy.orm.exc import NoResultFound

from apptax.database import db
from apptax.taxonomie.models import Taxref, TaxrefBdcStatutText, TMetaTaxref
Expand All @@ -17,6 +20,12 @@
from .migrate_taxref.commands_v15 import migrate_to_v15
from .migrate_taxref.commands_v16 import migrate_to_v16

from apptax.taxonomie.models import Taxref

import logging

logger = logging.getLogger("taxref_commands")


@click.group(help="Manager TaxRef referentials.")
def taxref():
Expand Down Expand Up @@ -82,6 +91,35 @@ def delete_bdc():
db.session.commit()


@taxref.command(
help="Importer des médias de l'INPN TaxRef à partir d'une liste de cd_ref de référence."
)
@click.option("--file", type=click.Path(exists=True))
@with_appcontext
def get_inpn_media(file):
"""
Importer des médias de l'INPN TaxRef à partir d'une liste de cd_ref de référence
Le fichier doit contenir une colonne avec la liste des cd_ref à traiter
"""
from apptax.utils.taxref_api import get_inpn_media

with open(file, "r") as file:
csvreader = csv.reader(file)
for row in csvreader:
value = row[0]
if value in ("cd_nom", "cd_ref"):
continue

# Get Taxon
try:
taxon = Taxref.query.get(int(value))
except (NoResultFound, ValueError):
logger.error(f"{value} is not a valid cd_ref")
continue

get_inpn_media(taxon.cd_ref, taxon.cd_nom, logger)


taxref.add_command(import_v14)
taxref.add_command(import_bdc_v14)
taxref.add_command(import_v15)
Expand All @@ -92,3 +130,4 @@ def delete_bdc():
taxref.add_command(migrate_to_v16)
taxref.add_command(link_bdc_statut_to_areas)
taxref.add_command(enable_bdc_statut_text)
taxref.add_command(get_inpn_media)
71 changes: 71 additions & 0 deletions apptax/utils/taxref_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import requests

from sqlalchemy.orm.exc import NoResultFound

from apptax.database import db

from apptax.taxonomie.models import TMedias, BibTypesMedia, BibNoms


API_URL = "https://taxref.mnhn.fr/api"


def get_inpn_media(cd_ref, cd_nom, logger=None):
"""
Fonction qui interroge l'api média de taxref
et qui peuple la table média
"""
url = f"{API_URL}/taxa/{cd_ref}/media"
inpn_response = requests.get(url)

if inpn_response.status_code != 200:
if logger:
logger.warning(
f"{cd_ref} : l'URL {url} retourne le code HTTP {inpn_response.status_code} !"
)
return

data = inpn_response.json()
if "_embedded" not in data:
if logger:
logger.warning(f"{cd_ref} : l'URL {url} ne retourne aucune réponse!")
return

if not data["_embedded"]["media"]:
if logger:
logger.warning(f"{cd_ref} : l'URL {url} ne retourne aucun media !")
return

medias = data["_embedded"]["media"]

# Get media type
type = BibTypesMedia.query.get(1)
for m_inpn in medias:
# Check is in bib_noms else create bib_noms
try:
bib_noms = BibNoms.query.filter_by(cd_nom=cd_ref).one()
except NoResultFound:
bib_noms = BibNoms(cd_ref=cd_ref, cd_nom=cd_nom)
db.session.add(bib_noms)

# Check if media exists
try:
m_obj = TMedias.query.filter_by(url=m_inpn["_links"]["file"]["href"]).one()
except NoResultFound:
m_obj = TMedias(
url=m_inpn["_links"]["file"]["href"],
)
m_obj.cd_ref = cd_nom
m_obj.titre = m_inpn["taxon"]["referenceNameHtml"]
m_obj.nom = m_inpn["taxon"]["scientificName"]
m_obj.auteur = m_inpn["copyright"]
m_obj.desc_media = m_inpn["title"]
m_obj.licence = m_inpn["licence"]
m_obj.is_public = True
m_obj.supprime = False
m_obj.source = "INPN"
m_obj.id_type = type.id_type

db.session.add(m_obj)
db.session.commit()
82 changes: 0 additions & 82 deletions data/scripts/import_inpn_media/README.rst

This file was deleted.

15 changes: 0 additions & 15 deletions data/scripts/import_inpn_media/config.py.sample

This file was deleted.

165 changes: 0 additions & 165 deletions data/scripts/import_inpn_media/import_inpn_media.py

This file was deleted.

Loading

0 comments on commit 666f02c

Please sign in to comment.