Add ability to merge 2 user accounts

This commit is contained in:
Lance Edgar 2017-02-17 12:49:15 -06:00
parent 35126e8e5b
commit 93f40ef36e
2 changed files with 43 additions and 1 deletions

View file

@ -482,7 +482,11 @@ class MasterView(View):
return result
def merge_objects(self, removing, keeping):
raise NotImplementedError("please implement `{}.merge_objects()`".format(self.__class__.__name__))
"""
Merge the two given objects. You should probably override this;
default behavior is merely to delete the 'removing' object.
"""
self.Session.delete(removing)
##############################
# Core Stuff

View file

@ -129,6 +129,19 @@ class UsersView(PrincipalMasterView):
Master view for the User model.
"""
model_class = model.User
mergeable = True
merge_additive_fields = [
'sent_message_count',
'received_message_count',
]
merge_fields = merge_additive_fields + [
'uuid',
'username',
'person_uuid',
'person_name',
'role_count',
'active',
]
def query(self, session):
return session.query(model.User)\
@ -202,6 +215,31 @@ class UsersView(PrincipalMasterView):
users.append(user)
return users
def get_merge_data(self, user):
return {
'uuid': user.uuid,
'username': user.username,
'person_uuid': user.person_uuid,
'person_name': user.person.display_name if user.person else None,
'_roles': user.roles,
'role_count': len(user.roles),
'active': user.active,
'sent_message_count': len(user.sent_messages),
'received_message_count': len(user._messages),
}
def get_merge_resulting_data(self, remove, keep):
result = super(UsersView, self).get_merge_resulting_data(remove, keep)
result['role_count'] = len(set(remove['_roles'] + keep['_roles']))
return result
def merge_objects(self, removing, keeping):
# TODO: merge roles, messages
assert not removing.sent_messages
assert not removing._messages
assert not removing._roles
self.Session.delete(removing)
class UserVersionView(VersionView):
"""