def handle(self, domain, case_type, **options): domains = {domain} if options["and_linked"]: domains = domains | { link.linked_domain for link in get_linked_domains(domain) } if options["username"]: user_id = username_to_user_id(options["username"]) if not user_id: raise Exception("The username you entered is invalid") else: user_id = SYSTEM_USER_ID self.output_file = options["output_file"] options.pop("and_linked") options.pop("username") options.pop("output_file", None) self.extra_options = options for domain in sorted(domains): print(f"Processing {domain}") self.update_cases(domain, case_type, user_id)
def update_cases(self, domain, case_type, user_id): case_ids = self.find_case_ids_by_type(domain, case_type) accessor = CaseAccessors(domain) case_blocks = [] skip_count = 0 for case in accessor.iter_cases(case_ids): username_of_associated_mobile_workers = case.get_case_property( 'username') try: normalized_username = normalize_username( username_of_associated_mobile_workers, domain) except ValidationError: skip_count += 1 continue user_id_of_mobile_worker = username_to_user_id(normalized_username) if user_id_of_mobile_worker: case_blocks.append( self.case_block(case, user_id_of_mobile_worker)) else: skip_count += 1 print( f"{len(case_blocks)} to update in {domain}, {skip_count} cases have skipped due to unknown username." ) total = 0 for chunk in chunked(case_blocks, BATCH_SIZE): submit_case_blocks(chunk, domain, device_id=DEVICE_ID, user_id=user_id) total += len(chunk) print("Updated {} cases on domain {}".format(total, domain))
def for_script(cls, name, username=None): user_kwargs = {} if username: user_id = username_to_user_id(username) if not user_id: raise Exception(f"User '{username}' not found") user_kwargs = { 'user_id': user_id, 'username': username, } return cls( device_id=name, **user_kwargs, )
def update_cases(domain, case_type, username): accessor = CaseAccessors(domain) case_ids = accessor.get_case_ids_in_domain(case_type) print(f"Found {len(case_ids)} {case_type} cases in {domain}") user_id = username_to_user_id(username) if not user_id: user_id = SYSTEM_USER_ID case_blocks = [] skip_count = 0 for case in accessor.iter_cases(case_ids): if should_skip(case): skip_count += 1 elif needs_update(case): case_blocks.append(case_block(case))
def handle(self, domain, case_type, **options): domains = {domain} if options["and_linked"]: domains = domains | { link.linked_domain for link in get_linked_domains(domain) } if options["username"]: user_id = username_to_user_id(options["username"]) if not user_id: raise Exception("The username you entered is invalid") else: user_id = SYSTEM_USER_ID self.location = options.get('location') for domain in domains: print(f"Processing {domain}") self.update_cases(domain, case_type, user_id)
def process_view(self, request, view_func, view_args, view_kwargs): request.analytics_enabled = True if 'domain' in view_kwargs: request.domain = view_kwargs['domain'] if 'org' in view_kwargs: request.org = view_kwargs['org'] if request.user and request.user.is_authenticated: user_id = username_to_user_id(request.user.username) couch_user = CouchUser.get_by_user_id(user_id) if not couch_user: couch_user = InvalidUser() request.couch_user = couch_user if not request.couch_user.analytics_enabled: request.analytics_enabled = False if 'domain' in view_kwargs: domain = request.domain request.couch_user.current_domain = domain elif is_public_reports(view_kwargs, request): request.couch_user = AnonymousCouchUser() return None
def case_blocks(self, case): username_of_associated_mobile_workers = case.get_case_property( 'username') try: normalized_username = normalize_username( username_of_associated_mobile_workers, case.domain) except ValidationError: self.logger.error( "ValidationError: invalid username:{} associated with " "case:{}".format(case.get_case_property('username'), case.case_id)) return None user_id_of_mobile_worker = username_to_user_id(normalized_username) if user_id_of_mobile_worker: return [ CaseBlock( create=False, case_id=case.case_id, update={'hq_user_id': user_id_of_mobile_worker}, ) ]
def process_view(self, request, view_func, view_args, view_kwargs): request.analytics_enabled = True if 'domain' in view_kwargs: request.domain = view_kwargs['domain'] if 'org' in view_kwargs: request.org = view_kwargs['org'] if request.user.is_anonymous and 'domain' in view_kwargs: if ANONYMOUS_WEB_APPS_USAGE.enabled(view_kwargs['domain']): request.couch_user = CouchUser.get_anonymous_mobile_worker(request.domain) if request.user and request.user.is_authenticated: user_id = username_to_user_id(request.user.username) request.couch_user = CouchUser.get_by_user_id(user_id) if not request.couch_user.analytics_enabled: request.analytics_enabled = False if 'domain' in view_kwargs: domain = request.domain if not request.couch_user: request.couch_user = InvalidUser() if request.couch_user: request.couch_user.current_domain = domain elif is_public_reports(view_kwargs, request): request.couch_user = AnonymousCouchUser() return None
def test_username_to_user_id_wrong_username(self): user_id = username_to_user_id('not-here') self.assertIsNone(user_id)
def test_username_to_user_id(self): user_id = username_to_user_id(self.user.username) self.assertEqual(user_id, self.user._id)