'Assigns a list of found users to the given role.'

@api
@has_access_api
@expose(u'/update_role/', methods=[u'POST'])
def update_role(self):

    data = request.get_json(force=True)
    gamma_role = sm.find_role(u'Gamma')
    username_set = set()
    user_data_dict = {}
    for user_data in data[u'users']:
        username = user_data[u'username']
        if (not username):
            continue
        user_data_dict[username] = user_data
        username_set.add(username)
    existing_users = db.session.query(sm.user_model).filter(sm.user_model.username.in_(username_set)).all()
    missing_users = username_set.difference(set([u.username for u in existing_users]))
    logging.info(u'Missing    users:    {}'.format(missing_users))
    created_users = []
    for username in missing_users:
        user_data = user_data_dict[username]
        user = sm.find_user(email=user_data[u'email'])
        if (not user):
            logging.info(u'Adding    user:    {}.'.format(user_data))
            sm.add_user(username=user_data[u'username'], first_name=user_data[u'first_name'], last_name=user_data[u'last_name'], email=user_data[u'email'], role=gamma_role)
            sm.get_session.commit()
            user = sm.find_user(username=user_data[u'username'])
        existing_users.append(user)
        created_users.append(user.username)
    role_name = data[u'role_name']
    role = sm.find_role(role_name)
    role.user = existing_users
    sm.get_session.commit()
    return self.json_response({u'role': role_name, u'#    missing    users': len(missing_users), u'#    granted': len(existing_users), u'created_users': created_users}, status=201)
