

def unify_users(request):
    user = request.user
    conf = app_by_application_name('molly.auth')
    users = set()
    for identifier in user.useridentifier_set.all():
        if (not (identifier.namespace in conf.unify_identifiers)):
            continue
        identifiers = UserIdentifier.objects.filter(namespace=identifier.namespace, value=identifier.value)
        users |= set((i.user for i in identifiers))
    token_namespaces = set((t.namespace for t in user.externalservicetoken_set.all()))
    identifier_namespaces = set((i.namespace for i in user.useridentifier_set.all()))
    root_user = min(users, key=(lambda u: u.date_joined))
    users_without_root = users.copy()
    users_without_root = users_without_root.remove(root_user)
    unifying_users.send_robust(sender=request, users=users_without_root, into=root_user)
    for u in sorted(users, cmp=(lambda x, y: ((- 1) if (x == root_user) else (1 if (y == root_user) else 0)))):
        u.usersession_set.all().update(user=root_user)
        for token in u.externalservicetoken_set.all():
            if ((u != user) and (token.namespace in token_namespaces)):
                token.delete()
            else:
                token.user = root_user
                token.save()
        for identifier in u.useridentifier_set.all():
            if ((u != user) and (identifier.namespace in identifier_namespaces)):
                identifier.delete()
            else:
                identifier.user = root_user
                identifier.save()
        if (u != root_user):
            u.delete()

dataset/ETHPy150Open mollyproject/mollyproject/molly/auth/__init__.py unify_users/original