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

Perf/bib listes #589

Merged
merged 2 commits into from
Dec 6, 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
10 changes: 7 additions & 3 deletions apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,17 @@ class BibListes(db.Model):

@hybrid_property
def nb_taxons(self):
return len(self.noms)
return db.session.scalar(
select([db.func.count(cor_nom_liste.c.cd_nom)]).where(
cor_nom_liste.c.id_liste == self.id_liste
)
)

@nb_taxons.expression
def nb_taxons(cls):
return (
db.select([db.func.count(cor_nom_liste.id_liste)])
.where(BibListes.id_liste == cls.id_liste)
db.select([db.func.count(cor_nom_liste.c.cd_nom)])
.where(cor_nom_liste.c.id_liste == cls.id_liste)
.label("nb_taxons")
)

Expand Down
37 changes: 23 additions & 14 deletions apptax/taxonomie/routesbiblistes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import os
import logging

from flask import Blueprint, request, current_app
from sqlalchemy import func, or_
from sqlalchemy.orm import joinedload
from flask import Blueprint
from sqlalchemy import select

from pypnusershub import routes as fnauth
from utils_flask_sqla.response import json_resp

from . import filemanager
from . import db
from .models import BibListes, Taxref
from .models import BibListes
from apptax.taxonomie.schemas import BibListesSchema

adresses = Blueprint("bib_listes", __name__)
Expand All @@ -24,26 +21,38 @@

@adresses.route("/", methods=["GET"])
@json_resp
def get_biblistes(id=None):
def get_biblistes():
"""
retourne les contenu de bib_listes dans "data"
et le nombre d'enregistrements dans "count"
"""
data = db.session.query(BibListes).all()
biblistes_records = db.session.execute(

Check warning on line 29 in apptax/taxonomie/routesbiblistes.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/routesbiblistes.py#L29

Added line #L29 was not covered by tests
select(
BibListes.id_liste,
BibListes.code_liste,
BibListes.nom_liste,
BibListes.desc_liste,
BibListes.nb_taxons,
BibListes.regne,
BibListes.group2_inpn,
)
).all()
biblistes_schema = BibListesSchema()
maliste = {"data": [], "count": 0}
maliste["count"] = len(data)
maliste["data"] = biblistes_schema.dump(data, many=True)
return maliste
biblistes_infos = {

Check warning on line 41 in apptax/taxonomie/routesbiblistes.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/routesbiblistes.py#L41

Added line #L41 was not covered by tests
"data": biblistes_schema.dump(biblistes_records, many=True),
"count": len(biblistes_records),
}

return biblistes_infos

Check warning on line 46 in apptax/taxonomie/routesbiblistes.py

View check run for this annotation

Codecov / codecov/patch

apptax/taxonomie/routesbiblistes.py#L46

Added line #L46 was not covered by tests


@adresses.route("/<regne>", methods=["GET"], defaults={"group2_inpn": None})
@adresses.route("/<regne>/<group2_inpn>", methods=["GET"])
def get_biblistesbyTaxref(regne, group2_inpn):
q = db.session.query(BibListes)
q = select(BibListes)
if regne:
q = q.where(BibListes.regne == regne)
if group2_inpn:
q = q.where(BibListes.group2_inpn == group2_inpn)
results = q.all()
results = db.session.scalars(q).all()
return BibListesSchema().dump(results, many=True)
9 changes: 8 additions & 1 deletion apptax/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ def liste():
"code_liste": "TEST_LIST_Plantae",
"nom_liste": "Liste test Plantae",
"desc_liste": "Liste description",
"regne": "Plantea",
"regne": "Plantae",
},
{
"code_liste": "TEST_LIST_Mousses",
"nom_liste": "Liste test Mousses",
"desc_liste": "Liste description",
"regne": "Plantae",
"group2_inpn": "Mousses",
},
]

Expand Down
22 changes: 10 additions & 12 deletions apptax/tests/test_biblistes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,21 @@

@pytest.mark.usefixtures("client_class", "temporary_transaction")
class TestApiBibListe:
schema_cor_nom_liste = Schema(
{
"items": [{"cd_nom": int, "id_liste": int}],
"total": int,
"limit": int,
"page": int,
}
)

schema_allnamebyListe = Schema(
[
{
"id_liste": int,
"code_liste": str,
"nom_liste": str,
"desc_liste": str,
"desc_liste": Or(str, None),
"regne": Or(str, None),
"group2_inpn": Or(str, None),
"nb_taxons": int,
}
]
)

def test_get_biblistes(self):
def test_get_biblistes(self, listes):
query_string = {"limit": 10}
response = self.client.get(
url_for(
Expand All @@ -45,11 +36,18 @@ def test_get_biblistes(self):
assert self.schema_allnamebyListe.is_valid(data["data"])

def test_get_biblistesbyTaxref(self, listes):

response = self.client.get(
url_for("bib_listes.get_biblistesbyTaxref", regne="Animalia", group2_inpn=None),
)
# Filter test list only
data = [d for d in response.json if d["desc_liste"] == "Liste description"]
self.schema_allnamebyListe.validate(data)
assert len(data) == 1

response = self.client.get(
url_for("bib_listes.get_biblistesbyTaxref", regne="Plantae", group2_inpn="Mousses"),
)
# Filter test list only
data = [d for d in response.json if d["desc_liste"] == "Liste description"]
self.schema_allnamebyListe.validate(data)
assert len(data) == 1
Loading