Skip to content

Commit

Permalink
sqla 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
joelclems committed Jan 11, 2024
1 parent a5e728f commit 08179dd
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 74 deletions.
4 changes: 2 additions & 2 deletions backend/gn_modulator/module/breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def breadcrumbs(cls, module_code, page_code, data):
q = sm.get_row(
data[sm.Model().pk_field_name()], module_code=module_code, params={}
)
m = q.one()
m = db.session.execute(q).scalar_one()
data_label = sm.serialize(m, fields=[sm.label_field_name()])
# label_page = f"{sm.label()} {data_label[sm.label_field_name()]}"
label_page = f"{sm.label()} {getAttr(data_label, sm.label_field_name())}"
Expand All @@ -63,7 +63,7 @@ def breadcrumbs(cls, module_code, page_code, data):
if ":" in parent_page_url and data.get(sm.Model().pk_field_name()):
data_parent_key = parent_page_url.split(":")[1]
q = sm.get_row(data[sm.Model().pk_field_name()], params={})
m = q.one()
m = db.session.execute(q).scalar_one()

data_parent = sm.serialize(m, fields=[data_parent_key])
else:
Expand Down
56 changes: 27 additions & 29 deletions backend/gn_modulator/query/existing_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,37 @@ def synthese_subquery_scope(cls, id_role):
scope_expression = db.session.query(Synthese).with_entities(
Synthese.id_synthese,
sa.case(
[
(
sa.or_(
Synthese.id_digitiser == id_role,
sa.exists().where(
sa.and_(
pre_scope.c.id_synthese == Synthese.id_synthese,
sa.or_(
pre_scope.c.id_role_obs == pre_scope.c.id_role_cur,
pre_scope.c.id_role_jdd == pre_scope.c.id_role_cur,
pre_scope.c.id_role_af == pre_scope.c.id_role_cur,
),
)
),
(
sa.or_(
Synthese.id_digitiser == id_role,
sa.exists().where(
sa.and_(
pre_scope.c.id_synthese == Synthese.id_synthese,
sa.or_(
pre_scope.c.id_role_obs == pre_scope.c.id_role_cur,
pre_scope.c.id_role_jdd == pre_scope.c.id_role_cur,
pre_scope.c.id_role_af == pre_scope.c.id_role_cur,
),
)
),
1,
),
(
sa.or_(
sa.exists().where(
sa.and_(
pre_scope.c.id_synthese == Synthese.id_synthese,
sa.or_(
pre_scope.c.id_organisme_obs == pre_scope.c.id_organisme_cur,
pre_scope.c.id_organisme_jdd == pre_scope.c.id_organisme_cur,
pre_scope.c.id_organisme_af == pre_scope.c.id_organisme_cur,
),
)
),
1,
),
(
sa.or_(
sa.exists().where(
sa.and_(
pre_scope.c.id_synthese == Synthese.id_synthese,
sa.or_(
pre_scope.c.id_organisme_obs == pre_scope.c.id_organisme_cur,
pre_scope.c.id_organisme_jdd == pre_scope.c.id_organisme_cur,
pre_scope.c.id_organisme_af == pre_scope.c.id_organisme_cur,
),
)
),
2,
),
],
2,
),
else_=3,
).label("scope"),
)
Expand Down
2 changes: 1 addition & 1 deletion backend/gn_modulator/query/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def process_filters(Model, query, filters):
filters_processed, query = process_filter_array(Model, query, filters)

if filters_processed is not None:
query = query.filter(filters_processed)
query = query.where(filters_processed)

return query

Expand Down
3 changes: 2 additions & 1 deletion backend/gn_modulator/query/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def query_list(Model, module_code, action, params, query_type, id_role=None):
TODO ajout permission_object_code ??
"""
query = Model.query

query = sa.select(Model)

model_pk_fields = [getattr(Model, pk_field_name) for pk_field_name in Model.pk_field_names()]

Expand Down
11 changes: 7 additions & 4 deletions backend/gn_modulator/routes/utils/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_list_rest(module_code, object_code, additional_params={}):
response.mimetype = "text/plain"
return response

res_list = [] if params.get("only_info") else q_list.all()
res_list = [] if params.get("only_info") else db.session.execute(q_list).all()

out = {
**query_infos,
Expand Down Expand Up @@ -84,7 +84,7 @@ def get_one_rest(module_code, object_code, value):
params=params,
)

m = q.one()
m = db.session.execute(q).unique().scalar_one()
except sm.errors.SchemaUnsufficientCruvedRigth as e:
return f"Erreur Cruved : {str(e)}", 403

Expand Down Expand Up @@ -154,13 +154,16 @@ def delete_rest(module_code, object_code, value):

params = parse_request_args(object_definition)

dict_out = sm.get_row_as_dict(
q = sm.get_row(
value,
field_name=params.get("field_name"),
module_code=permission_module_code,
action="D",
fields=params.get("fields"),
params=params,
)
m = db.session.execute(q).scalar_one()

dict_out = sm.serialize(m, fields=params.get("fields"), as_geojson=params.get("as_geojson"))

try:
sm.delete_row(
Expand Down
3 changes: 3 additions & 0 deletions backend/gn_modulator/schema/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def process_relation_model(self, key, relationship_def, Model):

kwargs = {}

if relationship_def.get("overlaps"):
kwargs["overlaps"] = relationship_def["overlaps"]

if relationship_def.get("relation_type") == "1-1":
kwargs["uselist"] = False

Expand Down
41 changes: 32 additions & 9 deletions backend/gn_modulator/schema/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ def update_row(
query_type="update",
)

m = q.one()
m = db.session.execute(q).unique().scalar_one()

if not self.is_new_data(m, data):
return m, False

Expand Down Expand Up @@ -219,13 +220,31 @@ def delete_row(
action="D",
params=params,
query_type="delete",
).cte("pre_delete")

Model = self.Model()
table = Model.__table__
q_delete = sa.delete(table).where(
sa.and_(
*map(
lambda x: getattr(table.c, x) == getattr(subquery_delete.c, x),
Model.pk_field_names(),
)
)
)

# patch pourris pourquoi sql ne met pas USING PDBDMSRMLGP???
propper_sql_delete = str(q_delete.compile(compile_kwargs={"literal_binds": True})).replace(
", pre_delete", "USING pre_delete"
)

db.session.execute(
sa.text(propper_sql_delete), execution_options={"synchronize_session": False}
)
# https://stackoverflow.com/questions/49794899/flask-sqlalchemy-delete-query-failing-with-could-not-evaluate-current-criteria?noredirect=1&lq=1
if not multiple:
subquery_delete.one()
subquery_delete.delete(synchronize_session=False)
db.session.flush()
# db.session.execute(q)
# m.delete(synchronize_session=False)
# db.session.flush()

if commit:
db.session.commit()
Expand All @@ -238,8 +257,10 @@ def get_query_infos(self, module_code=MODULE_CODE, action="R", params={}, url=No
action=action,
params=params,
query_type="total",
)
count_total = subquery_count_total.count()
).cte("count_total")
count_total = db.session.execute(
sa.select(sa.func.count()).select_from(subquery_count_total)
).scalar_one()

if params.get("filters"):
subquery_count_filtered = query_list(
Expand All @@ -248,9 +269,11 @@ def get_query_infos(self, module_code=MODULE_CODE, action="R", params={}, url=No
action=action,
params=params,
query_type="filtered",
)
).cte("count_filtered")

count_filtered = subquery_count_filtered.count()
count_filtered = db.session.execute(
sa.select(sa.func.count()).select_from(subquery_count_filtered)
).scalar_one()
else:
count_filtered = count_total

Expand Down
4 changes: 2 additions & 2 deletions backend/gn_modulator/schema/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def serialize(self, m, fields=None, as_geojson=False, geometry_field_name=None):
return data

def is_res_multiple_columns(self, m):
return isinstance(m, tuple)
return isinstance(m, sa.engine.row.Row)

def get_res_model(self, m):
return m[0] if self.is_res_multiple_columns(m) else m
Expand Down Expand Up @@ -365,7 +365,7 @@ def get_row_as_dict(
params={"fields": fields},
query_type=query_type,
)
m = q.one()
m = db.session.execute(q).one()

except sa.orm.exc.NoResultFound:
return None
Expand Down
4 changes: 3 additions & 1 deletion backend/gn_modulator/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def passages_faune_with_diagnostic(users):
uuids = ["0c92af92-000b-401c-9994-f2c12470493a", "0c92af92-000b-401c-9994-f2c12470493b"]
passages_faune = []

organisme = Organisme.query.filter_by(nom_organisme="ALL").one()
organisme = db.session.execute(
sa.select(Organisme).filter_by(nom_organisme="ALL")
).scalar_one()

with db.session.begin_nested():
for uuid in uuids:
Expand Down
2 changes: 1 addition & 1 deletion backend/gn_modulator/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ def test_schematisable(self):
{"fields": ["id_passage_faune", "nomenclatures_ouvrage_type.label_fr"]},
"select",
)
res = q.all()
res = db.session.execute(q).unique()
sm.serialize_list(res, fields=["nomenclatures_ouvrage_type.label_fr"])
16 changes: 8 additions & 8 deletions backend/gn_modulator/tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_repo_diag(self, users, passages_faune_with_diagnostic):
id_role=users["admin_user"].id_role,
)

m_list = q.all()
m_list = db.session.execute(q)
res = sm.serialize_list(m_list, fields)

assert True
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_repo_pf_rel(self, passages_faune_with_diagnostic, users):
"select",
id_role=users["admin_user"].id_role,
)
m_list = q.all()
m_list = db.session.execute(q).unique().all()
res = sm.serialize_list(m_list, fields)

assert len(res) == 2
Expand Down Expand Up @@ -270,7 +270,7 @@ def test_repo_pf_cruved(self, passages_faune_with_diagnostic, users):
"select",
id_role=users["admin_user"].id_role,
)
m_list = q.all()
m_list = db.session.execute(q)
res = sm.serialize_list(m_list, fields)

assert len(res) == 2
Expand All @@ -283,7 +283,7 @@ def test_repo_pf_cruved(self, passages_faune_with_diagnostic, users):
"select",
id_role=users["user"].id_role,
)
m_list = q.all()
m_list = db.session.execute(q)
res = sm.serialize_list(m_list, fields)

assert len(res) == 1
Expand Down Expand Up @@ -311,7 +311,7 @@ def test_repo_pf_filter_has_diagnostic(self, passages_faune_with_diagnostic, use
"select",
id_role=users["admin_user"].id_role,
)
m_list = q.all()
m_list = db.session.execute(q).unique().all()
res = sm.serialize_list(m_list, fields)
assert len(res) == 2

Expand All @@ -331,7 +331,7 @@ def test_repo_synthese_d_within(
"select",
id_role=users["admin_user"].id_role,
)
m_list = q.all()
m_list = db.session.execute(q).unique().all()
res = sm.serialize_list(m_list, fields=["id_synthese"])

assert len(res) == 4
Expand All @@ -355,7 +355,7 @@ def test_repo_synthese_scope(self, synthese_data, users, datasets):
id_role=users[user].id_role,
)

m_list = q.all()
m_list = db.session.execute(q)
res[user] = sm.serialize_list(m_list, fields=fields)

if user in ["admin_user", "user", "self_user"]:
Expand Down Expand Up @@ -386,7 +386,7 @@ def test_repo_synthese_permission(self, synthese_sensitive_data, users, g_permis
"select",
id_role=users[user].id_role,
)
m_list = q.all()
m_list = db.session.execute(q)
res[user] = sm.serialize_list(m_list, fields=fields)

for user in users:
Expand Down
13 changes: 9 additions & 4 deletions contrib/m_sipaf/backend/m_sipaf/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class PassageFaune(db.Model):
foreign_keys=[TMedias.uuid_attached_row],
cascade="all",
lazy="select",
overlaps="medias",
)

# actors
Expand All @@ -187,18 +188,19 @@ class PassageFaune(db.Model):
geom_text = column_property(sa.func.st_astext(geom).label("geom_text"))

label_infrastructures = column_property(
sa.select([sa.func.string_agg(LLinears.linear_name, sa.literal_column("', '"))])
sa.select(sa.func.string_agg(LLinears.linear_name, sa.literal_column("', '")))
.where(
sa.and_(
CorPfLinear.id_passage_faune == id_passage_faune,
CorPfLinear.id_linear == LLinears.id_linear,
)
)
.label("label_infrastructures")
.scalar_subquery()
)

label_communes = column_property(
sa.select([sa.func.string_agg(LAreas.area_name, sa.literal_column("', '"))])
sa.select(sa.func.string_agg(LAreas.area_name, sa.literal_column("', '")))
.where(
sa.and_(
CorPfArea.id_passage_faune == id_passage_faune,
Expand All @@ -208,10 +210,11 @@ class PassageFaune(db.Model):
)
)
.label("label_communes")
.scalar_subquery()
)

label_departements = column_property(
sa.select([sa.func.string_agg(LAreas.area_name, sa.literal_column("', '"))])
sa.select(sa.func.string_agg(LAreas.area_name, sa.literal_column("', '")))
.where(
sa.and_(
CorPfArea.id_passage_faune == id_passage_faune,
Expand All @@ -221,10 +224,11 @@ class PassageFaune(db.Model):
)
)
.label("label_departements")
.scalar_subquery()
)

label_regions = column_property(
sa.select([sa.func.string_agg(LAreas.area_name, sa.literal_column("', '"))])
sa.select(sa.func.string_agg(LAreas.area_name, sa.literal_column("', '")))
.where(
sa.and_(
CorPfArea.id_passage_faune == id_passage_faune,
Expand All @@ -234,6 +238,7 @@ class PassageFaune(db.Model):
)
)
.label("label_regions")
.scalar_subquery()
)


Expand Down
Loading

0 comments on commit 08179dd

Please sign in to comment.