Skip to content

Commit

Permalink
Route taxref : filtre cd_nom valeur multiple (#590)
Browse files Browse the repository at this point in the history
* Route taxref : filtre cd_nom valeur multiple
  • Loading branch information
amandine-sahl authored Dec 6, 2024
1 parent 6c68e5d commit bd41185
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
9 changes: 8 additions & 1 deletion apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,14 @@ def where_id_liste(cls, id_liste, *, query):
def where_params(cls, filters=None, *, query):

for filter in filters:
if hasattr(Taxref, filter) and filters[filter] != "":
# Test empty values
if not filters[filter]:
continue

if hasattr(Taxref, filter) and isinstance(filters[filter], list):
col = getattr(Taxref, filter)
query = query.filter(col.in_(tuple(filters[filter])))
elif hasattr(Taxref, filter) and filters[filter] != "":
col = getattr(Taxref, filter)
query = query.filter(col == filters[filter])
elif filter == "is_ref" and filters[filter] == "true":
Expand Down
12 changes: 10 additions & 2 deletions apptax/taxonomie/routestaxref.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,18 @@ def get_taxref_list():
limit = request.values.get("limit", 20, int)
page = request.values.get("page", 1, int)
id_liste = None
cd_nom = None

if "id_liste" in request.values:
id_liste = request.values.get("id_liste").split(",")
id_liste = (
request.values.get("id_liste").split(",") if request.values.get("id_liste") else None
)
if "cd_nom" in request.values:
cd_nom = request.values.get("cd_nom").split(",") if request.values.get("cd_nom") else None

fields = request.values.get("fields", type=str, default=[])
parameters = request.values.to_dict()
parameters["cd_nom"] = cd_nom

dump_options = {}
if fields:
Expand All @@ -178,10 +186,10 @@ def get_taxref_list():

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

if id_liste and "-1" not in id_liste:
query = Taxref.where_id_liste(id_liste, query=query)

count_filter = db.session.scalar(
db.select(func.count()).select_from(
Taxref.where_params(parameters, query=query),
Expand Down
24 changes: 24 additions & 0 deletions apptax/tests/test_taxref.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,27 @@ def test_get_taxref_list(self, liste_with_names):
assert response.status_code == 200
response_json = response.json
assert len(response_json["items"]) == limit

def test_get_taxref_list_filters_cd_noms(self):
# Test de la route taxref avec des filtres sur le cd_nom
# 1 cd_nom
response = self.client.get(
url_for("taxref.get_taxref_list"),
query_string={"cd_nom": 103536},
)
assert response.status_code == 200
assert len(response.json["items"]) == 1

# Plusieurs cd_nom
response = self.client.get(
url_for("taxref.get_taxref_list"),
query_string={"cd_nom": "103536, 461885,2891"},
)
assert len(response.json["items"]) == 3

# Valeur vide
response = self.client.get(
url_for("taxref.get_taxref_list"),
query_string={"cd_nom": ""},
)
assert response.status_code == 200
3 changes: 2 additions & 1 deletion docs/developpement.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
- limit (defaut = 50) : nombre d'éléments à retourner
- page (defaut = 0) : page à retourner
- is_ref (default = false) : ne retourne que les noms valides (cd_nom = cd_ref)
- id_liste
- id_liste : liste d'identifiant des listes
- cd_nom : liste de cd_nom
- fields (permet de spécifier les champs renvoyés). Permet aussi de récupérer les données secondaires
non renvoyées par défaut, en les spécifiant explicitement (`fields=status,rang,medias,attributs,synonymes,listes`)
- nomColonne : Permet de filtrer
Expand Down

0 comments on commit bd41185

Please sign in to comment.