From ba30ba380d4df23da92ed47a29ef83cc8370b8ae Mon Sep 17 00:00:00 2001 From: Sergey Golitsynskiy Date: Fri, 8 Oct 2021 20:39:59 -0400 Subject: [PATCH] Map UserRoleAssociation declaratively --- lib/tool_shed/webapp/model/__init__.py | 13 ++++++++++++- lib/tool_shed/webapp/model/mapping.py | 16 ++-------------- test/unit/shed_unit/model/test_mapping.py | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/tool_shed/webapp/model/__init__.py b/lib/tool_shed/webapp/model/__init__.py index af6aa8e39536..a82cd6ab16dd 100644 --- a/lib/tool_shed/webapp/model/__init__.py +++ b/lib/tool_shed/webapp/model/__init__.py @@ -108,6 +108,7 @@ class User(Base, Dictifiable, _HasTable): dict_collection_visible_keys = ['id', 'username'] dict_element_visible_keys = ['id', 'username'] bootstrap_admin_user = False + roles = relationship('UserRoleAssociation', back_populates='user') non_private_roles = relationship( 'UserRoleAssociation', viewonly=True, @@ -249,7 +250,17 @@ def __init__(self, user, group): self.group = group -class UserRoleAssociation(_HasTable): +class UserRoleAssociation(Base, _HasTable): + __tablename__ = 'user_role_association' + + id = Column(Integer, primary_key=True) + user_id = Column(Integer, ForeignKey('galaxy_user.id'), index=True) + role_id = Column(Integer, ForeignKey('role.id'), index=True) + create_time = Column(DateTime, default=now) + update_time = Column(DateTime, default=now, onupdate=now) + user = relationship('User', back_populates='roles') + role = relationship('Role', back_populates='users') + def __init__(self, user, role): self.user = user self.role = role diff --git a/lib/tool_shed/webapp/model/mapping.py b/lib/tool_shed/webapp/model/mapping.py index 643c74b2751b..ff32d59d8f0f 100644 --- a/lib/tool_shed/webapp/model/mapping.py +++ b/lib/tool_shed/webapp/model/mapping.py @@ -17,21 +17,14 @@ from tool_shed.webapp.model import mapper_registry from tool_shed.webapp.model import Repository, RepositoryCategoryAssociation from tool_shed.webapp.model import RepositoryMetadata, RepositoryRatingAssociation -from tool_shed.webapp.model import RepositoryReview, RepositoryRoleAssociation, Role -from tool_shed.webapp.model import User, UserRoleAssociation +from tool_shed.webapp.model import RepositoryReview, RepositoryRoleAssociation +from tool_shed.webapp.model import User from tool_shed.webapp.security import CommunityRBACAgent log = logging.getLogger(__name__) metadata = mapper_registry.metadata -UserRoleAssociation.table = Table("user_role_association", metadata, - Column("id", Integer, primary_key=True), - Column("user_id", Integer, ForeignKey("galaxy_user.id"), index=True), - Column("role_id", Integer, ForeignKey("role.id"), index=True), - Column("create_time", DateTime, default=now), - Column("update_time", DateTime, default=now, onupdate=now)) - Repository.table = Table("repository", metadata, Column("id", Integer, primary_key=True), Column("create_time", DateTime, default=now), @@ -78,11 +71,6 @@ Column("rating", Integer, index=True), Column("deleted", Boolean, index=True, default=False)) -mapper_registry.map_imperatively(UserRoleAssociation, UserRoleAssociation.table, - properties=dict( - user=relation(User, backref="roles"), - role=relation(Role, back_populates='users'))) - mapper_registry.map_imperatively(Repository, Repository.table, properties=dict( categories=relation(RepositoryCategoryAssociation, back_populates='repository'), diff --git a/test/unit/shed_unit/model/test_mapping.py b/test/unit/shed_unit/model/test_mapping.py index 0a2e78444c17..9c920c6cc3ec 100644 --- a/test/unit/shed_unit/model/test_mapping.py +++ b/test/unit/shed_unit/model/test_mapping.py @@ -816,7 +816,7 @@ def test_relationships(self, session, cls_, user, group): class TestUserRoleAssociation(BaseTest): def test_table(self, cls_): - assert cls_.table.name == 'user_role_association' + assert cls_.__tablename__ == 'user_role_association' def test_columns(self, session, cls_, user, role): create_time = datetime.now()