Skip to content

Commit

Permalink
sqlalchemy update for hierarchy and river basins
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Corny committed Apr 5, 2024
1 parent e18f905 commit 8cd4a12
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
20 changes: 9 additions & 11 deletions backend/gn_module_zh/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def __get_qualif_heritage(self):
if not nb:
nb = 0
try:
qualif = DB.session.scalars(
qualif = DB.session.execute(
select(CorRbRules, TItems, CorItemValue)
.join(TItems, TItems.cor_rule_id == CorRbRules.cor_rule_id)
.join(CorItemValue, TItems.attribute_id == CorItemValue.attribute_id)
Expand Down Expand Up @@ -602,7 +602,7 @@ def __get_qualif_management(self):
)
)
)
.scalar_one()
.all()[0]
.TNomenclatures,
"id_nomenclature",
)
Expand Down Expand Up @@ -887,12 +887,12 @@ def __get_rule_name(self):
try:
return (
DB.session.execute(
select(TRules, BibHierSubcategories)
select(TRules, BibHierCategories)
.join(TRules)
.where(TRules.rule_id == self.rule_id)
)
.scalar_one()
.BibHierSubcategories.label.capitalize()
.all()[0]
.BibHierCategories.label.capitalize()
)
except NoResultFound:
pass
Expand All @@ -902,7 +902,7 @@ def __get_rule_name(self):
.join(TRules)
.where(TRules.rule_id == self.rule_id)
)
.scalar_one()
.all()[0]
.BibHierCategories.label.capitalize()
)

Expand All @@ -920,9 +920,7 @@ def __get_knowledge_mnemo(self):
TNomenclatures.id_nomenclature == BibNoteTypes.id_knowledge,
)
.where(BibNoteTypes.note_id == self.knowledge)
)
.scalar_one()
.TNomenclatures,
).scalar_one(),
"mnemonique",
)
except Exception as e:
Expand Down Expand Up @@ -1214,7 +1212,7 @@ def __get_rb(self):
select(CorZhRb, TRiverBasin).join(TRiverBasin).where(CorZhRb.id_zh == self.id_zh)
)
.scalar_one()
.TRiverBasin.id_rb
.id_rb
)

def __check_if_rules(self):
Expand Down Expand Up @@ -1284,7 +1282,7 @@ def get_all_hierarchy_fields(id_rb: int):
tableName="all_rb_rules",
schemaName="pr_zh",
filters={"id_rb": id_rb, "orderby": "name"},
limit=-1,
limit=100000,
)

results = query.return_query()
Expand Down
6 changes: 3 additions & 3 deletions backend/gn_module_zh/model/zh_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,19 @@ def get_tzh_by_id(id):
@staticmethod
def get_zh_area_intersected(zh_area_type, id_zh_geom):
if zh_area_type == "river_basin":
q = DB.session.execute(
q = DB.session.scalars(
select(TRiverBasin).where(
TRiverBasin.geom.ST_Intersects(cast(id_zh_geom, Geography))
)
).all()
if zh_area_type == "hydro_area":
q = DB.session.execute(
q = DB.session.scalars(
select(THydroArea).where(
THydroArea.geom.ST_Intersects(cast(id_zh_geom, Geography))
)
).all()
if zh_area_type == "fct_area":
q = DB.session.execute(
q = DB.session.scalars(
select(TFctArea).where(TFctArea.geom.ST_Intersects(cast(id_zh_geom, Geography)))
).all()
return q
Expand Down
21 changes: 8 additions & 13 deletions backend/gn_module_zh/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ def generate_global_attributes_subquery(attributes: list, global_notes: dict):
"""
Generates the subquery taking care only on "GlobalMarks"
"""
# TODO: sqlalchemy1.4
subquery = DB.session.query(CorZhNotes.id_zh)
subquery = select(CorZhNotes.id_zh)
note_query = func.sum(CorZhNotes.note)

subquery = subquery.join(CorRbRules, CorRbRules.cor_rule_id == CorZhNotes.cor_rule_id).join(
Expand Down Expand Up @@ -407,9 +406,8 @@ def generate_global_attributes_subquery(attributes: list, global_notes: dict):


def generate_volet(subquery, volet: str, attribute_list: list, global_notes: dict, note_query):
# TODO: sqlalchemy1.4
if volet.lower() in [VOLET1.lower(), VOLET2.lower()]:
subquery = subquery.join(BibHierPanes, BibHierPanes.pane_id == TRules.pane_id).filter(
subquery = subquery.join(BibHierPanes, BibHierPanes.pane_id == TRules.pane_id).where(
BibHierPanes.label == volet
)
subquery = generate_volet_subquery(
Expand All @@ -429,7 +427,6 @@ def generate_volet_subquery(subquery, volet, attribute_list: list, global_notes:
"""
Subquery for "volet"
"""
# TODO: sqlalchemy1.4
volet_nb = COR_VOLET[volet]
max_note = global_notes[volet_nb]
and_query = []
Expand All @@ -443,8 +440,7 @@ def generate_volet_subquery(subquery, volet, attribute_list: list, global_notes:


def generate_rub(subquery, rub: str, attribute_list: list, global_notes: dict, note_query):
# TODO: sqlalchemy1.4
subquery = subquery.join(BibHierCategories, BibHierCategories.cat_id == TRules.cat_id).filter(
subquery = subquery.join(BibHierCategories, BibHierCategories.cat_id == TRules.cat_id).where(
BibHierCategories.label == rub
)
# Takes care of the several attributes choosen by the user.
Expand Down Expand Up @@ -500,8 +496,7 @@ def generate_rub(subquery, rub: str, attribute_list: list, global_notes: dict, n


def generate_attributes_subquery(attributes: list):
# TODO: sqlalchemy1.4
subquery = DB.session.query(CorZhNotes.id_zh)
subquery = select(CorZhNotes.id_zh)
attribute_ids = []
note_type_ids = []
cor_rule_ids = []
Expand All @@ -514,10 +509,10 @@ def generate_attributes_subquery(attributes: list):

# TODO: see if all of these are usefull... Are cor_rule_id with note sufficient?
subquery = (
subquery.filter(CorZhNotes.attribute_id.in_(attribute_ids))
.filter(CorZhNotes.note_type_id.in_(note_type_ids))
.filter(CorZhNotes.cor_rule_id.in_(cor_rule_ids))
.filter(CorZhNotes.note.in_(notes))
subquery.where(CorZhNotes.attribute_id.in_(attribute_ids))
.where(CorZhNotes.note_type_id.in_(note_type_ids))
.where(CorZhNotes.cor_rule_id.in_(cor_rule_ids))
.where(CorZhNotes.note.in_(notes))
)

return subquery.subquery()

0 comments on commit 8cd4a12

Please sign in to comment.