Skip to content

Commit

Permalink
Add routes names
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Nov 9, 2023
1 parent a7f4dd0 commit 36733d8
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
8 changes: 6 additions & 2 deletions apptax/taxonomie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BibNoms(db.Model):
nom_francais = db.Column(db.Unicode)
comments = db.Column(db.Unicode)

taxref = db.relationship("Taxref")
taxref = db.relationship("Taxref", back_populates="bib_nom")
attributs = db.relationship("CorTaxonAttribut")
listes = db.relationship("CorNomListe")
# medias relationship defined through backref
Expand Down Expand Up @@ -125,6 +125,8 @@ class Taxref(db.Model):
group3_inpn = db.Column(db.Unicode)
url = db.Column(db.Unicode)

bib_nom = db.relationship("BibNoms", back_populates="taxref")

@hybrid_property
def nom_vern_or_lb_nom(self):
return self.nom_vern if self.nom_vern else self.lb_nom
Expand Down Expand Up @@ -229,7 +231,7 @@ class VMTaxrefListForautocomplete(db.Model):
__tablename__ = "vm_taxref_list_forautocomplete"
__table_args__ = {"schema": "taxonomie"}
gid = db.Column(db.Integer, primary_key=True)
cd_nom = db.Column(db.Integer)
cd_nom = db.Column(db.Integer, ForeignKey(Taxref.cd_nom))
search_name = db.Column(db.Unicode)
unaccent_search_name = db.Column(db.Unicode)
cd_ref = db.Column(db.Integer)
Expand All @@ -240,6 +242,8 @@ class VMTaxrefListForautocomplete(db.Model):
group2_inpn = db.Column(db.Unicode)
group3_inpn = db.Column(db.Unicode)

taxon = db.relationship("Taxref")

def __repr__(self):
return "<VMTaxrefListForautocomplete %r>" % self.search_name

Expand Down
64 changes: 64 additions & 0 deletions apptax/taxonomie/routestaxref.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,67 @@ def get_AllTaxrefNameByListe(code_liste=None):
def get_bib_hab():
data = db.session.query(BibTaxrefHabitats).all()
return [d.as_dict() for d in data]


@adresses.route("/names", methods=["GET"])
@json_resp
def get_names():
"""
Route utilisée pour retourner les résultats de taxref avec autocompletes
params GET (facultatifs):
- search_name : nom recherché. Recherche basée sur la fonction
ilike de SQL avec un remplacement des espaces par %
- fields : liste des champs à retourner (liste des champs de VMTaxrefListForautocomplete + lists)
- id_liste : filtre sur une ou des listes associées à un taxon
- limit: nombre de résultats
- offset: numéro de la page
"""
limit = request.args.get("limit", 20, int)
page = request.args.get("page", 1, int)

q = VMTaxrefListForautocomplete.query.join(
Taxref).outerjoin(BibNoms).outerjoin(CorNomListe)

id_liste = request.args.getlist("id_liste", None)

if id_liste and not id_liste == -1:
q = q.filter(CorNomListe.id_liste.in_(tuple(id_liste)))

search_name = request.args.get("search_name")
if search_name:
search_name = search_name.replace(" ", "%")
q = q.filter(
VMTaxrefListForautocomplete.unaccent_search_name.ilike(
func.unaccent("%" + search_name + "%")
)
)
q = q.order_by(
desc(VMTaxrefListForautocomplete.cd_nom ==
VMTaxrefListForautocomplete.cd_ref)
)

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

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)]

for r in results.items:
data = r.as_dict(fields=fields_to_filter,
exclude=["unaccent_search_name"])
if not fields or "lists" in fields:
id_listes = []
if r.taxon.bib_nom:
id_listes = [l.id_liste for l in r.taxon.bib_nom[0].listes]
items.append(dict(data, lists=id_listes))
else:
items.append(data)

return items
64 changes: 63 additions & 1 deletion apptax/tests/test_taxref.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,23 @@ class TestAPITaxref:
}
]
)

schema_names = Schema(
[
{
"gid": int,
"cd_nom": int,
"cd_ref": int,
"search_name": str,
"nom_valide": str,
"nom_vern": Or(None, str),
"lb_nom": str,
"regne": str,
"group2_inpn": str,
"group3_inpn": str,
"lists": [int]
}
]
)
schema_taxref_detail = Schema(
{
"cd_nom": int,
Expand Down Expand Up @@ -96,6 +112,52 @@ def test_get_allnamebyListe_routes_without_list_filter_group3(self):
if data:
assert self.schema_allnamebyListe.is_valid(data)

def test_get_names_routes(self):
query_string = {"limit": 10}
response = self.client.get(
url_for("taxref.get_names"), query_string=query_string
)
assert response.status_code == 200
data = response.json
if data:
assert self.schema_names.is_valid(data)

def test_get_names_routes_without_list_filter_search_name(self):
query_string = {
"limit": 10,
"search_name": "poa"
}
response = self.client.get(
url_for("taxref.get_names"), query_string=query_string
)
assert response.status_code == 200
data = response.json
if data:
assert self.schema_names.is_valid(data)

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

if data:
assert current_schema.is_valid(data)

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 36733d8

Please sign in to comment.