예제 #1
0
def parse_users(group_memoizer, domain, user_data_model, location_cache):

    def _get_group_names(user):
        return sorted(map(
            lambda id: group_memoizer.get(id).name,
            Group.by_user(user, wrap=False)
        ), key=alphanumeric_sort_key)

    def _make_user_dict(user, group_names, location_cache):
        model_data, uncategorized_data = (
            user_data_model.get_model_and_uncategorized(user.user_data)
        )
        return {
            'data': model_data,
            'uncategorized_data': uncategorized_data,
            'group': group_names,
            'name': user.full_name,
            'password': "******",  # dummy display string for passwords
            'phone-number': user.phone_number,
            'email': user.email,
            'username': user.raw_username,
            'language': user.language,
            'user_id': user._id,
            'is_active': str(user.is_active),
            'location_code': location_cache.get(user.location_id),
        }

    unrecognized_user_data_keys = set()
    user_groups_length = 0
    user_dicts = []
    for user in get_all_commcare_users_by_domain(domain):
        group_names = _get_group_names(user)
        user_dict = _make_user_dict(user, group_names, location_cache)
        user_dicts.append(user_dict)
        unrecognized_user_data_keys.update(user_dict['uncategorized_data'].keys())
        user_groups_length = max(user_groups_length, len(group_names))

    user_headers = [
        'username', 'password', 'name', 'phone-number', 'email',
        'language', 'user_id', 'is_active',
    ]
    if domain_has_privilege(domain, privileges.LOCATIONS):
        user_headers.append('location_code')
    user_data_fields = [f.slug for f in user_data_model.get_fields(include_system=False)]
    user_headers.extend(build_data_headers(user_data_fields))
    user_headers.extend(build_data_headers(
        unrecognized_user_data_keys,
        header_prefix='uncategorized_data'
    ))
    user_headers.extend(json_to_headers(
        {'group': range(1, user_groups_length + 1)}
    ))

    def _user_rows():
        for user_dict in user_dicts:
            row = dict(flatten_json(user_dict))
            yield [row.get(header) or '' for header in user_headers]
    return user_headers, _user_rows()
예제 #2
0
def build_data_headers(keys, header_prefix='data'):
    return json_to_headers(
        {header_prefix: {key: None for key in keys}}
    )
예제 #3
0
def parse_users(group_memoizer, domain, user_data_model, location_cache):
    def _get_group_names(user):
        return sorted(map(lambda id: group_memoizer.get(id).name,
                          Group.by_user(user, wrap=False)),
                      key=alphanumeric_sort_key)

    def _make_user_dict(user, group_names, location_cache):
        model_data, uncategorized_data = (
            user_data_model.get_model_and_uncategorized(user.user_data))
        role = user.get_role(domain)
        return {
            'data': model_data,
            'uncategorized_data': uncategorized_data,
            'group': group_names,
            'name': user.full_name,
            'password': "******",  # dummy display string for passwords
            'phone-number': user.phone_number,
            'email': user.email,
            'username': user.raw_username,
            'language': user.language,
            'user_id': user._id,
            'is_active': str(user.is_active),
            'location_code': location_cache.get(user.location_id),
            'role': role.name if role else '',
        }

    unrecognized_user_data_keys = set()
    user_groups_length = 0
    user_dicts = []
    for user in get_all_commcare_users_by_domain(domain):
        group_names = _get_group_names(user)
        user_dict = _make_user_dict(user, group_names, location_cache)
        user_dicts.append(user_dict)
        unrecognized_user_data_keys.update(
            user_dict['uncategorized_data'].keys())
        user_groups_length = max(user_groups_length, len(group_names))

    user_headers = [
        'username',
        'password',
        'name',
        'phone-number',
        'email',
        'language',
        'role',
        'user_id',
        'is_active',
    ]
    if domain_has_privilege(domain, privileges.LOCATIONS):
        user_headers.append('location_code')
    user_data_fields = [
        f.slug for f in user_data_model.get_fields(include_system=False)
    ]
    user_headers.extend(build_data_headers(user_data_fields))
    user_headers.extend(
        build_data_headers(unrecognized_user_data_keys,
                           header_prefix='uncategorized_data'))
    user_headers.extend(
        json_to_headers({'group': range(1, user_groups_length + 1)}))

    def _user_rows():
        for user_dict in user_dicts:
            row = dict(flatten_json(user_dict))
            yield [row.get(header) or '' for header in user_headers]

    return user_headers, _user_rows()
예제 #4
0
def build_data_headers(keys, header_prefix='data'):
    return json_to_headers({header_prefix: {key: None for key in keys}})
예제 #5
0
 def _extend_headers(prefix, headers):
     tab_headers.extend(
         json_to_headers({prefix: {header: None
                                   for header in headers}}))
예제 #6
0
파일: util.py 프로젝트: nnestle/commcare-hq
 def _extend_headers(prefix, headers):
     tab_headers.extend(json_to_headers(
         {prefix: {header: None for header in headers}}
     ))