From 92bf84f67d10a1e23a496b169cb14e14a393644a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Bouttier?= Date: Tue, 8 Oct 2024 14:42:59 +0200 Subject: [PATCH] feat(taxref): taxons comparison through path --- apptax/taxonomie/models.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/apptax/taxonomie/models.py b/apptax/taxonomie/models.py index 64184c43..2addcce9 100644 --- a/apptax/taxonomie/models.py +++ b/apptax/taxonomie/models.py @@ -1,3 +1,4 @@ +from typing import Self from flask import url_for from sqlalchemy import ForeignKey, select, func, event @@ -230,6 +231,9 @@ def where_params(cls, filters=None, *, query): query = query.filter(col.ilike(value + "%")) return query + def __le__(self, other: Self) -> bool: + return self.tree <= other.tree + @serializable class BibListes(db.Model): @@ -546,6 +550,11 @@ class TaxrefTree(db.Model): taxref = db.relationship(Taxref, backref=backref("tree", uselist=False)) path = db.Column(db.String, nullable=False) + def __le__(self, other: Self) -> bool: + # self <= other means taxon other is the same or a parent of self + p1, p2 = self.path.split("."), other.path.split(".") + return len(p1) >= len(p2) and p1[: len(p2)] == p2 + # Taxref deffered properties