Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Dec 6, 2024
2 parents ea9434d + a1fd19d commit 8e36e7e
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 114 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0
2.1.0
90 changes: 90 additions & 0 deletions apptax/migrations/versions/3c4762751898_taxref_tree_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""create vm_taxref_tree v2
Revision ID: 3c4762751898
Revises: 83d7105edb76
Create Date: 2024-12-03 13:30:26.521216
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "3c4762751898"
down_revision = "83d7105edb76"
branch_labels = None
depends_on = None


def upgrade():
op.execute("DROP MATERIALIZED VIEW IF EXISTS taxonomie.vm_taxref_tree")
op.execute(
"""
CREATE MATERIALIZED VIEW taxonomie.vm_taxref_tree AS
WITH RECURSIVE
biota AS (
SELECT
t.cd_nom,
t.cd_ref::TEXT::ltree AS path
FROM
taxonomie.taxref t
WHERE
t.cd_nom = 349525
UNION ALL
SELECT
child.cd_nom AS cd_nom,
parent.path || child.cd_ref::TEXT AS path
FROM
taxonomie.taxref child
JOIN
taxonomie.taxref child_ref ON child.cd_ref = child_ref.cd_nom
JOIN
biota parent ON parent.cd_nom = child_ref.cd_sup
),
orphans AS (
SELECT
t.cd_nom,
t.cd_ref::TEXT::ltree AS path
FROM
taxonomie.taxref t
JOIN
taxonomie.taxref t_ref ON t.cd_ref = t_ref.cd_nom
LEFT JOIN
taxonomie.taxref parent ON t_ref.cd_sup = parent.cd_nom AND parent.cd_nom != t_ref.cd_nom
WHERE
parent.cd_nom IS NULL
)
SELECT
cd_nom,
path
FROM
biota
UNION DISTINCT -- do not include biota twice
SELECT
cd_nom,
path
FROM
orphans
WITH DATA;
"""
)
op.create_index(
index_name="taxref_tree_cd_nom_idx",
schema="taxonomie",
table_name="vm_taxref_tree",
columns=["cd_nom"],
unique=True,
)
# required for these operators: <, <=, =, >=, >, @>, <@, @, ~, ?
op.create_index(
index_name="taxref_tree_path_idx",
schema="taxonomie",
table_name="vm_taxref_tree",
columns=["path"],
postgresql_using="gist",
)


def downgrade():
op.execute("DROP MATERIALIZED VIEW taxonomie.vm_taxref_tree")
77 changes: 0 additions & 77 deletions apptax/migrations/versions/83d7105edb76_taxref_tree.py

This file was deleted.

25 changes: 25 additions & 0 deletions apptax/migrations/versions/83d7105edb76_taxref_tree_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""create vm_taxref_tree v1
Revision ID: 83d7105edb76
Revises: 44447746cacc
Create Date: 2024-10-05 17:40:11.302423
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "83d7105edb76"
down_revision = "6a20cd1055ec"
branch_labels = None
depends_on = None


def upgrade():
pass


def downgrade():
op.execute("DROP MATERIALIZED VIEW IF EXISTS taxonomie.vm_taxref_tree")
19 changes: 15 additions & 4 deletions 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 Expand Up @@ -261,13 +268,17 @@ class BibListes(db.Model):

@hybrid_property
def nb_taxons(self):
return len(self.noms)
return db.session.scalar(
select([db.func.count(cor_nom_liste.c.cd_nom)]).where(
cor_nom_liste.c.id_liste == self.id_liste
)
)

@nb_taxons.expression
def nb_taxons(cls):
return (
db.select([db.func.count(cor_nom_liste.id_liste)])
.where(BibListes.id_liste == cls.id_liste)
db.select([db.func.count(cor_nom_liste.c.cd_nom)])
.where(cor_nom_liste.c.id_liste == cls.id_liste)
.label("nb_taxons")
)

Expand Down
37 changes: 23 additions & 14 deletions apptax/taxonomie/routesbiblistes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import os
import logging

from flask import Blueprint, request, current_app
from sqlalchemy import func, or_
from sqlalchemy.orm import joinedload
from flask import Blueprint
from sqlalchemy import select

from pypnusershub import routes as fnauth
from utils_flask_sqla.response import json_resp

from . import filemanager
from . import db
from .models import BibListes, Taxref
from .models import BibListes
from apptax.taxonomie.schemas import BibListesSchema

adresses = Blueprint("bib_listes", __name__)
Expand All @@ -24,26 +21,38 @@

@adresses.route("/", methods=["GET"])
@json_resp
def get_biblistes(id=None):
def get_biblistes():
"""
retourne les contenu de bib_listes dans "data"
et le nombre d'enregistrements dans "count"
"""
data = db.session.query(BibListes).all()
biblistes_records = db.session.execute(
select(
BibListes.id_liste,
BibListes.code_liste,
BibListes.nom_liste,
BibListes.desc_liste,
BibListes.nb_taxons,
BibListes.regne,
BibListes.group2_inpn,
)
).all()
biblistes_schema = BibListesSchema()
maliste = {"data": [], "count": 0}
maliste["count"] = len(data)
maliste["data"] = biblistes_schema.dump(data, many=True)
return maliste
biblistes_infos = {
"data": biblistes_schema.dump(biblistes_records, many=True),
"count": len(biblistes_records),
}

return biblistes_infos


@adresses.route("/<regne>", methods=["GET"], defaults={"group2_inpn": None})
@adresses.route("/<regne>/<group2_inpn>", methods=["GET"])
def get_biblistesbyTaxref(regne, group2_inpn):
q = db.session.query(BibListes)
q = select(BibListes)
if regne:
q = q.where(BibListes.regne == regne)
if group2_inpn:
q = q.where(BibListes.group2_inpn == group2_inpn)
results = q.all()
results = db.session.scalars(q).all()
return BibListesSchema().dump(results, many=True)
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
17 changes: 15 additions & 2 deletions apptax/taxonomie/routestmedias.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from flask import json, Blueprint, request, current_app, send_file, abort


from .models import TMedias
from .schemas import TMediasSchema
from .models import TMedias, BibTypesMedia
from .schemas import TMediasSchema, BibTypesMediaSchema

from .filemanager import FILEMANAGER

Expand All @@ -29,6 +29,19 @@ def get_tmedias(id=None):
return TMediasSchema().dump(medias, many=True)


@adresses.route("/types", methods=["GET"])
@adresses.route("/types/<int:id>", methods=["GET"])
def get_type_tmedias(id=None):
"""
Liste des types de médias
"""
if id:
type_media = BibTypesMedia.query.get(id)
return BibTypesMediaSchema().dump(type_media)
types_media = BibTypesMedia.query.all()
return BibTypesMediaSchema().dump(types_media, many=True)


@adresses.route("/bycdref/<cd_ref>", methods=["GET"])
def get_tmediasbyTaxon(cd_ref):
"""
Expand Down
9 changes: 8 additions & 1 deletion apptax/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ def liste():
"code_liste": "TEST_LIST_Plantae",
"nom_liste": "Liste test Plantae",
"desc_liste": "Liste description",
"regne": "Plantea",
"regne": "Plantae",
},
{
"code_liste": "TEST_LIST_Mousses",
"nom_liste": "Liste test Mousses",
"desc_liste": "Liste description",
"regne": "Plantae",
"group2_inpn": "Mousses",
},
]

Expand Down
Loading

0 comments on commit 8e36e7e

Please sign in to comment.