def test_delete(self): user_to_delete = CommCareUser.create( self.domain, f'delete@{self.domain}.commcarehq.org', '******', created_by=None, created_via=None) user_to_delete_id = user_to_delete.get_id deleted_username = user_id_to_username(user_to_delete_id) user_to_delete.delete(self.domain, deleted_by=self.web_user, deleted_via=USER_CHANGE_VIA_WEB) user_history = UserHistory.objects.get(by_domain=self.domain, for_domain=self.domain, user_id=user_to_delete_id) self.assertEqual(user_history.by_domain, self.domain) self.assertEqual(user_history.for_domain, self.domain) self.assertEqual(user_history.user_type, "CommCareUser") self.assertEqual(user_history.changed_by, self.web_user.get_id) self.assertEqual(user_history.changes, _get_expected_changes_json(user_to_delete)) self.assertEqual(user_history.changed_via, USER_CHANGE_VIA_WEB) self.assertEqual(user_history.change_messages, {}) self.assertEqual(user_history.action, UserModelAction.DELETE.value) self.assertEqual(user_history.user_repr, deleted_username) self.assertEqual(user_history.changed_by_repr, user_id_to_username(self.web_user.get_id))
def test_user_id_to_username_with_names(self): self.assertEqual('no_name', user_id_to_username(self.user_without_name.user_id, use_name_if_available=True)) self.assertEqual('Alice', user_id_to_username(self.user_with_first_name.user_id, use_name_if_available=True)) self.assertEqual('Jones', user_id_to_username(self.user_with_last_name.user_id, use_name_if_available=True)) self.assertEqual('Alice Jones', user_id_to_username(self.user_with_full_name.user_id, use_name_if_available=True))
def open_cases_json(request, domain): delete_ids = json.loads(request.GET.get('delete_ids', 'false')) cases = CommCareCase.get_all_cases(domain, status='open', include_docs=True) user_id_to_type_to_cases = defaultdict(lambda:defaultdict(list)) for case in cases: case_json = deepcopy(case.to_json()) user_id_to_type_to_cases[case.user_id][case.type].append(case_json) del case_json['domain'] if delete_ids: del case_json['_id'] del case_json['_rev'] del case_json['user_id'] # del case_json['type'] del case_json['doc_type'] case_json['actions'] = [action.action_type for action in case.actions] case_json['referrals'] = [referral.type for referral in case.referrals] usercases = [{ "username": user_id_to_username(user_id), "cases": [{"type": type, "cases":cases} for (type, cases) in type_to_cases.items()] } for (user_id, type_to_cases) in user_id_to_type_to_cases.items()] usercases.sort(key=lambda x: x['username']) return HttpResponse(json.dumps(usercases))
def open_cases_json(request, domain): delete_ids = json.loads(request.GET.get("delete_ids", "false")) cases = CommCareCase.view( "hqcase/open_cases", startkey=[domain], endkey=[domain, {}], reduce=False, include_docs=True ) user_id_to_type_to_cases = defaultdict(lambda: defaultdict(list)) for case in cases: case_json = deepcopy(case.to_json()) user_id_to_type_to_cases[case.user_id][case.type].append(case_json) del case_json["domain"] if delete_ids: del case_json["_id"] del case_json["_rev"] del case_json["user_id"] # del case_json['type'] del case_json["doc_type"] case_json["actions"] = [action.action_type for action in case.actions] case_json["referrals"] = [referral.type for referral in case.referrals] usercases = [ { "username": user_id_to_username(user_id), "cases": [{"type": type, "cases": cases} for (type, cases) in type_to_cases.items()], } for (user_id, type_to_cases) in user_id_to_type_to_cases.items() ] usercases.sort(key=lambda x: x["username"]) return HttpResponse(json.dumps(usercases))
def open_cases_json(request, domain): delete_ids = json.loads(request.GET.get('delete_ids', 'false')) cases = CommCareCase.view('hqcase/open_cases', startkey=[domain], endkey=[domain, {}], reduce=False, include_docs=True) user_id_to_type_to_cases = defaultdict(lambda:defaultdict(list)) for case in cases: case_json = deepcopy(case.to_json()) user_id_to_type_to_cases[case.user_id][case.type].append(case_json) del case_json['domain'] if delete_ids: del case_json['_id'] del case_json['_rev'] del case_json['user_id'] # del case_json['type'] del case_json['doc_type'] case_json['actions'] = [action.action_type for action in case.actions] case_json['referrals'] = [referral.type for referral in case.referrals] usercases = [{ "username": user_id_to_username(user_id), "cases": [{"type": type, "cases":cases} for (type, cases) in type_to_cases.items()] } for (user_id, type_to_cases) in user_id_to_type_to_cases.items()] usercases.sort(key=lambda x: x['username']) return HttpResponse(json.dumps(usercases))
def test_create(self): user_history = UserHistory.objects.get( user_id=self.commcare_user.get_id, action=UserModelAction.CREATE.value) self.assertEqual(user_history.by_domain, self.domain) self.assertEqual(user_history.for_domain, self.domain) self.assertEqual(user_history.user_type, "CommCareUser") self.assertEqual(user_history.user_id, self.commcare_user.get_id) self.assertIsNotNone(user_history.changed_by) self.assertEqual(user_history.changed_by, self.web_user.get_id) self.assertEqual(user_history.changes, _get_expected_changes_json(self.commcare_user)) self.assertEqual(user_history.changed_via, USER_CHANGE_VIA_WEB) self.assertEqual(user_history.change_messages, {}) self.assertEqual(user_history.action, UserModelAction.CREATE.value) self.assertEqual(user_history.user_repr, user_id_to_username(self.commcare_user.get_id)) self.assertEqual(user_history.changed_by_repr, user_id_to_username(self.web_user.get_id))
def _household_verification_json( domain="dodoma", last_hvid_path=["household_verification"], next_hvid_path=["followup_id"], xmlns='http://openrosa.org/formdesigner/9DAACA82-A414-499A-9C40-BC43775CEE79', range=None): if range: start, end = map(string_to_datetime, range) else: now = datetime.utcnow() start, end = now - timedelta(days=7), now key = make_form_couch_key(domain, xmlns=xmlns) submissions = XFormInstance.view( 'reports_forms/all_forms', reduce=False, startkey=key + [json_format_datetime(start)], endkey=key + [json_format_datetime(end)], include_docs=True, ) stats = get_household_verification_data( submissions=submissions, next_hvid_path=next_hvid_path, last_hvid_path=last_hvid_path, ) stats_by_userID = {} for s in stats: stats_by_userID[s['userID']] = s s['username'] = "******" % s['userID'] users = CommCareUser.by_domain(domain) for user in users: userID = user.user_id username = user_id_to_username(userID) if userID in stats_by_userID: stats_by_userID[userID]['username'] = username else: stats.append({ 'userID': userID, 'username': username, 'total': 0, 'correct': 0 }) stats.sort(key=lambda s: s['username']) return { "headers": ["Username", "Correct", "Total", "Percent Correct"], "rows": [[ s['username'], s['correct'], s['total'], ("%s%%" % int(s['correct'] * 100 / s['total']) if s['total'] else "---") ] for s in stats], }
def test_cached_user_id_to_user_display(self): self.assertEqual('Alice', cached_user_id_to_user_display(self.user_with_first_name.user_id)) self.assertEqual('Alice Jones', cached_user_id_to_user_display(self.user_with_full_name.user_id)) self.user_with_first_name.first_name = 'Betty' self.user_with_first_name.save() self.assertEqual('Betty', user_id_to_username(self.user_with_first_name.user_id, use_name_if_available=True)) self.assertEqual('Alice', cached_user_id_to_user_display(self.user_with_first_name.user_id)) self.assertEqual('Alice Jones', cached_user_id_to_user_display(self.user_with_full_name.user_id)) # set username back because other tests rely on it self.user_with_first_name.first_name = 'Alice' self.user_with_first_name.save()
def _household_verification_json( domain="dodoma", last_hvid_path=["household_verification"], next_hvid_path=["followup_id"], xmlns='http://openrosa.org/formdesigner/9DAACA82-A414-499A-9C40-BC43775CEE79', range=None ): if range: start, end = map(string_to_datetime, range) else: now = datetime.utcnow() start, end = now - timedelta(days=7), now key = make_form_couch_key(domain, xmlns=xmlns) submissions = XFormInstance.view('reports_forms/all_forms', reduce=False, startkey=key+[json_format_datetime(start)], endkey=key+[json_format_datetime(end)], include_docs=True, ) stats = get_household_verification_data( submissions=submissions, next_hvid_path=next_hvid_path, last_hvid_path=last_hvid_path, ) stats_by_userID = {} for s in stats: stats_by_userID[s['userID']] = s s['username'] = "******" % s['userID'] users = CommCareUser.by_domain(domain) for user in users: userID = user.user_id username = user_id_to_username(userID) if userID in stats_by_userID: stats_by_userID[userID]['username'] = username else: stats.append({'userID': userID, 'username': username, 'total': 0, 'correct': 0}) stats.sort(key=lambda s: s['username']) return { "headers": ["Username", "Correct", "Total", "Percent Correct"], "rows": [[ s['username'], s['correct'], s['total'], ("%s%%" % int(s['correct']*100/s['total']) if s['total'] else "---") ] for s in stats], }
def test_update(self): restore_phone_numbers_to = self.commcare_user.to_json( )['phone_numbers'] self.commcare_user.add_phone_number("9999999999") change_messages = UserChangeMessage.phone_numbers_added(["9999999999"]) user_history = log_user_change( self.domain, self.domain, self.commcare_user, self.web_user, changed_via=USER_CHANGE_VIA_BULK_IMPORTER, change_messages=change_messages, fields_changed={ 'phone_numbers': self.commcare_user.phone_numbers, 'password': '******' }, action=UserModelAction.UPDATE) self.assertEqual(user_history.by_domain, self.domain) self.assertEqual(user_history.for_domain, self.domain) self.assertEqual(user_history.user_type, "CommCareUser") self.assertIsNotNone(user_history.user_id) self.assertEqual(user_history.user_id, self.commcare_user.get_id) self.assertIsNotNone(user_history.changed_by) self.assertEqual(user_history.changed_by, self.web_user.get_id) self.assertEqual(user_history.changes, {'phone_numbers': ['9999999999']}) self.assertEqual(user_history.changed_via, USER_CHANGE_VIA_BULK_IMPORTER) self.assertEqual(user_history.change_messages, change_messages) self.assertEqual(user_history.action, UserModelAction.UPDATE.value) self.assertEqual(user_history.user_repr, user_id_to_username(self.commcare_user.get_id)) self.assertEqual(user_history.changed_by_repr, user_id_to_username(self.web_user.get_id)) self.commcare_user.phone_numbers = restore_phone_numbers_to
def get_username_from_forms(domain, user_id): key = make_form_couch_key(domain, user_id=user_id) user_info = get_db().view('reports_forms/all_forms', startkey=key, limit=1, reduce=False).one() username = HQUserType.human_readable[HQUserType.ADMIN] try: possible_username = user_info['value']['username'] if not possible_username == 'none': username = possible_username return username except KeyError: possible_username = user_id_to_username(user_id) if possible_username: username = possible_username return username
def get_username_from_forms(domain, user_id): user_info = get_db().view( 'reports/submit_history', startkey=[domain, user_id], limit=1, reduce=False ).one() username = HQUserType.human_readable[HQUserType.ADMIN] try: possible_username = user_info['value']['username'] if not possible_username == 'none': username = possible_username return username except KeyError: possible_username = user_id_to_username(user_id) if possible_username: username = possible_username return username
def get_username_from_forms(domain, user_id): key = make_form_couch_key(domain, user_id=user_id) user_info = get_db().view( 'reports_forms/all_forms', startkey=key, limit=1, reduce=False ).one() username = HQUserType.human_readable[HQUserType.ADMIN] try: possible_username = user_info['value']['username'] if not possible_username == 'none': username = possible_username return username except KeyError: possible_username = user_id_to_username(user_id) if possible_username: username = possible_username return username
def user_column(self, user_id): return DataTablesColumn(user_id_to_username(user_id))
def test_invalid_ids(self, invalid_id): self.assertEqual(None, get_wrapped_owner(invalid_id)) self.assertEqual(None, user_id_to_username(invalid_id))
def possible_usernames(): yield get_username_in_last_form_user_id_submitted(domain, user_id) yield user_id_to_username(user_id)
def _get_username(self): if self.userID: return user_id_to_username(self.userID) or "" return ""
def test_user_id_to_username_no_names(self): for user in self.users: self.assertEqual(user.username, user_id_to_username(user.user_id))