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

Alter taxref route and deprecated warning #554

Merged
merged 5 commits into from
Oct 4, 2024
Merged
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
12 changes: 8 additions & 4 deletions apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from flask import url_for
from sqlalchemy import ForeignKey, select, func, event

from sqlalchemy.ext.hybrid import hybrid_property
Expand Down Expand Up @@ -172,14 +173,17 @@
url = db.Column(db.Unicode)

status = db.relationship("VBdcStatus", order_by="VBdcStatus.lb_type_statut")
rang = db.relationship("BibTaxrefRangs", uselist=False)
synonymes = db.relationship(
"Taxref", foreign_keys=[cd_ref], primaryjoin="Taxref.cd_ref == Taxref.cd_ref", uselist=True
)
attributs = db.relationship("CorTaxonAttribut", back_populates="taxon")
listes = db.relationship("BibListes", secondary=cor_nom_liste, back_populates="noms")
medias = db.relationship("apptax.taxonomie.models.TMedias", back_populates="taxon")

rang = db.relationship("BibTaxrefRangs", uselist=False)
habitat = db.relationship("BibTaxrefHabitats", uselist=False)
statut_presence = db.relationship("BibTaxrefStatus", uselist=False)

@hybrid_property
def nom_vern_or_lb_nom(self):
return self.nom_vern if self.nom_vern else self.lb_nom
Expand Down Expand Up @@ -313,7 +317,7 @@
if self.url:
return self.url
else:
return url_for("media", filename=self.chemin, _external=True)
return url_for("media_taxhub", filename=self.chemin, _external=True)

Check warning on line 320 in apptax/taxonomie/models.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/models.py#L320

Added line #L320 was not covered by tests

def __repr__(self):
return self.titre
Expand Down Expand Up @@ -343,7 +347,7 @@
class BibTaxrefHabitats(db.Model):
__tablename__ = "bib_taxref_habitats"
__table_args__ = {"schema": "taxonomie"}
id_habitat = db.Column(db.Integer, primary_key=True)
id_habitat = db.Column(db.Integer, ForeignKey("taxonomie.taxref.id_habitat"), primary_key=True)
nom_habitat = db.Column(db.Unicode)
desc_habitat = db.Column(db.Text)

Expand All @@ -367,7 +371,7 @@
class BibTaxrefStatus(db.Model):
__tablename__ = "bib_taxref_statuts"
__table_args__ = {"schema": "taxonomie"}
id_statut = db.Column(db.Integer, primary_key=True)
id_statut = db.Column(db.Integer, ForeignKey("taxonomie.taxref.id_statut"), primary_key=True)
nom_statut = db.Column(db.Unicode)

def __repr__(self):
Expand Down
91 changes: 43 additions & 48 deletions apptax/taxonomie/repositories.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import logging
import os.path
from typing import List

from flask import current_app
from sqlalchemy.exc import IntegrityError
from sqlalchemy import select
from sqlalchemy.orm import joinedload

from . import db
from ..utils.utilssqlalchemy import dict_merge
from .models import (
TaxrefBdcStatutCorTextValues,
TaxrefBdcStatutTaxon,
TaxrefBdcStatutText,
TaxrefBdcStatutType,
TaxrefBdcStatutValues,
VBdcStatus,
TMedias,
)
from .models import TaxrefBdcStatutCorTextValues, TaxrefBdcStatutTaxon, TaxrefBdcStatutText
from ref_geo.models import LAreas


logger = logging.getLogger()


class BdcStatusRepository:

