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

Route taxref : filtre cd_nom valeur multiple #590

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
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
Loading