dankor commented on code in PR #34836:
URL: https://github.com/apache/superset/pull/34836#discussion_r2309397083
##########
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:
Why is this useful? We don’t currently expose these URLs in the UI. Maybe in
the next iteration we should reintroduce the slug + short id URL style (similar
to Medium), for example:
- dashboard/unicode-test-91c65d59d37/
- chart/unicode-cloud-b86ba33fc5cd/
That said, this looks like a complex and potentially breaking change. It
could be useful for dashboards, especially as a replacement for custom slugs.
But charts and datasets don’t follow the same pattern — they just redirect you
to the `/explore` endpoint.
I’d prefer to discuss this in a different setting, since it will require
some brainstorming.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]