From 7c8fe41142f7798f827871c4fee43fd819c283a9 Mon Sep 17 00:00:00 2001 From: John Davis Date: Fri, 25 Oct 2024 17:05:01 -0400 Subject: [PATCH] Overwrite role name w/hybrid property --- lib/galaxy/model/__init__.py | 16 +++++++++++++++- lib/galaxy/model/db/user.py | 3 +++ test/unit/data/model/db/test_role.py | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/galaxy/model/__init__.py b/lib/galaxy/model/__init__.py index 85f3c9155687..4089ce8db13e 100644 --- a/lib/galaxy/model/__init__.py +++ b/lib/galaxy/model/__init__.py @@ -106,6 +106,7 @@ association_proxy, AssociationProxy, ) +from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.ext.orderinglist import ordering_list from sqlalchemy.orm import ( aliased, @@ -3747,7 +3748,7 @@ class Role(Base, Dictifiable, RepresentById): id: Mapped[int] = mapped_column(primary_key=True) create_time: Mapped[datetime] = mapped_column(default=now, nullable=True) update_time: Mapped[datetime] = mapped_column(default=now, onupdate=now, nullable=True) - name: Mapped[str] = mapped_column(String(255), index=True) + _name: Mapped[str] = mapped_column("name", String(255), index=True) description: Mapped[Optional[str]] = mapped_column(TEXT) type: Mapped[Optional[str]] = mapped_column(String(40), index=True) deleted: Mapped[Optional[bool]] = mapped_column(index=True, default=False) @@ -3766,6 +3767,19 @@ class types(str, Enum): ADMIN = "admin" SHARING = "sharing" + @hybrid_property + def name(self): + if self.type == Role.types.PRIVATE: + users = self.users + assert len(users) == 1, f"Did not find exactly one user for private role {self}" + return f"private role for {users[0].email}" + else: + return self._name + + @name.setter + def name(self, name): + self._name = name + @staticmethod def default_name(role_type): return f"{role_type.value} role" diff --git a/lib/galaxy/model/db/user.py b/lib/galaxy/model/db/user.py index 823247f725dd..d0e86c79025a 100644 --- a/lib/galaxy/model/db/user.py +++ b/lib/galaxy/model/db/user.py @@ -1,3 +1,4 @@ +import logging from typing import Optional from sqlalchemy import ( @@ -11,6 +12,8 @@ from galaxy.model import User from galaxy.model.scoped_session import galaxy_scoped_session +log = logging.getLogger(__name__) + def get_users_by_ids(session: galaxy_scoped_session, user_ids): stmt = select(User).where(User.id.in_(user_ids)) diff --git a/test/unit/data/model/db/test_role.py b/test/unit/data/model/db/test_role.py index 213314c5c609..af90ce4484d2 100644 --- a/test/unit/data/model/db/test_role.py +++ b/test/unit/data/model/db/test_role.py @@ -7,6 +7,23 @@ from . import have_same_elements +def test_role_name_property(session, make_role, make_user_and_role): + r1 = make_role(name="foo") + u, r2 = make_user_and_role(email="joe@bar.com") + + # Verify private role name references user + assert r1.name == "foo" + assert r2.name == "private role for joe@bar.com" + + # Update user's email + u.email = "updated@bar.com" + session.add(u) + session.commit() + + # Verify private role name references updated user's email + assert r2.name == "private role for updated@bar.com" + + def test_get_npns_roles(session, make_role): make_role(deleted=True) make_role(type=Role.types.PRIVATE)