Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Enables polymorphic subclasses on users
Browse files Browse the repository at this point in the history
  • Loading branch information
msom committed Aug 17, 2017
1 parent 283514e commit dc8fb66
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
- Adds user groups.
[msom]

- Enables polymorphic subclasses on users.
[msom]

0.14.1 (2017-07-12)
~~~~~~~~~~~~~~~~~~~

Expand Down
10 changes: 10 additions & 0 deletions onegov/user/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class User(Base, TimestampMixin, ORMSearchable):

__tablename__ = 'users'

#: the type of the item, this can be used to create custom polymorphic
#: subclasses of this class. See
#: `<http://docs.sqlalchemy.org/en/improve_toc/\
#: orm/extensions/declarative/inheritance.html>`_.
type = Column(Text, nullable=True)

__mapper_args__ = {
'polymorphic_on': type
}

es_language = 'de' # XXX add to database in the future
es_properties = {
'username': {'type': 'text'},
Expand Down
36 changes: 36 additions & 0 deletions onegov/user/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,39 @@ def test_user_group(session):
assert group.users.one() == user
assert user.group == group
assert user.group_id == group.id


def test_polymorphism_user(session):

class MyUser(User):
__mapper_args__ = {'polymorphic_identity': 'my'}

class MyOtherUser(User):
__mapper_args__ = {'polymorphic_identity': 'other'}

session.add(User(username='default', password='pwd', role='editor'))
session.add(MyUser(username='my', password='pwd', role='editor'))
session.add(MyOtherUser(username='other', password='pwd', role='editor'))
session.flush()

assert session.query(User).count() == 3
assert session.query(MyUser).one().username == 'my'
assert session.query(MyOtherUser).one().username == 'other'


def test_polymorphism_group(session):

class MyGroup(UserGroup):
__mapper_args__ = {'polymorphic_identity': 'my'}

class MyOtherGroup(UserGroup):
__mapper_args__ = {'polymorphic_identity': 'other'}

session.add(UserGroup(name='original'))
session.add(MyGroup(name='my'))
session.add(MyOtherGroup(name='other'))
session.flush()

assert session.query(UserGroup).count() == 3
assert session.query(MyGroup).one().name == 'my'
assert session.query(MyOtherGroup).one().name == 'other'
9 changes: 8 additions & 1 deletion onegov/user/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,11 @@ def add_group_id_column(context):
Column('group_id', UUID, nullable=True)
)

# add user.type

@upgrade_task('Add type column')
def add_type_column(context):
if not context.has_column('users', 'type'):
context.operations.add_column(
'users',
Column('type', Text, nullable=True)
)

0 comments on commit dc8fb66

Please sign in to comment.