Skip to content

Commit

Permalink
Add distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Nov 9, 2023
1 parent 4e13870 commit e5715be
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
14 changes: 11 additions & 3 deletions apptax/taxonomie/routestaxref.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ def get_names():
if id_liste and not id_liste == -1:
q = q.filter(CorNomListe.id_liste.in_(tuple(id_liste)))

# filtre sur search_name
search_name = request.args.get("search_name")
if search_name:
search_name = search_name.replace(" ", "%")
Expand All @@ -491,16 +492,23 @@ def get_names():
desc(VMTaxrefListForautocomplete.cd_nom == VMTaxrefListForautocomplete.cd_ref)
)

results = q.paginate(page=page, per_page=limit, error_out=False)
items = []

# Filtrer champs retourner par la requête
fields = request.args.get("fields", type=str, default=[])
if fields:
fields = fields.split(",")
fields_to_filter = None
if fields:
fields_to_filter = [f for f in fields if getattr(VMTaxrefListForautocomplete, f, None)]
# Ajout disctinct pour ne pas avoir de doublons
for field in [
getattr(VMTaxrefListForautocomplete, f, None)
for f in fields
if getattr(VMTaxrefListForautocomplete, f, None)
]:
q = q.distinct(field)

results = q.paginate(page=page, per_page=limit, error_out=False)
items = []
for r in results.items:
data = r.as_dict(fields=fields_to_filter, exclude=["unaccent_search_name"])
if not fields or "lists" in fields:
Expand Down
11 changes: 11 additions & 0 deletions apptax/tests/test_taxref.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ def test_get_names_routes_limit_fields(self):
if data:
assert current_schema.is_valid(data)

def test_get_names_routes_limit_filter_id_liste(self):
query_string = {"limit": 10, "id_liste": "100", "fields": "cd_nom"}
response = self.client.get(url_for("taxref.get_names"), query_string=query_string)
current_schema = Schema([{"cd_nom": int}])
assert response.status_code == 200
data = response.json

if data:
assert current_schema.is_valid(data)
assert len(data) == 9

def test_get_distinct_routes(self):
response = self.client.get(url_for("taxref.getDistinctField", field="regne"))
assert response.status_code == 200
Expand Down

0 comments on commit e5715be

Please sign in to comment.