Skip to content

Commit

Permalink
Ajout info détail attribut
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Oct 3, 2024
1 parent 7f094f1 commit c8d3c81
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
20 changes: 16 additions & 4 deletions apptax/taxonomie/routestaxref.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

from flask import abort, jsonify, Blueprint, request
from sqlalchemy import desc, func, and_, select
from sqlalchemy.orm import joinedload
from sqlalchemy.orm.exc import NoResultFound

from utils_flask_sqla.response import json_resp
from utils_flask_sqla.generic import serializeQuery

from .models import (
from apptax.taxonomie.models import (
Taxref,
VMTaxrefListForautocomplete,
BibTaxrefHabitats,
BibTaxrefRangs,
BibTaxrefHabitats,
BibListes,
TMetaTaxref,
CorTaxonAttribut,
)

from .repositories import BdcStatusRepository
Expand All @@ -30,6 +31,15 @@
adresses = Blueprint("taxref", __name__)


def get_joinedload_when_attributs(fields):
joinedload_when_attributs = []
if [i for i in fields if i.startswith("attributs.bib_attribut")]:
joinedload_when_attributs = [
joinedload(Taxref.attributs).options(joinedload(CorTaxonAttribut.bib_attribut))
]
return joinedload_when_attributs


@adresses.route("/version", methods=["GET"])
@json_resp
def getTaxrefVersion():
Expand Down Expand Up @@ -166,7 +176,8 @@ def get_taxref_list():

count_total = db.session.scalar(query_count)

query = Taxref.joined_load(fields)
joinedload_when_attributs = get_joinedload_when_attributs(fields=fields)
query = Taxref.joined_load(fields).options(*joinedload_when_attributs)

if id_liste and "-1" not in id_liste:
query = Taxref.where_id_liste(id_liste, query=query)
Expand Down Expand Up @@ -211,7 +222,8 @@ def getTaxrefDetail(id):

fields = [f for f in fields if not f.split(".")[0] == "status"]

query = Taxref.joined_load(join_relationship)
joinedload_when_attributs = get_joinedload_when_attributs(fields=fields)
query = Taxref.joined_load(join_relationship).options(*joinedload_when_attributs)
query = query.where(Taxref.cd_nom == id)
results = db.session.scalars(query).unique().one_or_none()
if not results:
Expand Down
9 changes: 9 additions & 0 deletions apptax/taxonomie/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
VBdcStatus,
BibTaxrefHabitats,
BibTaxrefStatus,
BibAttributs,
)


Expand All @@ -25,6 +26,12 @@ class Meta:
include_fk = True


class BibAttributsSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = BibAttributs
include_fk = True


class TMediasSchema(SmartRelationshipsMixin, ma.SQLAlchemyAutoSchema):
class Meta:
model = TMedias
Expand All @@ -47,6 +54,8 @@ class Meta:
model = CorTaxonAttribut
include_fk = True

bib_attribut = fields.Nested(BibAttributsSchema, many=False)


class BibTaxrefRangsSchema(SmartRelationshipsMixin, ma.SQLAlchemyAutoSchema):
class Meta:
Expand Down
25 changes: 20 additions & 5 deletions apptax/tests/test_taxref.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ class TestAPITaxref:
}
)

schema_taxref_detail_bib_attributs = Schema(
{
"cd_nom": int,
"attributs": [
{"bib_attribut": {"label_attribut": str}, "id_attribut": int, "cd_ref": int}
],
}
)

schema_response = Schema(
{"items": list, "limit": int, "page": int, "total": int, "total_filtered": int}
)
Expand Down Expand Up @@ -180,11 +189,6 @@ def test_taxrefDetail_routes(self):
assert response.status_code == 200
assert self.schema_taxref_detail.is_valid(response.json)

def test_taxrefDetail_routes(self):
response = self.client.get(url_for("taxref.getTaxrefDetail", id=29708))
assert response.status_code == 200
assert self.schema_taxref_detail.is_valid(response.json)

def test_taxrefDetail_routes_fields(self):
response = self.client.get(
url_for(
Expand All @@ -196,6 +200,17 @@ def test_taxrefDetail_routes_fields(self):
assert response.status_code == 200
assert self.schema_taxref_detail_simple.is_valid(response.json)

def test_taxrefDetail_routes_fields_attributs(self):
response = self.client.get(
url_for(
"taxref.getTaxrefDetail",
id=67111,
fields="attributs.bib_attribut.label_attribut,cd_nom",
)
)
assert response.status_code == 200
assert self.schema_taxref_detail_bib_attributs.is_valid(response.json)

def test_taxrefDetail_filter_area(self):
area = db.session.scalar(select(LAreas).where(LAreas.area_code == "48"))
response = self.client.get(
Expand Down

0 comments on commit c8d3c81

Please sign in to comment.