def get_custom_data_models(domain, limit_types=None): fields = defaultdict(dict) for field_view in [LocationFieldsView, ProductFieldsView, UserFieldsView]: if limit_types and field_view.field_type not in limit_types: continue model = CustomDataFieldsDefinition.get(domain, field_view.field_type) if model: fields[field_view.field_type]['fields'] = [{ 'slug': field.slug, 'is_required': field.is_required, 'label': field.label, 'choices': field.choices, 'regex': field.regex, 'regex_msg': field.regex_msg, } for field in model.get_fields()] if field_view.show_profiles: fields[field_view.field_type]['profiles'] = [ profile.to_json() for profile in model.get_profiles() ] return fields
def get_location_data_fields(domain): from corehq.apps.locations.views import LocationFieldsView fields_definition = CustomDataFieldsDefinition.get(domain, LocationFieldsView.field_type) if fields_definition: return fields_definition.get_fields() else: return []
def test_migration(self): doc = CustomDataFieldsDefinition( domain='botswana', field_type='UserFields', fields=[ CustomDataField( slug='color', is_required=True, label='Color', choices=['red', 'orange', 'yellow'], ), ], ) doc.save(sync_to_sql=False) call_command('populate_custom_data_fields') self.assertIsNone( Command.diff_couch_and_sql( doc.to_json(), SQLCustomDataFieldsDefinition.objects.get( couch_id=doc.get_id))) # Additional call should apply any updates doc = CustomDataFieldsDefinition.get(doc.get_id) doc.field_type = 'LocationFields' doc.save(sync_to_sql=False) call_command('populate_custom_data_fields') self.assertIsNone( Command.diff_couch_and_sql( doc.to_json(), SQLCustomDataFieldsDefinition.objects.get( couch_id=doc.get_id)))
def remove_unused_custom_fields_from_users(domain): """ Removes all unused custom data fields from all users in the domain """ from corehq.apps.users.views.mobile.custom_data_fields import CUSTOM_USER_DATA_FIELD_TYPE fields_definition = CustomDataFieldsDefinition.get( domain, CUSTOM_USER_DATA_FIELD_TYPE) assert fields_definition, 'remove_unused_custom_fields_from_users called without a valid definition' configured_field_keys = set( [f.slug for f in fields_definition.get_fields()]) for user in get_all_commcare_users_by_domain(domain): keys_to_delete = _get_invalid_user_data_fields(user, configured_field_keys) if keys_to_delete: for key in keys_to_delete: user.pop_metadata(key) user.save()
def _get_domain_info(domain): domain_info = domain_info_by_domain.get(domain) if domain_info: return domain_info if domain == upload_domain: domain_group_memoizer = group_memoizer or GroupMemoizer(domain) else: domain_group_memoizer = GroupMemoizer(domain) domain_group_memoizer.load_all() can_assign_locations = domain_has_privilege(domain, privileges.LOCATIONS) location_cache = None if can_assign_locations: location_cache = SiteCodeToLocationCache(domain) domain_obj = Domain.get_by_name(domain) allowed_group_names = [ group.name for group in domain_group_memoizer.groups ] roles_by_name = { role.name: role for role in UserRole.by_domain(domain) } profiles_by_name = {} definition = CustomDataFieldsDefinition.get(domain, UserFieldsView.field_type) if definition: profiles_by_name = { profile.name: profile for profile in definition.get_profiles() } domain_user_specs = [ spec for spec in user_specs if spec.get('domain', upload_domain) == domain ] validators = get_user_import_validators(domain_obj, domain_user_specs, allowed_group_names, list(roles_by_name), list(profiles_by_name), upload_domain) domain_info = DomainInfo(validators, can_assign_locations, location_cache, roles_by_name, profiles_by_name, domain_group_memoizer) domain_info_by_domain[domain] = domain_info return domain_info
def get_custom_data_models(domain, limit_types=None): fields = {} for field_view in [LocationFieldsView, ProductFieldsView, UserFieldsView]: if limit_types and field_view.field_type not in limit_types: continue model = CustomDataFieldsDefinition.get(domain, field_view.field_type) if model: fields[field_view.field_type] = [ { 'slug': field.slug, 'is_required': field.is_required, 'label': field.label, 'choices': field.choices, 'regex': field.regex, 'regex_msg': field.regex_msg, } for field in model.get_fields() ] return fields
def product_fixture_generator_json(domain): if not SQLProduct.objects.filter(domain=domain).exists(): return None fields = [x for x in PRODUCT_FIELDS if x != CUSTOM_DATA_SLUG] fields.append('@id') custom_fields = CustomDataFieldsDefinition.get(domain, 'ProductFields') if custom_fields: for f in custom_fields.get_fields(): fields.append(CUSTOM_DATA_SLUG + '/' + f.slug) uri = 'jr://fixture/{}'.format(ProductFixturesProvider.id) return { 'id': 'products', 'uri': uri, 'path': '/products/product', 'name': 'Products', 'structure': {f: {'name': f, 'no_option': True} for f in fields}, }
def get_domain_info(domain, upload_domain, user_specs, domain_info_by_domain, upload_user=None, group_memoizer=None, is_web_upload=False): from corehq.apps.users.views.mobile.custom_data_fields import UserFieldsView domain_info = domain_info_by_domain.get(domain) if domain_info: return domain_info if domain == upload_domain: domain_group_memoizer = group_memoizer or GroupMemoizer(domain) else: domain_group_memoizer = GroupMemoizer(domain) domain_group_memoizer.load_all() can_assign_locations = domain_has_privilege(domain, privileges.LOCATIONS) location_cache = None if can_assign_locations: location_cache = SiteCodeToLocationCache(domain) domain_obj = Domain.get_by_name(domain) allowed_group_names = [ group.name for group in domain_group_memoizer.groups ] profiles_by_name = {} domain_user_specs = [ spec for spec in user_specs if spec.get('domain', upload_domain) == domain ] if is_web_upload: roles_by_name = { role[1]: role[0] for role in get_editable_role_choices( domain, upload_user, allow_admin_role=True) } validators = get_user_import_validators( Domain.get_by_name(domain), domain_user_specs, True, allowed_roles=list(roles_by_name), upload_domain=upload_domain, ) else: roles_by_name = { role.name: role for role in UserRole.by_domain(domain) } definition = CustomDataFieldsDefinition.get(domain, UserFieldsView.field_type) if definition: profiles_by_name = { profile.name: profile for profile in definition.get_profiles() } validators = get_user_import_validators(domain_obj, domain_user_specs, False, allowed_group_names, list(roles_by_name), list(profiles_by_name), upload_domain) domain_info = DomainInfo(validators, can_assign_locations, location_cache, roles_by_name, profiles_by_name, domain_group_memoizer) domain_info_by_domain[domain] = domain_info return domain_info