@staticmethod
def get_status(
self,
cd_ref: int,
type_statut: str,
areas: List[int] = None,
Expand All @@ -34,40 +25,44 @@
format=False,
):
"""
Retourne la liste des statuts associés à un taxon
sous forme hiérarchique

Args:
cd_ref (int): cd_ref
type_statut (str): code du type de statut
areas (List[int], optional): limite les statuts renvoyés
aux identifiants de zones géographiques fournies.
areas_code (List[str], optional): limite les statuts renvoyés
aux codes de zones géographiques fournies.
enable (bool, optional): ne retourner que les statuts actifs Defaults to True.
format (bool, optional): retourne les données formatées. Defaults to False.

Returns:
listes des statuts du taxon
Retourne la liste des statuts associés à un taxon sous forme hiérarchique.

Parameters
----------
cd_ref : int
cd_ref
type_statut : str
code du type de statut
areas : List[int], optional
Limite les statuts renvoyés aux identifiants de zones géographiques fournies.
areas_code : List[str], optional
Limite les statuts renvoyés aux codes de zones géographiques fournies.
enable : bool, optional
Ne retourner que les statuts actifs (default is True)
format : bool, optional
Retourne les données formatées (default is False)

Returns
-------
listes des statuts du taxon
"""
q = (
db.session.query(TaxrefBdcStatutTaxon)
query = (
select(TaxrefBdcStatutTaxon)
.join(TaxrefBdcStatutCorTextValues)
.join(TaxrefBdcStatutText)
.filter(TaxrefBdcStatutTaxon.cd_ref == cd_ref)
.filter(TaxrefBdcStatutText.enable == enable)
.where(TaxrefBdcStatutTaxon.cd_ref == cd_ref, TaxrefBdcStatutText.enable == enable)
)

if type_statut:
q = q.filter(TaxrefBdcStatutText.cd_type_statut == type_statut)
query = query.where(TaxrefBdcStatutText.cd_type_statut == type_statut)

Check warning on line 57 in apptax/taxonomie/repositories.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/repositories.py#L57

Added line #L57 was not covered by tests

if areas:
q = q.filter(TaxrefBdcStatutText.areas.any(LAreas.id_area.in_(areas)))
query = query.where(TaxrefBdcStatutText.areas.any(LAreas.id_area.in_(areas)))

if areas_code:
q = q.filter(TaxrefBdcStatutText.areas.any(LAreas.area_code.in_(areas_code)))
query = query.where(TaxrefBdcStatutText.areas.any(LAreas.area_code.in_(areas_code)))

q = q.options(
query = query.options(
joinedload(TaxrefBdcStatutTaxon.value_text).joinedload(
TaxrefBdcStatutCorTextValues.value
)
Expand All @@ -76,27 +71,27 @@
.joinedload(TaxrefBdcStatutCorTextValues.text)
.joinedload(TaxrefBdcStatutText.type_statut)
)
data = q.all()
data = db.session.scalars(query).all()

# Retour des données sous forme formatées ou pas
if format:
return self.format_hierarchy_status(data)
return BdcStatusRepository.format_hierarchy_status(data)
else:
return data

def format_hierarchy_status(self, data):
@staticmethod
def format_hierarchy_status(data):
"""
Formatage des données sous la forme d'un dictionnaire
type de statut : {
[text : {
[valeurs]
}]
}
Formatage des données sous la forme d'un dictionnaire

Args:
data ([resultProxy]): Données à formater
Parameters
----------
data : resultProxy
Données à formater

Returns:
Returns
-------
dict
[type]: [description]
"""
results = {}
Expand Down
2 changes: 1 addition & 1 deletion apptax/taxonomie/routesbdcstatuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
# get parameters type:
type_statut = request.args.get("type_statut")

results = BdcStatusRepository().get_status(
results = BdcStatusRepository.get_status(

Check warning on line 48 in apptax/taxonomie/routesbdcstatuts.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/routesbdcstatuts.py#L48

Added line #L48 was not covered by tests
cd_ref=cd_ref, type_statut=type_statut, enable=True, format=True
)

Expand Down
10 changes: 7 additions & 3 deletions apptax/taxonomie/routesbibnoms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# coding: utf8
from warnings import warn
import logging

from flask import json, Blueprint, request, current_app
from sqlalchemy import func
from flask import Blueprint, request
from werkzeug.exceptions import NotFound


Expand All @@ -16,7 +16,6 @@
)
from .models import Taxref, CorTaxonAttribut, BibThemes, BibAttributs, TMedias
from .schemas import TMediasSchema
from pypnusershub import routes as fnauth
from . import db

adresses = Blueprint("bib_noms", __name__)
Expand All @@ -37,6 +36,11 @@ def getOne_bibtaxonsInfo(cd_nom):
- id_attribut(integer): id_attribut
(Possibilité de passer plusiers id_attribut)
"""
warn(
"Route taxoninfo is deprecated, please use taxref detail route instead)",
DeprecationWarning,
)

# Récupération du cd_ref à partir du cd_nom
taxon = Taxref.query.get(cd_nom)
if not taxon:
Expand Down
Loading
Loading