mistercrunch commented on code in PR #34836: URL: https://github.com/apache/superset/pull/34836#discussion_r2305880408
########## superset/daos/base.py: ########## @@ -47,12 +47,41 @@ class BaseDAO(Generic[T]): Child classes can register base filtering to be applied to all filter methods """ id_column_name = "id" + uuid_column_name = "uuid" def __init_subclass__(cls) -> None: cls.model_cls = get_args( cls.__orig_bases__[0] # type: ignore # pylint: disable=no-member )[0] + @classmethod + def find_by_id_or_uuid( + cls, + model_id_or_uuid: str, + skip_base_filter: bool = False, + ) -> T | None: + """ + Find a model by id, if defined applies `base_filter` + """ + query = db.session.query(cls.model_cls) + if cls.base_filter and not skip_base_filter: + data_model = SQLAInterface(cls.model_cls, db.session) + query = cls.base_filter( # pylint: disable=not-callable + cls.id_column_name, data_model + ).apply(query, None) + id_column = getattr(cls.model_cls, cls.id_column_name) + uuid_column = getattr(cls.model_cls, cls.uuid_column_name) + + if model_id_or_uuid.isdigit(): + filter = id_column == int(model_id_or_uuid) + else: + filter = uuid_column == model_id_or_uuid + try: + return query.filter(filter).one_or_none() + except StatementError: + # can happen if int is passed instead of a string or similar + return None Review Comment: i guess mypy should complain if using an `int` anywhere in the codebase. Since this is a base function going to get reused it could be good to just support `int` here. Might even be a tiny bit cheaper to validate the type than `isdigit()` is -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org