Skip to content

Commit

Permalink
drop .query
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize authored and bouttier committed Dec 19, 2023
1 parent 96dd4b5 commit 699a5e1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
29 changes: 17 additions & 12 deletions src/pypnnomenclature/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask_admin.contrib.sqla.filters import BaseSQLAFilter
from .models import TNomenclatures, BibNomenclaturesTypes
from .env import db
from sqlalchemy import select


# https://github.com/flask-admin/flask-admin/issues/1807
Expand All @@ -31,11 +32,12 @@ def operation(self):
def get_dynamic_options(self, view):
if has_app_context():
if not hasattr(g, "TNomenclatureFiltersType"):
bib_nomenc_labels = db.session.scalars(
db.select(BibNomenclaturesTypes).order_by(BibNomenclaturesTypes.label_default)
).all()
g.TNomenclatureFiltersType = [
(nomenclature.id_type, nomenclature.label_default)
for nomenclature in db.session.query(BibNomenclaturesTypes).order_by(
BibNomenclaturesTypes.label_default
) # noqa
for nomenclature in bib_nomenc_labels # noqa
]
yield from g.TNomenclatureFiltersType

Expand All @@ -56,11 +58,12 @@ def operation(self):
def get_dynamic_options(self, view):
if has_app_context():
if not hasattr(g, "TNomenclatureFiltersMnemonique"):
bib_nomenc_type = db.session.scalars(
db.select(BibNomenclaturesTypes).order_by(BibNomenclaturesTypes.mnemonique)
).all()
g.TNomenclatureFiltersMnemonique = [
(nomenclature.id_type, nomenclature.mnemonique)
for nomenclature in db.session.query(BibNomenclaturesTypes).order_by(
BibNomenclaturesTypes.mnemonique
) # noqa
for nomenclature in bib_nomenc_type # noqa
]
yield from g.TNomenclatureFiltersMnemonique

Expand Down Expand Up @@ -129,11 +132,12 @@ def operation(self):
def get_dynamic_options(self, view):
if has_app_context():
if not hasattr(g, "BibNomenclatureFiltersLabel"):
nomenc_def_label = db.session.scalars(
select(BibNomenclaturesTypes).order_by(BibNomenclaturesTypes.label_default)
).all()
g.BibNomenclatureFiltersLabel = [
(nomenclature.label_default, nomenclature.label_default)
for nomenclature in db.session.query(BibNomenclaturesTypes).order_by(
BibNomenclaturesTypes.label_default
) # noqa
for nomenclature in nomenc_def_label # noqa
]
yield from g.BibNomenclatureFiltersLabel

Expand Down Expand Up @@ -161,11 +165,12 @@ def apply(self, query, value, alias=None):
def get_dynamic_options(self, view):
if has_app_context():
if not hasattr(g, "BibNomenclatureFiltersMnemonique"):
nomenc_mnemonique = db.session.scalars(
select(BibNomenclaturesTypes).order_by(BibNomenclaturesTypes.mnemonique)
).all()
g.BibNomenclatureFiltersMnemonique = [
(nomenclature.mnemonique, nomenclature.mnemonique)
for nomenclature in db.session.query(BibNomenclaturesTypes).order_by(
BibNomenclaturesTypes.mnemonique
) # noqa
for nomenclature in nomenc_mnemonique # noqa
]
yield from g.BibNomenclatureFiltersMnemonique

Expand Down
36 changes: 15 additions & 21 deletions src/pypnnomenclature/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
VNomenclatureTaxonomie,
BibNomenclaturesTypeTaxo,
)
from sqlalchemy import text
from sqlalchemy import text, select

from .env import db

Expand All @@ -28,41 +28,37 @@ def get_nomenclature_list(
Récupération de la liste des termes d'un type de nomenclature
"""

q = db.session.query(BibNomenclaturesTypes)
q = select(BibNomenclaturesTypes)
if filter_params is None:
filter_params = []

if code_type:
nomenclature = q.filter_by(mnemonique=code_type).first()
nomenclature = db.session.scalars(q.filter_by(mnemonique=code_type).limit(1)).first()
elif id_type:
nomenclature = q.filter_by(id_type=id_type).first()
nomenclature = db.session.scalars(q.filter_by(id_type=id_type).limit(1)).first()
else:
nomenclature = None

if not nomenclature:
return None

# Terme de nomenclatures
q = (
db.session.query(TNomenclatures)
.filter_by(id_type=nomenclature.id_type)
.filter_by(active=True)
)
q = select(TNomenclatures).filter_by(id_type=nomenclature.id_type).filter_by(active=True)

# Filtrer sur la hiérarchie
if hierarchy:
q = q.filter(TNomenclatures.hierarchy.like("{}%".format(hierarchy)))
q = q.where(TNomenclatures.hierarchy.like("{}%".format(hierarchy)))
if current_app.config["ENABLE_NOMENCLATURE_TAXONOMIC_FILTERS"]:
# Filtrer en fonction du groupe taxonomie
if regne:
q = q.join(
VNomenclatureTaxonomie,
VNomenclatureTaxonomie.id_nomenclature == TNomenclatures.id_nomenclature,
).filter(VNomenclatureTaxonomie.regne.in_(("all", regne)))
).where(VNomenclatureTaxonomie.regne.in_(("all", regne)))
if group2_inpn:
q = q.filter(VNomenclatureTaxonomie.group2_inpn.in_(("all", group2_inpn)))
q = q.where(VNomenclatureTaxonomie.group2_inpn.in_(("all", group2_inpn)))
if "cd_nomenclature" in filter_params:
q = q.filter(TNomenclatures.cd_nomenclature.in_(filter_params.getlist("cd_nomenclature")))
q = q.where(TNomenclatures.cd_nomenclature.in_(filter_params.getlist("cd_nomenclature")))
# Ordonnancement
if "orderby" in filter_params:
order_col = getattr(TNomenclatures, filter_params["orderby"])
Expand All @@ -73,7 +69,7 @@ def get_nomenclature_list(

q = q.order_by(order_col)
# @TODO Autres filtres
data = q.all()
data = db.session.scalars(q).all()

response = nomenclature.as_dict()
if data:
Expand Down Expand Up @@ -113,9 +109,9 @@ def get_nomenclature_with_taxonomy_list():
Fetch nomenclature definition list with taxonomy
"""

q = db.session.query(BibNomenclaturesTypeTaxo).order_by("mnemonique")
q = select(BibNomenclaturesTypeTaxo).order_by("mnemonique")

nomenclature_types = q.all()
nomenclature_types = db.session.scalars(q).all()
data = list()

for t in nomenclature_types:
Expand Down Expand Up @@ -177,11 +173,9 @@ def get_nomenclature_id_term(cd_type, cd_term, raise_exp=True):

t = text("SELECT ref_nomenclatures.get_id_nomenclature(:cd_type, :cd_term) as id")
try:
value = (
db.session.query("id")
.from_statement(t.params(cd_type=cd_type, cd_term=cd_term))
.first()
)
value = db.session.scalars(
select("id").from_statement(t.params(cd_type=cd_type, cd_term=cd_term).limit(1))
).first()
return value
except Exception as e:
if raise_exp:
Expand Down

0 comments on commit 699a5e1

Please sign in to comment.