def handle(self, **options): if options['print_kafka_offsets']: start, end = self.get_min_max_offsets() print("\n\nKakfa topic offset range: {} - {}".format(start, end)) return start_offset = options['offset_start'] end_offset = options['offset_end'] start, end = self.get_min_max_offsets() if start_offset < start: start_offset = start if end_offset < 0 or end_offset > end: end_offset = end if start_offset > end_offset: raise CommandError("Start greater than end: {} > {}".format(start_offset, end_offset)) print('Using kafka offset range: {} - {}'.format(start_offset, end_offset)) if options['find_start_offset']: find_first_match = FindFirstMatch(start_offset, end_offset, check_user_at_offset) first_matching_offset = find_first_match.search() if first_matching_offset is None: raise CommandError("Unable to find first matching offset. " "Try a different search range.") else: print("\nFirst matching offset = {}".format(first_matching_offset)) return check = options['check'] seen_ids = set() change_feed = KafkaChangeFeed(topics=[COMMCARE_USER], group_id='user-repair') for change in change_feed.iter_changes(since=start_offset, forever=False): if change.sequence_id > end_offset: return if change.id in seen_ids: continue seen_ids.add(change.id) if change.deleted: continue try: user = change.get_document() except ResourceNotFound: continue user = CommCareUser.wrap(user) if user_looks_ok(user): continue restore_domain_membership(user, check=check) if change.sequence_id % 100 == 0: print("Processed up to offset: {}".format(change.sequence_id))
def get_user_by_org_unit(domain, org_unit_id, top_org_unit_name): """ Look up user ID by a DHIS2 organisation unit ID """ result = ( UserES().domain(domain).mobile_users() # .term('user_data.dhis_org_id', org_unit_id) .run()) # cf. http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-dynamic-mapping.html # If/when we upgrade elasticsearch, we can filter on dynamic mappings and # uncomment the ".term" line above. Until then, check it ourselves ... for doc in result.hits: if doc['user_data'].get('dhis_org_id') == org_unit_id: return CommCareUser.wrap(doc) # No user is assigned to this organisation unit (i.e. region or facility). # Try its parent org unit. Dhis2OrgUnit.objects = FixtureManager(Dhis2OrgUnit, domain, ORG_UNIT_FIXTURES) org_units = {ou.id: ou for ou in Dhis2OrgUnit.objects.all()} if (org_unit_id in org_units and org_units[org_unit_id].name != top_org_unit_name and org_units[org_unit_id].parent_id): return get_user_by_org_unit(domain, org_units[org_unit_id].parent_id, top_org_unit_name) # We don't know that org unit ID, or we're at the top for this project, or we're at the top of DHIS2 return None
def bootstrap_user( setup, username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, ): user_data = user_data or {} user = CommCareUser.create(domain, username, password, phone_numbers=[TEST_NUMBER], user_data=user_data, first_name=first_name, last_name=last_name) if home_loc == setup.loc.site_code: user.set_location(setup.loc) entry = user.get_or_create_phone_entry(phone_number) entry.set_two_way() entry.set_verified() entry.backend_id = backend entry.save() return CommCareUser.wrap(user.to_json())
def bootstrap_user(loc, username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, language=None): user_data = user_data or {} user = CommCareUser.create(domain, username, password, phone_numbers=[phone_number], user_data=user_data, first_name=first_name, last_name=last_name) if language: user.language = language if home_loc == loc.site_code: interface = SupplyInterface(domain) if not interface.get_by_location(loc): interface.create_from_location(domain, loc) user.set_location(loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) user.save() return CommCareUser.wrap(user.to_json())
def rows(self): rows = [] locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id']) dg = [] for date in list(rrule.rrule(rrule.MONTHLY, dtstart=self.config['startdate'], until=self.config['enddate'])): dg.extend(DeliveryGroups().submitting(locations, date.month)) for child in dg: total_responses = 0 total_possible = 0 submitted, rr_value = randr_value(child.location_id, self.config['startdate'], self.config['enddate']) if child.is_archived and not rr_value: continue group_summaries = GroupSummary.objects.filter( org_summary__date__lte=self.config['startdate'], org_summary__location_id=child.location_id, title=SupplyPointStatusTypes.R_AND_R_FACILITY ) for group_summary in group_summaries: if group_summary: total_responses += group_summary.responded total_possible += group_summary.total hist_resp_rate = rr_format_percent(total_responses, total_possible) url = make_url(FacilityDetailsReport, self.config['domain'], '?location_id=%s&filter_by_program=%s&' 'datespan_type=%s&datespan_first=%s&datespan_second=%s', (self.config['location_id'], self.config['program'], self.config['datespan_type'], self.config['datespan_first'], self.config['datespan_second'])) contact = CommCareUser.get_db().view( 'locations/users_by_location_id', startkey=[child.location_id], endkey=[child.location_id, {}], include_docs=True ).first() if contact and contact['doc']: contact = CommCareUser.wrap(contact['doc']) role = contact.user_data.get('role') or "" args = (contact.first_name, contact.last_name, role, contact.default_phone_number) contact_string = "%s %s (%s) %s" % args else: contact_string = "" rows.append( [ child.site_code, link_format(child.name, url), get_span(submitted) % (rr_value.strftime("%d %b %Y") if rr_value else "Not reported"), contact_string, hist_resp_rate ] ) return rows
def bootstrap_user( loc, username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, ): user_data = user_data or {} user = CommCareUser.create(domain, username, password, phone_numbers=[phone_number], user_data=user_data, first_name=first_name, last_name=last_name) if home_loc == loc.site_code: if not SupplyPointCase.get_by_location(loc): make_supply_point(domain, loc) user.set_location(loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommCareUser.wrap(user.to_json())
def restore_domain_membership(user, check=False): doc_id = user._id db = CommCareUser.get_db() revisions = get_doc_revisions(db, doc_id) for rev in revisions[1:]: doc = get_doc_rev(db, doc_id, rev) if not doc: continue prev_user = CommCareUser.wrap(doc) if user_looks_ok(prev_user): if user.location_id != prev_user.domain_membership.location_id: continue if user.assigned_location_ids != prev_user.domain_membership.assigned_location_ids: continue if check: print('Ready to patch user: {} ({})'.format( user.domain, doc_id)) old = json.dumps(user.domain_membership.to_json(), indent=2) print('Old domain membership: \n{}\n'.format(old)) new = json.dumps(prev_user.domain_membership.to_json(), indent=2) print('New domain membership: \n{}\n'.format(new)) if not confirm('Proceed with updating user?'): return print("Patching user: {} ({})".format(user.domain, doc_id)) prev_domain_membership = prev_user.domain_membership user.domain_membership = prev_domain_membership user.save() return print('Unable to fix user: {} ({})'.format(user.domain, doc_id))
def bootstrap_user(username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, program_id=None ): user_data = user_data or {} user = CommCareUser.create( domain, username, password, user_data=user_data, first_name=first_name, last_name=last_name ) user.phone_numbers = [phone_number] if home_loc: user.set_location(home_loc) dm = user.get_domain_membership(domain) dm.program_id = program_id user.save() entry = user.get_or_create_phone_entry(phone_number) entry.set_two_way() entry.set_verified() entry.backend_id = backend entry.save() return CommCareUser.wrap(user.to_json())
def get_user_by_org_unit(domain, org_unit_id, top_org_unit_name): """ Look up user ID by a DHIS2 organisation unit ID """ result = (UserES() .domain(domain) .mobile_users() # .term('user_data.dhis_org_id', org_unit_id) .run()) # cf. http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-dynamic-mapping.html # If/when we upgrade elasticsearch, we can filter on dynamic mappings and # uncomment the ".term" line above. Until then, check it ourselves ... for doc in result.hits: if doc['user_data'].get('dhis_org_id') == org_unit_id: return CommCareUser.wrap(doc) # No user is assigned to this organisation unit (i.e. region or facility). # Try its parent org unit. Dhis2OrgUnit.objects = FixtureManager(Dhis2OrgUnit, domain, ORG_UNIT_FIXTURES) org_units = {ou.id: ou for ou in Dhis2OrgUnit.objects.all()} if ( org_unit_id in org_units and org_units[org_unit_id].name != top_org_unit_name and org_units[org_unit_id].parent_id ): return get_user_by_org_unit(domain, org_units[org_unit_id].parent_id, top_org_unit_name) # We don't know that org unit ID, or we're at the top for this project, or we're at the top of DHIS2 return None
def _format_user(self, user_json): user = CommCareUser.wrap(user_json) user_data = {} for field in self.custom_data.fields: user_data[field.slug] = user.user_data.get(field.slug, '') return { 'username': user.raw_username, 'customFields': user_data, 'first_name': user.first_name, 'last_name': user.last_name, 'phoneNumbers': user.phone_numbers, 'user_id': user.user_id, 'mark_activated': False, 'mark_deactivated': False, 'dateRegistered': user.created_on.strftime(USER_DATE_FORMAT) if user.created_on else '', 'editUrl': reverse(EditCommCareUserView.urlname, args=[self.domain, user.user_id]), 'deactivateUrl': "#", 'actionText': _("Deactivate") if user.is_active else _("Activate"), 'action': 'deactivate' if user.is_active else 'activate', }
def message_form(self): if self.request.method == 'POST': return MessageForm(self.request.POST, **self.form_kwargs) broadcast = self.broadcast schedule = broadcast.schedule schedule_instances = get_alert_schedule_instances_for_schedule(schedule) recipients = [ (instance.recipient_type, instance.recipient_id) for instance in schedule_instances ] ret = [] if recipients: for doc_type, doc_id in recipients: user = CommCareUser.wrap(CommCareUser.get_db().get(doc_id)) ret.append({"id": doc_id, "text": user.raw_username}) initial = { 'schedule_name': broadcast.name, 'send_frequency': 'immediately', 'recipients': ret, 'content': 'sms', # only works for SMS 'message': schedule.memoized_events[0].content.message, } return MessageForm(initial=initial, **self.form_kwargs)
def bootstrap_user(username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, program_id=None): user_data = user_data or {} user = CommCareUser.create(domain, username, password, phone_numbers=[phone_number], user_data=user_data, first_name=first_name, last_name=last_name) if home_loc: user.set_location(home_loc) dm = user.get_domain_membership(domain) dm.program_id = program_id user.save() user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommCareUser.wrap(user.to_json())
def bootstrap_user( username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, ): from corehq.apps.commtrack.helpers import make_supply_point user_data = user_data or {} user = CommCareUser.create(domain, username, password, phone_numbers=[TEST_NUMBER], user_data=user_data, first_name=first_name, last_name=last_name) if not SupplyPointCase.get_by_location(home_loc): make_supply_point(domain, home_loc) home_loc.save() user.set_location(home_loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommCareUser.wrap(user.to_json())
def bootstrap_user( username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name="", last_name="", home_loc=None, user_data=None, program_id=None, ): user_data = user_data or {} user = CommCareUser.create( domain, username, password, phone_numbers=[TEST_NUMBER], user_data=user_data, first_name=first_name, last_name=last_name, ) user.set_location(home_loc) dm = user.get_domain_membership(domain) dm.program_id = program_id user.save() user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommCareUser.wrap(user.to_json())
def bootstrap_user(username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, ): from corehq.apps.commtrack.helpers import make_supply_point user_data = user_data or {} user = CommCareUser.create( domain, username, password, phone_numbers=[TEST_NUMBER], user_data=user_data, first_name=first_name, last_name=last_name ) if not SupplyPointCase.get_by_location(home_loc): make_supply_point(domain, home_loc) home_loc.save() user.set_location(home_loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommCareUser.wrap(user.to_json())
def bootstrap_user(loc, username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, language=None ): user_data = user_data or {} user = CommCareUser.create( domain, username, password, phone_numbers=[phone_number], user_data=user_data, first_name=first_name, last_name=last_name ) if language: user.language = language if home_loc == loc.site_code: interface = SupplyInterface(domain) if not interface.get_by_location(loc): interface.create_from_location(domain, loc) user.set_location(loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) user.save() return CommCareUser.wrap(user.to_json())
def users_from_es(self): if self.es_results is None: self.query_es() users = [ res['_source'] for res in self.es_results.get('hits', {}).get('hits', []) ] return [CommCareUser.wrap(user) for user in users]
def handle(self, domain, **options): ids = (CommCareUser.ids_by_domain(domain, is_active=True) + CommCareUser.ids_by_domain(domain, is_active=False)) for doc in iter_docs(CommCareUser.get_db(), ids): user = CommCareUser.wrap(doc) try: self.process_user(user) except Exception as e: print("Error processing user %s: %s" % (user._id, e))
def handle(self, domain, **options): ids = ( CommCareUser.ids_by_domain(domain, is_active=True) + CommCareUser.ids_by_domain(domain, is_active=False) ) for doc in iter_docs(CommCareUser.get_db(), ids): user = CommCareUser.wrap(doc) try: self.process_user(user) except Exception as e: print("Error processing user %s: %s" % (user._id, e))
def _get_users_by_loc_id(location_type): """Find any existing users previously assigned to this type""" loc_ids = SQLLocation.objects.filter( location_type=location_type).location_ids() user_ids = list(UserES().domain(location_type.domain).show_inactive().term( 'user_location_id', list(loc_ids)).values_list('_id', flat=True)) return { user_doc['user_location_id']: CommCareUser.wrap(user_doc) for user_doc in iter_docs(CommCareUser.get_db(), user_ids) if 'user_location_id' in user_doc }
def get_all_commcare_users_by_domain(domain): """Returns all CommCareUsers by domain regardless of their active status""" from corehq.apps.users.models import CommCareUser key = [domain, CommCareUser.__name__] ids = [user['id'] for user in CommCareUser.get_db().view( 'domain/docs', startkey=key, endkey=key + [{}], reduce=False, include_docs=False)] return [CommCareUser.wrap(user) for user in iter_docs(CommCareUser.get_db(), ids)]
def deidentify_commcare_user(doc): assert(doc.doc["doc_type"] == "CommCareUser") user = CommCareUser.wrap(doc.doc) for i in range(len(user.phone_numbers)): user.phone_numbers[i] = random_phonenumber() name = arbitrary_fullname() user.first_name = name.split(" ")[0] user.last_name = name.split(" ")[1] user.username = username_from_name(name) doc.doc = user._doc return doc
def _get_users_by_loc_id(location_type): """Find any existing users previously assigned to this type""" loc_ids = SQLLocation.objects.filter(location_type=location_type).location_ids() user_ids = list(UserES() .domain(location_type.domain) .show_inactive() .term('user_location_id', list(loc_ids)) .values_list('_id', flat=True)) return { user_doc['user_location_id']: CommCareUser.wrap(user_doc) for user_doc in iter_docs(CommCareUser.get_db(), user_ids) if 'user_location_id' in user_doc }
def get_meta(id): try: doc = get_db().get(id) except (ResourceNotFound, AttributeError): return {'name': None, 'doc_type': None} return { 'name': { 'CommCareUser': lambda user: CommCareUser.wrap(user).raw_username, 'WebUser': lambda user: user['username'], 'Group': lambda group: group['name'] }.get(doc['doc_type'], lambda x: None)(doc), 'doc_type': doc['doc_type'] }
def bulk_auto_deactivate_commcare_users(user_ids, domain): """ Deactivates CommCareUsers in bulk. Please pre-chunk ids to a reasonable size. Also please reference the save() method in CommCareUser when making changes. :param user_ids: list of user IDs :param domain: name of domain user IDs belong to """ from corehq.apps.users.models import UserHistory, CommCareUser from corehq.apps.users.model_log import UserModelAction last_modified = json_format_datetime(datetime.datetime.utcnow()) user_docs_to_bulk_save = [] for user_doc in get_docs(CommCareUser.get_db(), keys=user_ids): if user_doc['is_active']: user_doc['is_active'] = False user_doc['last_modified'] = last_modified user_docs_to_bulk_save.append(user_doc) # bulk save django Users user_query = User.objects.filter( username__in=[u["username"] for u in user_docs_to_bulk_save]) user_query.update(is_active=False) # bulk save in couch CommCareUser.get_db().bulk_save(user_docs_to_bulk_save) # bulk create all the UserHistory logs UserHistory.objects.bulk_create([ UserHistory(by_domain=domain, for_domain=domain, user_type=CommCareUser.doc_type, user_repr=u['username'].split('@')[0], changed_by_repr=SYSTEM_USER_ID, user_id=u['_id'], changed_by=SYSTEM_USER_ID, changes={'is_active': False}, changed_via=USER_CHANGE_VIA_AUTO_DEACTIVATE, change_messages={}, action=UserModelAction.UPDATE.value, user_upload_record_id=None) for u in user_docs_to_bulk_save ]) # clear caches and fire signals for user_doc in user_docs_to_bulk_save: commcare_user = CommCareUser.wrap(user_doc) commcare_user.clear_quickcache_for_user() commcare_user.fire_signals()
def users_from_es(self): q = { "query": { "query_string": { "query": self.query }}, "filter": {"and": ADD_TO_ES_FILTER["users"][:]}, "sort": {'username.exact': 'asc'}, } params = { "domain": self.domain, "is_active": not self.show_inactive, } results = es_query(params=params, q=q, es_url=ES_URLS["users"], size=self.users_list_limit, start_at=self.users_list_skip) users = [res['_source'] for res in results.get('hits', {}).get('hits', [])] return [CommCareUser.wrap(user) for user in users]
def handle(self, *args, **options): if len(args) == 0: raise CommandError("Usage: python manage.py resync_location_user_data %s" % self.args) domain = args[0] ids = ( CommCareUser.ids_by_domain(domain, is_active=True) + CommCareUser.ids_by_domain(domain, is_active=False) ) for doc in iter_docs(CommCareUser.get_db(), ids): user = CommCareUser.wrap(doc) try: self.process_user(user) except Exception as e: print "Error processing user %s: %s" % (user._id, e)
def handle(self, *args, **options): if len(args) == 0: raise CommandError( "Usage: python manage.py resync_location_user_data %s" % self.args) domain = args[0] ids = (CommCareUser.ids_by_domain(domain, is_active=True) + CommCareUser.ids_by_domain(domain, is_active=False)) for doc in iter_docs(CommCareUser.get_db(), ids): user = CommCareUser.wrap(doc) try: self.process_user(user) except Exception as e: print "Error processing user %s: %s" % (user._id, e)
def _unassign_users_from_location(domain, location_id): """ Unset location for all users assigned to that location. """ from corehq.apps.locations.dbaccessors import user_ids_at_locations from corehq.apps.users.models import CommCareUser from dimagi.utils.couch.database import iter_docs user_ids = user_ids_at_locations([location_id]) for doc in iter_docs(CommCareUser.get_db(), user_ids): user = CommCareUser.wrap(doc) if user.is_web_user(): user.unset_location_by_id(domain, location_id, fall_back_to_next=True) elif user.is_commcare_user(): user.unset_location_by_id(location_id, fall_back_to_next=True)
def test_get_owner_ids(self): loc_type = self.loc.location_type_object self.assertFalse(loc_type.shares_cases) owner_ids = self.user.get_owner_ids() self.assertEqual(1, len(owner_ids)) self.assertEqual(self.user._id, owner_ids[0]) # change it so case sharing is enabled and make sure it is now included loc_type.shares_cases = True loc_type.save() # we have to re-create the user object because various things are cached user = CommCareUser.wrap(self.user.to_json()) owner_ids = user.get_owner_ids() self.assertEqual(2, len(owner_ids)) self.assertEqual(self.loc.location_id, owner_ids[1]) # set it back to false in case other tests needed that loc_type.shares_cases = False loc_type.save()
def test_get_owner_ids(self): loc_type = self.loc.location_type_object self.assertFalse(loc_type.shares_cases) owner_ids = self.user.get_owner_ids() self.assertEqual(1, len(owner_ids)) self.assertEqual(self.user._id, owner_ids[0]) # change it so case sharing is enabled and make sure it is now included loc_type.shares_cases = True loc_type.save() # we have to re-create the user object because various things are cached user = CommCareUser.wrap(self.user.to_json()) owner_ids = user.get_owner_ids() self.assertEqual(2, len(owner_ids)) self.assertEqual(self.loc._id, owner_ids[1]) # set it back to false in case other tests needed that loc_type.shares_cases = False loc_type.save()
def _mobile_user_rows(self, domain): rows = [] for user in get_all_user_rows(domain.name, include_web_users=False, include_mobile_users=True, include_inactive=False, include_docs=True): user = CommCareUser.wrap(user['doc']) rows.append([ re.sub(r'@.*', '', user.username), user.full_name, self._format_date( user.reporting_metadata.last_sync_for_user.sync_date), self._format_date(user.reporting_metadata. last_submission_for_user.submission_date), user.reporting_metadata.last_submission_for_user. commcare_version or '', ] + self._domain_properties(domain)) return rows
def bootstrap_user(setup, username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, ): user_data = user_data or {} user = CommCareUser.create( domain, username, password, phone_numbers=[TEST_NUMBER], user_data=user_data, first_name=first_name, last_name=last_name ) if home_loc == setup.loc.site_code: user.set_location(setup.loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommCareUser.wrap(user.to_json())
def _format_user(self, user_json): user = CommCareUser.wrap(user_json) user_data = {} for field in self.custom_data.fields: user_data[field.slug] = user.user_data.get(field.slug, '') return { 'username': user.raw_username, 'customFields': user_data, 'first_name': user.first_name, 'last_name': user.last_name, 'phoneNumbers': user.phone_numbers, 'user_id': user.user_id, 'mark_activated': False, 'mark_deactivated': False, 'dateRegistered': user.created_on.strftime(USER_DATE_FORMAT) if user.created_on else '', 'editUrl': reverse(EditCommCareUserView.urlname, args=[self.domain, user.user_id]), 'deactivateUrl': "#", 'actionText': _("Deactivate") if user.is_active else _("Activate"), 'action': 'deactivate' if user.is_active else 'activate', }
def bootstrap_user(loc, username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, ): user_data = user_data or {} user = CommCareUser.create( domain, username, password, phone_numbers=[phone_number], user_data=user_data, first_name=first_name, last_name=last_name ) if home_loc == loc.site_code: if not SupplyPointCase.get_by_location(loc): make_supply_point(domain, loc) user.set_location(loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) return CommCareUser.wrap(user.to_json())
def get_user_by_org_unit(domain, org_unit_id, top_org_unit_name): """ Look up user ID by a DHIS2 organisation unit ID """ result = (UserES() .domain(domain) .term('user_data.dhis_org_id', org_unit_id) .run()) if result.total: # Don't just assign all cases to the first user. Spread them fairly. i = random.randrange(result.total) return CommCareUser.wrap(result.hits[i]) # No user is assigned to this organisation unit (i.e. region or facility). # Try its parent org unit. org_unit_objects = FixtureManager(Dhis2OrgUnit, domain, ORG_UNIT_FIXTURES) org_units = {ou.id: ou for ou in org_unit_objects.all()} if ( org_unit_id in org_units and org_units[org_unit_id]['name'] != top_org_unit_name and org_units[org_unit_id]['parent_id'] ): return get_user_by_org_unit(domain, org_units[org_unit_id]['parent_id'], top_org_unit_name) # We don't know that org unit ID, or we're at the top for this project, or we're at the top of DHIS2 return None
def find_missing_user_repeat_records(startdate, enddate, domains, should_create): stats_per_domain = {} for index, domain in enumerate(domains): t0 = time.time() total_missing_count = 0 location_repeaters_in_domain = get_location_repeaters_in_domain(domain) user_dicts = get_users_created_since_startdate(domain, startdate) users = [CommCareUser.wrap(user_dict) for user_dict in user_dicts] for user in users: missing_count = find_missing_repeat_records_in_domain( domain, location_repeaters_in_domain, user, enddate, should_create) total_missing_count += missing_count t1 = time.time() time_to_run = t1 - t0 if total_missing_count > 0: rounded_time = f'{round(time_to_run, 0)} seconds' stats_per_domain[domain] = { FORMS: { MISSING_REPEAT_RECORD_COUNT: total_missing_count, TIME_TO_RUN: rounded_time, } } logger.info( f'{domain} complete. Found {total_missing_count}" missing repeat records in ' f'{rounded_time}. Due to limitations with users, we only looked for missing repeat ' f'records for newly created users, not recently modified.') if index + 1 % 5 == 0: logger.info(f"{(index+1)}/{len(domains)} domains complete.") return stats_per_domain
def assign_users(self, users): for doc in iter_docs(CommCareUser.get_db(), users): CommCareUser.wrap(doc).add_to_assigned_locations(self.location)
def unassign_users(self, users): for doc in iter_docs(CommCareUser.get_db(), users): # This could probably be sped up by bulk saving, but there's a lot # of stuff going on - seems tricky. CommCareUser.wrap(doc).unset_location_by_id(self.location.location_id, fall_back_to_next=True)
def get_users(self): user_ids = CommCareUser.ids_by_domain(self.domain) for user_doc in iter_docs(CommCareUser.get_db(), user_ids): yield CommCareUser.wrap(user_doc)
def get_users(self): user_ids = CommCareUser.ids_by_domain(self.domain) for user_doc in iter_docs(CommCareUser.get_db(), user_ids): yield CommCareUser.wrap(user_doc)
def unassign_users(self, users): for doc in iter_docs(CommCareUser.get_db(), users): # This could probably be sped up by bulk saving, but there's a lot # of stuff going on - seems tricky. CommCareUser.wrap(doc).unset_location_by_id(self.location.location_id, fall_back_to_next=True)
def assign_users(self, users): for doc in iter_docs(CommCareUser.get_db(), users): CommCareUser.wrap(doc).add_to_assigned_locations(self.location)
def users_from_es(self): if self.es_results is None: self.query_es() users = [res['_source'] for res in self.es_results.get('hits', {}).get('hits', [])] return [CommCareUser.wrap(user) for user in users]