def initiate_sms_verification_workflow(contact, phone_number): # For now this is only applicable to mobile workers assert isinstance(contact, CommCareUser) logged_event = MessagingEvent.get_current_verification_event( contact.domain, contact.get_id, phone_number) with CriticalSection(['verifying-phone-number-%s' % phone_number]): vn = VerifiedNumber.by_phone(phone_number, include_pending=True) if vn: if vn.owner_id != contact._id: return VERIFICATION__ALREADY_IN_USE if vn.verified: return VERIFICATION__ALREADY_VERIFIED else: result = VERIFICATION__RESENT_PENDING else: contact.save_verified_number(contact.domain, phone_number, False) result = VERIFICATION__WORKFLOW_STARTED # Always create a new event when the workflow starts if logged_event: logged_event.status = MessagingEvent.STATUS_NOT_COMPLETED logged_event.save() logged_event = MessagingEvent.create_verification_event(contact.domain, contact) if not logged_event: logged_event = MessagingEvent.create_verification_event(contact.domain, contact) send_verification(contact.domain, contact, phone_number, logged_event) return result
def initiate_sms_verification_workflow(contact, phone_number): # For now this is only applicable to mobile workers assert isinstance(contact, CommCareUser) logged_event = MessagingEvent.get_current_verification_event( contact.domain, contact.get_id, phone_number) with CriticalSection(['verifying-phone-number-%s' % phone_number]): vn = PhoneNumber.by_phone(phone_number, include_pending=True) if vn: if vn.owner_id != contact.get_id: return VERIFICATION__ALREADY_IN_USE if vn.verified: return VERIFICATION__ALREADY_VERIFIED else: result = VERIFICATION__RESENT_PENDING else: contact.save_verified_number(contact.domain, phone_number, False) result = VERIFICATION__WORKFLOW_STARTED # Always create a new event when the workflow starts if logged_event: logged_event.status = MessagingEvent.STATUS_NOT_COMPLETED logged_event.save() logged_event = MessagingEvent.create_verification_event( contact.domain, contact) if not logged_event: logged_event = MessagingEvent.create_verification_event( contact.domain, contact) send_verification(contact.domain, contact, phone_number, logged_event) return result
def initiate_sms_verification_workflow(contact, phone_number): # For now this is only applicable to mobile workers assert isinstance(contact, CommCareUser) phone_number = apply_leniency(phone_number) logged_event = MessagingEvent.get_current_verification_event( contact.domain, contact.get_id, phone_number) p = PhoneNumber.get_reserved_number(phone_number) if p: if p.owner_id != contact.get_id: return VERIFICATION__ALREADY_IN_USE if p.verified: return VERIFICATION__ALREADY_VERIFIED else: result = VERIFICATION__RESENT_PENDING else: entry = contact.get_or_create_phone_entry(phone_number) try: entry.set_pending_verification() except PhoneNumberInUseException: # On the off chance that the phone number was reserved between # the check above and now return VERIFICATION__ALREADY_IN_USE result = VERIFICATION__WORKFLOW_STARTED # Always create a new event when the workflow starts if logged_event: logged_event.status = MessagingEvent.STATUS_NOT_COMPLETED logged_event.save() logged_event = MessagingEvent.create_verification_event( contact.domain, contact) if not logged_event: logged_event = MessagingEvent.create_verification_event( contact.domain, contact) send_verification(contact.domain, contact, phone_number, logged_event) return result
def initiate_sms_verification_workflow(contact, phone_number): # For now this is only applicable to mobile workers assert isinstance(contact, CommCareUser) phone_number = apply_leniency(phone_number) logged_event = MessagingEvent.get_current_verification_event( contact.domain, contact.get_id, phone_number) p = PhoneNumber.get_reserved_number(phone_number) if p: if p.owner_id != contact.get_id: return VERIFICATION__ALREADY_IN_USE if p.verified: return VERIFICATION__ALREADY_VERIFIED else: result = VERIFICATION__RESENT_PENDING else: entry = contact.get_or_create_phone_entry(phone_number) try: entry.set_pending_verification() except PhoneNumberInUseException: # On the off chance that the phone number was reserved between # the check above and now return VERIFICATION__ALREADY_IN_USE result = VERIFICATION__WORKFLOW_STARTED # Always create a new event when the workflow starts if logged_event: logged_event.status = MessagingEvent.STATUS_NOT_COMPLETED logged_event.save() logged_event = MessagingEvent.create_verification_event(contact.domain, contact) if not logged_event: logged_event = MessagingEvent.create_verification_event(contact.domain, contact) send_verification(contact.domain, contact, phone_number, logged_event) return result
def process_verification(v, msg, verification_keywords=None, create_subevent_for_inbound=True): verification_keywords = verification_keywords or ['123'] logged_event = MessagingEvent.get_current_verification_event( v.domain, v.owner_id, v.phone_number) if not logged_event: logged_event = MessagingEvent.create_verification_event(v.domain, v.owner) msg.domain = v.domain msg.couch_recipient_doc_type = v.owner_doc_type msg.couch_recipient = v.owner_id if create_subevent_for_inbound: subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id ) subevent.completed() msg.messaging_subevent_id = subevent.pk msg.save() if ( not domain_has_privilege(msg.domain, privileges.INBOUND_SMS) or not verification_response_ok(msg.text, verification_keywords) ): return False v.set_two_way() v.set_verified() v.save() logged_event.completed() subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id ) message = messages.get_message( messages.MSG_VERIFICATION_SUCCESSFUL, verified_number=v ) send_sms_to_verified_number(v, message, metadata=MessageMetadata(messaging_subevent_id=subevent.pk)) subevent.completed() return True
def process_verification(phone_number, msg, backend_id=None): v = VerifiedNumber.by_phone(phone_number, True) if not v: return logged_event = MessagingEvent.get_current_verification_event( v.domain, v.owner_id, phone_number) if not logged_event: logged_event = MessagingEvent.create_verification_event( v.domain, v.owner) subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id) subevent.completed() msg.domain = v.domain msg.couch_recipient_doc_type = v.owner_doc_type msg.couch_recipient = v.owner_id msg.messaging_subevent_id = subevent.pk msg.save() if (not domain_has_privilege(msg.domain, privileges.INBOUND_SMS) or not verification_response_ok(msg.text)): return if backend_id: backend = MobileBackend.load(backend_id) else: backend = MobileBackend.auto_load(phone_number, v.domain) assert v.owner_doc_type == 'CommCareUser' owner = CommCareUser.get(v.owner_id) v = owner.save_verified_number(v.domain, phone_number, True, backend.name) logged_event.completed() subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id) with localize(owner.language): send_sms_to_verified_number( v, _(CONFIRM), metadata=MessageMetadata(messaging_subevent_id=subevent.pk)) subevent.completed()
def process_verification(v, msg, verification_keywords=None, create_subevent_for_inbound=True): verification_keywords = verification_keywords or ['123'] logged_event = MessagingEvent.get_current_verification_event( v.domain, v.owner_id, v.phone_number) if not logged_event: logged_event = MessagingEvent.create_verification_event(v.domain, v.owner) msg.domain = v.domain msg.couch_recipient_doc_type = v.owner_doc_type msg.couch_recipient = v.owner_id if create_subevent_for_inbound: subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id ) subevent.completed() msg.messaging_subevent_id = subevent.pk msg.save() if ( not domain_has_privilege(msg.domain, privileges.INBOUND_SMS) or not verification_response_ok(msg.text, verification_keywords) ): return False v.set_two_way() v.set_verified() v.save() logged_event.completed() subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id ) message = messages.get_message( messages.MSG_VERIFICATION_SUCCESSFUL, verified_number=v ) send_sms_to_verified_number(v, message, metadata=MessageMetadata(messaging_subevent_id=subevent.pk)) subevent.completed() return True
def process_verification(v, msg): if not v or v.verified: return logged_event = MessagingEvent.get_current_verification_event( v.domain, v.owner_id, v.phone_number) if not logged_event: logged_event = MessagingEvent.create_verification_event(v.domain, v.owner) subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id ) subevent.completed() msg.domain = v.domain msg.couch_recipient_doc_type = v.owner_doc_type msg.couch_recipient = v.owner_id msg.messaging_subevent_id = subevent.pk msg.save() if ( not domain_has_privilege(msg.domain, privileges.INBOUND_SMS) or not verification_response_ok(msg.text) ): return v.verified = True v.save() logged_event.completed() subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id ) message = messages.get_message( messages.MSG_VERIFICATION_SUCCESSFUL, verified_number=v ) send_sms_to_verified_number(v, message, metadata=MessageMetadata(messaging_subevent_id=subevent.pk)) subevent.completed()
def process_verification(v, msg): if not v or v.verified: return logged_event = MessagingEvent.get_current_verification_event( v.domain, v.owner_id, v.phone_number) if not logged_event: logged_event = MessagingEvent.create_verification_event(v.domain, v.owner) subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id ) subevent.completed() msg.domain = v.domain msg.couch_recipient_doc_type = v.owner_doc_type msg.couch_recipient = v.owner_id msg.messaging_subevent_id = subevent.pk msg.save() if ( not domain_has_privilege(msg.domain, privileges.INBOUND_SMS) or not verification_response_ok(msg.text) ): return v.verified = True v.save() logged_event.completed() subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id ) with localize(v.owner.get_language_code()): send_sms_to_verified_number(v, _(CONFIRM), metadata=MessageMetadata(messaging_subevent_id=subevent.pk)) subevent.completed()
def process_verification(v, msg): if not v or v.verified: return logged_event = MessagingEvent.get_current_verification_event( v.domain, v.owner_id, v.phone_number) if not logged_event: logged_event = MessagingEvent.create_verification_event( v.domain, v.owner) subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id) subevent.completed() msg.domain = v.domain msg.couch_recipient_doc_type = v.owner_doc_type msg.couch_recipient = v.owner_id msg.messaging_subevent_id = subevent.pk msg.save() if (not domain_has_privilege(msg.domain, privileges.INBOUND_SMS) or not verification_response_ok(msg.text)): return v.verified = True v.save() logged_event.completed() subevent = logged_event.create_subevent_for_single_sms( v.owner_doc_type, v.owner_id) message = messages.get_message(messages.MSG_VERIFICATION_SUCCESSFUL, verified_number=v) send_sms_to_verified_number( v, message, metadata=MessageMetadata(messaging_subevent_id=subevent.pk)) subevent.completed()