#NEWLINE# def process_survey_keyword_actions( verified_number , survey_keyword , text , msg ) : #NEWLINE# #INDENT# sender = verified_number . owner #NEWLINE# case = None #NEWLINE# args = split_args ( text , survey_keyword ) #NEWLINE# logged_event = MessagingEvent . create_from_keyword ( survey_keyword , sender ) #NEWLINE# subevent = logged_event . create_subevent_for_single_sms ( msg . couch_recipient_doc_type , msg . couch_recipient , completed = True ) #NEWLINE# add_msg_tags ( msg , MessageMetadata ( messaging_subevent_id = subevent . pk ) ) #NEWLINE# SQLXFormsSession . close_all_open_sms_sessions ( verified_number . domain , verified_number . owner_id ) #NEWLINE# if ( sender . doc_type == 'CommCareCase' ) : #NEWLINE# #INDENT# case = sender #NEWLINE# args = args [ 1 : ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( sender . doc_type == 'CommCareUser' ) : #NEWLINE# #INDENT# if keyword_uses_form_that_requires_case ( survey_keyword ) : #NEWLINE# #INDENT# if ( len ( args ) > 1 ) : #NEWLINE# #INDENT# external_id = args [ 1 ] #NEWLINE# ( case , matches ) = get_case_by_external_id ( verified_number . domain , external_id , sender ) #NEWLINE# if ( matches == 0 ) : #NEWLINE# #INDENT# send_keyword_response ( verified_number , MSG_CASE_NOT_FOUND , logged_event ) #NEWLINE# logged_event . error ( MessagingEvent . ERROR_CASE_EXTERNAL_ID_NOT_FOUND ) #NEWLINE# return #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( matches > 1 ) : #NEWLINE# #INDENT# send_keyword_response ( verified_number , MSG_MULTIPLE_CASES_FOUND , logged_event ) #NEWLINE# logged_event . error ( MessagingEvent . ERROR_MULTIPLE_CASES_WITH_EXTERNAL_ID_FOUND ) #NEWLINE# return #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# send_keyword_response ( verified_number , MSG_MISSING_EXTERNAL_ID , logged_event ) #NEWLINE# logged_event . error ( MessagingEvent . ERROR_NO_EXTERNAL_ID_GIVEN ) #NEWLINE# return #NEWLINE# #UNINDENT# args = args [ 2 : ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# args = args [ 1 : ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# def cmp_fcn( a1 , a2 ) : #NEWLINE# #INDENT# a1_ss = ( a1 . action == METHOD_STRUCTURED_SMS ) #NEWLINE# a2_ss = ( a2 . action == METHOD_STRUCTURED_SMS ) #NEWLINE# if ( a1_ss and a2_ss ) : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if a1_ss : #NEWLINE# #INDENT# return ( - 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if a2_ss : #NEWLINE# #INDENT# return 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return 0 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if case : #NEWLINE# #INDENT# subevent . case_id = case . get_id #NEWLINE# subevent . save ( ) #NEWLINE# #UNINDENT# actions = sorted ( survey_keyword . actions , cmp = cmp_fcn ) #NEWLINE# for survey_keyword_action in actions : #NEWLINE# #INDENT# if ( survey_keyword_action . recipient == RECIPIENT_SENDER ) : #NEWLINE# #INDENT# contact = sender #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( survey_keyword_action . recipient == RECIPIENT_OWNER ) : #NEWLINE# #INDENT# if ( sender . doc_type == 'CommCareCase' ) : #NEWLINE# #INDENT# contact = get_wrapped_owner ( get_owner_id ( sender ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# contact = None #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( survey_keyword_action . recipient == RECIPIENT_USER_GROUP ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# contact = Group . get ( survey_keyword_action . recipient_id ) #NEWLINE# assert ( contact . doc_type == 'Group' ) #NEWLINE# assert ( contact . domain == verified_number . domain ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# contact = None #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# contact = None #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( contact is None ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( survey_keyword_action . action == METHOD_SMS ) : #NEWLINE# #INDENT# create_immediate_reminder ( contact , METHOD_SMS , reminder_type = REMINDER_TYPE_KEYWORD_INITIATED , message = survey_keyword_action . message_content , case = case , logged_event = logged_event ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( survey_keyword_action . action == METHOD_SMS_SURVEY ) : #NEWLINE# #INDENT# create_immediate_reminder ( contact , METHOD_SMS_SURVEY , reminder_type = REMINDER_TYPE_KEYWORD_INITIATED , form_unique_id = survey_keyword_action . form_unique_id , case = case , logged_event = logged_event ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( survey_keyword_action . action == METHOD_STRUCTURED_SMS ) : #NEWLINE# #INDENT# res = handle_structured_sms ( survey_keyword , survey_keyword_action , sender , verified_number , text , send_response = True , msg = msg , case = case , text_args = args , logged_event = logged_event ) #NEWLINE# if ( not res ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# logged_event . completed ( )