def test_get_commcare_users_by_filters(self): expected_users = [self.ccuser_2, self.ccuser_1] expected_usernames = [user.username for user in expected_users] actual_usernames = [ user.username for user in get_all_commcare_users_by_domain(self.ccdomain.name) ] self.assertItemsEqual(actual_usernames, expected_usernames)
def bulk_deactivate_users(domain): from corehq.apps.users.dbaccessors import get_all_commcare_users_by_domain users = get_all_commcare_users_by_domain(domain.name) for user in users: if not user.is_active: continue user.is_active = False user.save()
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 = get_by_domain_and_type(domain, CUSTOM_USER_DATA_FIELD_TYPE) assert fields_definition, 'remove_unused_custom_fields_from_users called without a valid CustomDataFieldsDefinition' 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: del user.user_data[key] user.save()
def _delete_demo_user_restores(domain_name): from corehq.apps.ota.models import DemoUserRestore from corehq.apps.users.dbaccessors import get_practice_mode_mobile_workers try: users = get_practice_mode_mobile_workers(domain_name) except ESError: # Fallback in case of ES Error users = get_all_commcare_users_by_domain(domain_name) for user in users: if user.demo_restore_id: try: DemoUserRestore.objects.get(id=user.demo_restore_id).delete() except DemoUserRestore.DoesNotExist: pass
def test_exclude_retired_users(self): deleted_user = CommCareUser.create( domain=self.ccdomain.name, username='******', password='******', created_by=None, created_via=None, email='*****@*****.**', ) deleted_user.retire(deleted_by=None) self.assertNotIn(deleted_user.username, [ user.username for user in get_all_commcare_users_by_domain(self.ccdomain.name) ]) deleted_user.delete(deleted_by=None)