def get_translated_response_message(incoming_request, original_message):
    message = incoming_request.get('incoming_message')
    dbm = incoming_request.get('dbm')
    parser = SMSParserFactory().getSMSParser(message, dbm)
    parsing_result = parser.parse(message)
    form_model = get_form_model_by_code(dbm, parsing_result[0])
    translation.activate(form_model.activeLanguages[0])
    incoming_request['outgoing_message'] = ugettext(original_message)
    return incoming_request
Example #2
0
    def submit(self, submission, reporter_id):
        try:
            form_code, values, extra_data = SMSParserFactory().getSMSParser(submission, self.dbm).parse(submission)

            form_model = get_form_model_by_code(self.dbm, form_code)

            if self.organization.has_exceeded_quota_and_notify_users():
                return False, "Exceeded Submission Limit"

            if isinstance(form_model, EntityFormModel):
                response = self._create_identification_number(form_code, values, extra_data)
            else:
                response = self._create_survey_response(form_model, reporter_id, values, extra_data)

        except FormModelDoesNotExistsException as e:
            request = {"form_code": e.data[0],
                       "incoming_message": submission,
                       "organization": self.organization,
                       "transport_info": TransportInfo("api", "", "")}
            create_failure_log("Form Code is not valid.", request)
            return False, "Form Code is not valid."

        except DataObjectAlreadyExists:
            return False, "Duplicate unique id"

        except SMSParserInvalidFormatException:
            return False, "Wrong number of answers"

        except MangroveException as e:
            return False, e.message

        if response.success:
            return True, 'submitted successfully'
        else:
            return False, response.errors.values()[0]
Example #3
0
 def _parse(self, message):
     return SMSParserFactory().getSMSParser(message,
                                            self.dbm).parse(message)
Example #4
0
 def _parse(self, message):
     if self.parser is None:
         self.parser = SMSParserFactory().getSMSParser(message, self.dbm)
     return self.parser.parse(message)
Example #5
0
class SMSPlayer(Player):
    def __init__(self, dbm, location_tree=None, parser=None,
                 post_sms_parser_processors=None, feeds_dbm=None):
        if not post_sms_parser_processors: post_sms_parser_processors = []
        Player.__init__(self, dbm, location_tree)
        self.parser = parser
        self.post_sms_parser_processor = post_sms_parser_processors
        self.feeds_dbm = feeds_dbm

    def _process(self, values, form_code, reporter_entity):
        form_model = get_form_model_by_code(self.dbm, form_code)
        if form_model.is_entity_registration_form():
            values = RegistrationWorkFlow(self.dbm, form_model, self.location_tree).process(values)
        return form_model, values

    def _process_post_parse_callback(self, form_code, values, extra_elements=[]):
        for post_sms_parser_processors in self.post_sms_parser_processor:
            if len(inspect.getargspec(post_sms_parser_processors.process)[0]) == 4:
                response = post_sms_parser_processors.process(form_code, values, extra_elements)
            else:
                response = post_sms_parser_processors.process(form_code, values)
            if response is not None:
                return response

    def _parse(self, message):
        if self.parser is None:
            self.parser = SMSParserFactory().getSMSParser(message, self.dbm)
        return self.parser.parse(message)

    def select_form_model(self, form_code):
        try:
            form_model = get_form_model_by_code(self.dbm, form_code)
            check_if_form_code_is_poll(self, form_model)
        except FormModelDoesNotExistsException:
            form_model = get_active_form_model(self.dbm, form_code)
        return form_model

    def get_form_model(self, request):
        form_code, values, extra_elements = self._parse(request.message)
        return self.select_form_model(form_code)

    def accept(self, request, logger=None, additional_feed_dictionary=None,
               translation_processor=None):
        ''' This is a single point of entry for all SMS based workflows, we do not have  a separation on the view layer for different sms
        workflows, hence we will be branching to different methods here. Current implementation does the parse twice but that will go away
        once the entity registration is separated '''
        form_model = self.get_form_model(request)
        if form_model.is_entity_registration_form() or form_model.form_code == ENTITY_DELETION_FORM_CODE:
            return self.entity_api(request, logger)
        sms_player_v2 = SMSPlayerV2(self.dbm, post_sms_parser_processors=self.post_sms_parser_processor,
            feeds_dbm=self.feeds_dbm)
        return sms_player_v2.add_survey_response(request, logger, additional_feed_dictionary,
                                                 translation_processor=translation_processor)

    def entity_api(self, request, logger):
        form_code, values, extra_elements = self._parse(request.message)
        post_sms_processor_response = self._process_post_parse_callback(form_code, values, extra_elements)

        log_entry = "message:message " + repr(request.message) + "|source: " + request.transport.source + "|"

        if post_sms_processor_response is not None:
            if logger is not None:
                log_entry += "Status: False"
                logger.info(log_entry)
            post_sms_processor_response.is_registration = True
            return post_sms_processor_response

        reporter_entity = reporters.find_reporter_entity(self.dbm, request.transport.source)
        form_model, values = self._process(values, form_code, reporter_entity)
        reporter_entity_names = [{NAME_FIELD: reporter_entity.value(NAME_FIELD)}]
        response = self.submit(form_model, values, reporter_entity_names)
        if logger is not None:
            log_entry += "Status: True" if response.success else "Status: False"
            logger.info(log_entry)
        return response