(v, text, msg)
The form session handler will use the inbound text to answer the next question
    in the open SQLXformsSession for the associated contact. If no session is open,
    the handler passes. If multiple sessions are open, they are all closed and an
    error message is displayed to the user.
The form session handler will use the inbound text to answer the next question
    in the open SQLXformsSession for the associated contact. If no session is open,
    the handler passes. If multiple sessions are open, they are all closed and an
    error message is displayed to the user.
The form session handler will use the inbound text to answer the next question in the open SQLXformsSession for the associated contact . If no session is open the handler passes . If multiple sessions are open they are all closed and an error message is displayed to the user .
def form_session_handler(v, text, msg):
    """
    The form session handler will use the inbound text to answer the next question
    in the open SQLXformsSession for the associated contact. If no session is open,
    the handler passes. If multiple sessions are open, they are all closed and an
    error message is displayed to the user.
    """
    with critical_section_for_smsforms_sessions(v.owner_id):
        if toggles.ONE_PHONE_NUMBER_MULTIPLE_CONTACTS.enabled(v.domain):
            channel = get_channel_for_contact(v.owner_id, v.phone_number)
            running_session_info = XFormsSessionSynchronization.get_running_session_info_for_channel(channel)
            if running_session_info.session_id:
                session = SQLXFormsSession.by_session_id(running_session_info.session_id)
                if not session.session_is_open:
                    # This should never happen. But if it does we should set the channel free
                    # and act like there was no available session
                    notify_error("The supposedly running session was not open and was released. "
                                 'No known way for this to happen, so worth investigating.')
                    XFormsSessionSynchronization.clear_stale_channel_claim(channel)
                    session = None
            else:
                session = None
        else:
            multiple, session = get_single_open_session_or_close_multiple(v.domain, v.owner_id)
            if multiple:
                send_sms_to_verified_number(v, get_message(MSG_MULTIPLE_SESSIONS, v))
                return True

        if session:
            session.phone_number = v.phone_number
            session.modified_time = datetime.utcnow()
            session.save()

            subevent = session.related_subevent
            subevent_id = subevent.id if subevent else None

            # Metadata to be applied to the inbound message
            inbound_metadata = MessageMetadata(
                workflow=session.workflow,
                reminder_id=session.reminder_id,
                xforms_session_couch_id=session._id,
                messaging_subevent_id=subevent_id,
            )
            add_msg_tags(msg, inbound_metadata)
            msg.save()
            try:
                answer_next_question(v, text, msg, session, subevent_id)
            except Exception:
                # Catch any touchforms errors
                log_sms_exception(msg)
                send_sms_to_verified_number(v, get_message(MSG_TOUCHFORMS_DOWN, v))
            return True
        else:
            return False
def form_session_handler ( v , text , msg ) : with critical_section_for_smsforms_sessions ( v . owner_id ) : if toggles . ONE_PHONE_NUMBER_MULTIPLE_CONTACTS . enabled ( v . domain ) : channel = get_channel_for_contact ( v . owner_id , v . phone_number ) running_session_info = XFormsSessionSynchronization . get_running_session_info_for_channel ( channel ) if running_session_info . session_id : session = SQLXFormsSession . by_session_id ( running_session_info . session_id ) if not session . session_is_open : # This should never happen. But if it does we should set the channel free # and act like there was no available session notify_error ( "The supposedly running session was not open and was released. " 'No known way for this to happen, so worth investigating.' ) XFormsSessionSynchronization . clear_stale_channel_claim ( channel ) session = None else : session = None else : multiple , session = get_single_open_session_or_close_multiple ( v . domain , v . owner_id ) if multiple : send_sms_to_verified_number ( v , get_message ( MSG_MULTIPLE_SESSIONS , v ) ) return True if session : session . phone_number = v . phone_number session . modified_time = datetime . utcnow ( ) session . save ( ) subevent = session . related_subevent subevent_id = subevent . id if subevent else None # Metadata to be applied to the inbound message inbound_metadata = MessageMetadata ( workflow = session . workflow , reminder_id = session . reminder_id , xforms_session_couch_id = session . _id , messaging_subevent_id = subevent_id , ) add_msg_tags ( msg , inbound_metadata ) msg . save ( ) try : answer_next_question ( v , text , msg , session , subevent_id ) except Exception : # Catch any touchforms errors log_sms_exception ( msg ) send_sms_to_verified_number ( v , get_message ( MSG_TOUCHFORMS_DOWN , v ) ) return True else : return False