def get_user_cases_at_location(location): users = get_all_users_by_location(location.domain, location.location_id) user_cases = [ u.memoized_usercase for u in users if isinstance(u, CommCareUser) and u.is_active ] return [case for case in user_cases if case]
def expand_location_ids(domain, location_ids): user_ids = set() for location_id in location_ids: for user in get_all_users_by_location(domain, location_id): if user.is_active and user.get_id not in user_ids: user_ids.add(user.get_id) yield user
def send_ms_alert(self, previous_stockouts, transactions, ms_type): stockouts = { SQLProduct.objects.get(product_id=transaction.product_id).name for transaction in transactions if transaction.quantity == 0 and transaction.action == 'stockonhand' } with_stock = { SQLProduct.objects.get(product_id=transaction.product_id).name for transaction in transactions if transaction.quantity != 0 and transaction.action == 'stockonhand' } resolved_stockouts = previous_stockouts.intersection(with_stock) locations = self.sql_location.parent.get_descendants(include_self=True)\ .filter(location_type__administrative=True) for sql_location in locations: for user in get_all_users_by_location(self.domain, sql_location.location_id): phone_number = get_preferred_phone_number_for_recipient(user) if not phone_number: continue stockouts_and_resolved = [(MS_RESOLVED_STOCKOUTS, resolved_stockouts), (MS_STOCKOUT, stockouts)] for message, data in stockouts_and_resolved: if data: message = message % { 'products_names': ', '.join(data), 'ms_type': ms_type } send_sms(self.domain, user, phone_number, message)
def expand_recipients(self): """ Can be used as a generator to iterate over all individual contacts who are the recipients of this ScheduleInstance. """ if self.recipient is None: return elif self.recipient_is_an_individual_contact: yield self.recipient elif isinstance(self.recipient, CommCareCaseGroup): case_group = self.recipient for case in case_group.get_cases(): yield case elif isinstance(self.recipient, Group): group = self.recipient for user in group.get_users(is_active=True, only_commcare=False): yield user elif isinstance(self.recipient, SQLLocation): location = self.recipient if self.memoized_schedule.include_descendant_locations: location_ids = location.get_descendants( include_self=True).filter( is_archived=False).location_ids() else: location_ids = [location.location_id] user_ids = set() for location_id in location_ids: for user in get_all_users_by_location(self.domain, location_id): if user.is_active and user.get_id not in user_ids: user_ids.add(user.get_id) yield user else: raise UnknownRecipientType(self.recipient.__class__.__name__)
def send_ms_alert(self, previous_stockouts, transactions, ms_type): stockouts = { SQLProduct.objects.get(product_id=transaction.product_id).name for transaction in transactions if transaction.quantity == 0 and transaction.action == 'stockonhand' } with_stock = { SQLProduct.objects.get(product_id=transaction.product_id).name for transaction in transactions if transaction.quantity != 0 and transaction.action == 'stockonhand' } resolved_stockouts = previous_stockouts.intersection(with_stock) locations = self.sql_location.parent.get_descendants(include_self=True)\ .filter(location_type__administrative=True) for sql_location in locations: for user in get_all_users_by_location(self.domain, sql_location.location_id): phone_number = get_preferred_phone_number_for_recipient(user) if not phone_number: continue stockouts_and_resolved = [ (MS_RESOLVED_STOCKOUTS, resolved_stockouts), (MS_STOCKOUT, stockouts) ] for message, data in stockouts_and_resolved: if data: message = message % {'products_names': ', '.join(data), 'ms_type': ms_type} send_sms(self.domain, user, phone_number, message)
def _unassign_users_from_location(domain, location_id): """ Unset location for all users assigned to that location. """ from corehq.apps.locations.dbaccessors import get_all_users_by_location for user in get_all_users_by_location(domain, location_id): 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 _unassign_users_from_location(domain, location_id): """ Unset location for all users assigned to that location. """ from corehq.apps.locations.dbaccessors import get_all_users_by_location for user in get_all_users_by_location(domain, location_id): if user.is_web_user(): user.unset_location(domain) elif user.is_commcare_user(): user.unset_location()
def get_location_user_for_notification(location): all_users_assigned_to_location = get_all_users_by_location( location.domain, location.location_id ) location_type_code = location.location_type.code user_with_user_type_as_loc = [] for user in all_users_assigned_to_location: if user.user_data.get('usertype') == location_type_code: user_with_user_type_as_loc.append(user) if len(user_with_user_type_as_loc) == 0: raise NikshayHealthEstablishmentInvalidUpdate("Location user not found") if len(user_with_user_type_as_loc) > 1: raise NikshayHealthEstablishmentInvalidUpdate("Multiple location users found") return user_with_user_type_as_loc[0]
def _expand_recipient(self, recipient): if recipient is None: return elif self.recipient_is_an_individual_contact(recipient): yield recipient elif isinstance(recipient, CommCareCaseGroup): case_group = recipient for case in case_group.get_cases(): yield case elif isinstance(recipient, Group): group = recipient for user in group.get_users(is_active=True, only_commcare=False): yield user elif isinstance(recipient, SQLLocation): location = recipient if (self.recipient_type == self.RECIPIENT_TYPE_LOCATION and self.memoized_schedule.include_descendant_locations): # Only include descendant locations when the recipient_type # is RECIPIENT_TYPE_LOCATION. This is because we only do this # for locations the user selected in the UI, and not for # locations that happen to get here because they are a case # owner, for example. qs = location.get_descendants(include_self=True).filter( is_archived=False) # We also only apply the location_type_filter when the recipient_type # is RECIPIENT_TYPE_LOCATION for the same reason. if self.memoized_schedule.location_type_filter: qs = qs.filter(location_type_id__in=self.memoized_schedule. location_type_filter) location_ids = qs.location_ids() else: location_ids = [location.location_id] user_ids = set() for location_id in location_ids: for user in get_all_users_by_location(self.domain, location_id): if user.is_active and user.get_id not in user_ids: user_ids.add(user.get_id) yield user else: raise UnknownRecipientType(recipient.__class__.__name__)
def get_commcare_users_by_location(domain_name, location_id): for user in get_all_users_by_location(domain_name, location_id): if user.is_commcare_user(): yield user