def get_one_way_phone_number(cls, recipient): phone_number = get_one_way_number_for_recipient(recipient) if not phone_number and isinstance(recipient, CommCareUser): if recipient.memoized_usercase: phone_number = get_one_way_number_for_recipient(recipient.memoized_usercase) if not phone_number or len(phone_number) <= 3: # Avoid processing phone numbers that are obviously fake to # save on processing time return None return phone_number
def run_indicator(domain, user_id, indicator_class, language_code=None): """ Runs the given indicator for the given user and sends the SMS if needed. :param domain: The domain the indicator is being run for :param user_id: The id of either an AWW or LS CommCareUser :param indicator_class: a subclass of AWWIndicator or LSIndicator """ user = CommCareUser.get_by_user_id(user_id, domain=domain) # The user's phone number and preferred language is stored on the usercase usercase = user.get_usercase() phone_number = get_one_way_number_for_recipient(usercase) if not phone_number or phone_number == '91': # If there is no phone number, don't bother calculating the indicator return if not language_code: language_code = usercase.get_language_code() indicator = indicator_class(domain, user) messages = indicator.get_messages(language_code=language_code) if not isinstance(messages, list): raise ValueError("Expected a list of messages") metadata = MessageMetadata(custom_metadata={ 'icds_indicator': indicator_class.slug, }) for message in messages: send_sms(domain, usercase, phone_number, message, metadata=metadata)
def get_two_way_entry_or_phone_number(cls, recipient, try_user_case=True): """ If recipient has a two-way number, returns it as a PhoneNumber entry. If recipient does not have a two-way number but has a phone number configured, returns the one-way phone number as a string. If try_user_case is True and recipient is a CommCareUser who doesn't have a two-way or one-way phone number, then it will try to get the two-way or one-way number from the user's user case if one exists. """ if use_phone_entries(): phone_entry = get_two_way_number_for_recipient(recipient) if phone_entry: return phone_entry phone_number = get_one_way_number_for_recipient(recipient) # Avoid processing phone numbers that are obviously fake (len <= 3) to # save on processing time if phone_number and len(phone_number) > 3: return phone_number if try_user_case and isinstance( recipient, CommCareUser) and recipient.memoized_usercase: return cls.get_two_way_entry_or_phone_number( recipient.memoized_usercase) return None
def get_two_way_entry_or_phone_number(cls, recipient, try_user_case=True, domain_for_toggles=None): """ If recipient has a two-way number, returns it as a PhoneNumber entry. If recipient does not have a two-way number but has a phone number configured, returns the one-way phone number as a string. If try_user_case is True and recipient is a CommCareUser who doesn't have a two-way or one-way phone number, then it will try to get the two-way or one-way number from the user's user case if one exists. """ if settings.USE_PHONE_ENTRIES: phone_entry = get_two_way_number_for_recipient(recipient) if phone_entry: return phone_entry phone_number = get_one_way_number_for_recipient(recipient) if toggles.INBOUND_SMS_LENIENCY.enabled(domain_for_toggles) and \ toggles.ONE_PHONE_NUMBER_MULTIPLE_CONTACTS.enabled(domain_for_toggles): phone_entry = PhoneNumber.get_phone_number_for_owner(recipient.get_id, phone_number) if phone_entry: return phone_entry # Avoid processing phone numbers that are obviously fake (len <= 3) to # save on processing time if phone_number and len(phone_number) > 3: return phone_number if try_user_case and isinstance(recipient, CommCareUser) and recipient.memoized_usercase: return cls.get_two_way_entry_or_phone_number(recipient.memoized_usercase, domain_for_toggles=domain_for_toggles) return None
def run_indicator(domain, user_id, indicator_class): """ Runs the given indicator for the given user and sends the SMS if needed. :param domain: The domain the indicator is being run for :param user_id: The id of either an AWW or LS CommCareUser :param indicator_class: a subclass of AWWIndicator or LSIndicator """ user = CommCareUser.get_by_user_id(user_id, domain=domain) indicator = indicator_class(domain, user) # The user's phone number and preferred language is stored on the usercase usercase = user.get_usercase() messages = indicator.get_messages( language_code=usercase.get_language_code()) if not isinstance(messages, list): raise ValueError("Expected a list of messages") if messages: phone_number = get_one_way_number_for_recipient(usercase) if not phone_number: return for message in messages: send_sms(domain, usercase, phone_number, message)
def get_one_way_phone_number(self, recipient): phone_number = get_one_way_number_for_recipient(recipient) if not phone_number or len(phone_number) <= 3: # Avoid processing phone numbers that are obviously fake to # save on processing time return None return phone_number
def get_recipient_phone_number(reminder, recipient, verified_numbers): verified_number = verified_numbers.get(recipient.get_id, None) unverified_number = None if verified_number is None: unverified_number = get_one_way_number_for_recipient(recipient) return (verified_number, unverified_number)