From 2d35876f542f246313baf3810b8e6b95c425b7e8 Mon Sep 17 00:00:00 2001 From: psrok1 Date: Wed, 11 Dec 2024 17:12:28 +0100 Subject: [PATCH] Don't load filtered-out parents eagerly on every Object.access --- mwdb/model/object.py | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/mwdb/model/object.py b/mwdb/model/object.py index 5710525b..e6805bc0 100644 --- a/mwdb/model/object.py +++ b/mwdb/model/object.py @@ -7,7 +7,7 @@ from sqlalchemy import and_, cast, distinct, exists, func, select from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.exc import IntegrityError -from sqlalchemy.orm import aliased, column_property, contains_eager +from sqlalchemy.orm import column_property from sqlalchemy.sql.expression import true from mwdb.core.capabilities import Capabilities @@ -467,35 +467,11 @@ def access(cls, identifier, requestor=None): if requestor is None: requestor = g.auth_user - obj = cls.get(identifier) + obj = cls.get(identifier).first() # If object doesn't exist - it doesn't exist - if obj.first() is None: + if obj is None: return None - # In that case we want only those parents to which requestor has access. - stmtp = ( - db.session.query(Object) - .join(relation, relation.c.parent_id == Object.id) - .filter( - Object.id.in_( - db.session.query(relation.c.parent_id).filter( - relation.c.child_id == obj.first().id - ) - ) - ) - .order_by(relation.c.creation_time.desc()) - .filter(requestor.has_access_to_object(Object.id)) - ) - stmtp = stmtp.subquery() - - parent = aliased(Object, stmtp) - - obj = ( - obj.outerjoin(parent, Object.parents) - .options(contains_eager(Object.parents, alias=parent)) - .all()[0] - ) - # Ok, now let's check whether requestor has explicit access if obj.has_explicit_access(requestor): return obj