示例#1
0
def add():
    opts = get_opts(
        'add_user', 'Adds User to a muam', {
            'username': '******',
            'email': 'email',
            'first_name': 'first_name',
            'full_name': 'full_name',
            'last_name': 'last_name',
            'phone': 'phone',
            'create_users': 'create_users',
            'create_groups': 'create_groups',
            'grant_privs': 'grant_privs',
        })

    session = get_session(opts)
    if not session:
        sys.exit('Invalid username/password.')

    add_item = User(**vars(opts))
    success, resp = add_item.save_new(session)

    if success:
        print('Added User with id: {}'.format(','.join(add_item.get_ids())))
    else:
        print('Add failed with code {} and message: {}'.format(
            resp.status_code, resp.text))
示例#2
0
def import_csv():
    opts = get_opts('import_user', 'Adds list of User to a muam',
                    {'f': 'filename'})

    if not opts.filename or not os.path.isfile(opts.filename):
        sys.exit('Filename {} does not exist or is not a file.'.format(
            opts.filename))

    session = get_session(opts)

    field_map = {}

    with open(opts.filename, "r") as csvfile:
        tagreader = csv.reader(csvfile)
        rowcount = 0
        for row in tagreader:
            rowcount += 1
            if rowcount == 1:
                fieldcount = 0
                for field in row:
                    field_map[field] = fieldcount
                    fieldcount += 1
                continue
            obj_data = {}
            for field in field_map:
                if field == 'authorizations':
                    obj_data['authorizations'] = []
                    auth_list = row[field_map[field]].split(', ')
                    for auth_item in auth_list:
                        auth_data = auth_item.split(':')
                        auth_gid = get_id_from_name(session, auth_data[0])
                        obj_data['authorizations'].append({
                            'group_id': auth_gid,
                            'role': auth_data[1]
                        })
                else:
                    lookup_res = get_id(session, field, row[field_map[field]])
                    obj_data[field] = lookup_res if lookup_res else row[
                        field_map[field]]
            new_obj = User(**obj_data)
            success, resp = new_obj.save_new(session)
            if success:
                print('row {} id: {}'.format(str(rowcount), new_obj.get_ids()))
            else:
                sys.exit('Add failed, code: {}, text: {}'.format(
                    resp.status_code, resp.text))