Skip to content

Commit

Permalink
Map UserRoleAssociation declaratively
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavcs committed Oct 9, 2021
1 parent feaa362 commit 82a56b2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
13 changes: 12 additions & 1 deletion lib/tool_shed/webapp/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
16 changes: 2 additions & 14 deletions lib/tool_shed/webapp/model/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion test/unit/shed_unit/model/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 82a56b2

Please sign in to comment.