Skip to content

Commit

Permalink
Overwrite role name w/hybrid property
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavcs committed Oct 25, 2024
1 parent 0063dd5 commit 7c8fe41
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/galaxy/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions lib/galaxy/model/db/user.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import Optional

from sqlalchemy import (
Expand All @@ -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))
Expand Down
17 changes: 17 additions & 0 deletions test/unit/data/model/db/test_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]")

# Verify private role name references user
assert r1.name == "foo"
assert r2.name == "private role for [email protected]"

# Update user's email
u.email = "[email protected]"
session.add(u)
session.commit()

# Verify private role name references updated user's email
assert r2.name == "private role for [email protected]"


def test_get_npns_roles(session, make_role):
make_role(deleted=True)
make_role(type=Role.types.PRIVATE)
Expand Down

0 comments on commit 7c8fe41

Please sign in to comment.