Exemplo n.º 1
0
 def fakeIncoming(self, message, connection=None):
     if connection is None:
         connection = Connection.objects.all()[0]
     # if so, process it
     incomingmessage = IncomingMessage(connection, message)
     incomingmessage.db_message = Message.objects.create(direction='I', connection=connection, text=message)
     return incomingmessage
Exemplo n.º 2
0
 def fakeIncoming(self, message, connection=None):
     if connection is None:
         connection = Connection.objects.all()[0]
     # if so, process it
     incomingmessage = IncomingMessage(connection, message)
     incomingmessage.db_message = Message.objects.create(
         direction='I', connection=connection, text=message)
     return incomingmessage
Exemplo n.º 3
0
def fake_incoming_message(message, connection):
    incomingmessage = IncomingMessage(connection, message)
    #router = get_router()
    #router.handle_incoming(connection.backend.name, connection.identity, message)
    
    incomingmessage.db_message = Message.objects.create(direction='I', connection=connection, text=message)
    incomingmessage.db_message.handled_by = 'poll'
    return incomingmessage
Exemplo n.º 4
0
 def spoof_incoming_obj(self, message, connection=None):
     if connection is None:
         connection = Connection.objects.all()[0]
     incomingmessage = IncomingMessage(connection, message)
     incomingmessage.db_message = Message.objects.create(
         direction='I',
         connection=Connection.objects.all()[0],
         text=message)
     return incomingmessage
Exemplo n.º 5
0
    def fake_error_submission(self, message, connection=None):
        form = XForm.find_form(message)

        if connection is None:
            connection = Connection.objects.all()[0]
        # if so, process it
        incomingmessage = IncomingMessage(connection, message)
        incomingmessage.db_message = Message.objects.create(direction='I', connection=Connection.objects.all()[0], text=message)
        if form:
            submission = form.process_sms_submission(incomingmessage)
            self.failUnless(submission.has_errors)
        return
Exemplo n.º 6
0
 def fake_submission(self, message, connection=None):
     form = XForm.find_form(message)
     if connection is None:
         try:
             connection = Connection.objects.all()[0]
         except IndexError:
             backend, created = Backend.objects.get_or_create(name='test')
             connection, created = Connection.objects.get_or_create(identity='8675309',
                                                                    backend=backend)
     # if so, process it
     incomingmessage = IncomingMessage(connection, message)
     incomingmessage.db_message = Message.objects.create(direction='I', connection=connection, text=message)
     if form:
         submission = form.process_sms_submission(incomingmessage)
         return submission
     return None
Exemplo n.º 7
0
 def spoof_incoming_obj(self, message, connection=None):
     if connection is None:
         connection = Connection.objects.all()[0]
     incomingmessage = IncomingMessage(connection, message)
     incomingmessage.db_message = Message.objects.create(direction='I', connection=Connection.objects.all()[0], text=message)
     return incomingmessage
Exemplo n.º 8
0
def handle_incoming(router,backend, sender, text,ignore_result=True,**kwargs):
    """
        Handles an incoming message.
        """
    # create our db message for logging
    db_message = router.add_message(backend, sender, text, 'I', 'R')

    # and our rapidsms transient message for processing
    msg = IncomingMessage(db_message.connection, text, db_message.date)

    # add an extra property to IncomingMessage, so httprouter-aware
    # apps can make use of it during the handling phase
    msg.db_message = db_message

    router.info("SMS[%d] IN (%s) : %s" % (db_message.id, msg.connection, msg.text))
    try:
        for phase in router.incoming_phases:
            router.debug("In %s phase" % phase)
            if phase == "default":
                if msg.handled:
                    router.debug("Skipping phase")
                    break

            for app in router.apps:
                router.debug("In %s app" % app)
                handled = False

                try:
                    func = getattr(app, phase)
                    handled = func(msg)

                except Exception, err:
                    import traceback
                    traceback.print_exc(err)
                    app.exception()

                # during the _filter_ phase, an app can return True
                # to abort ALL further processing of this message
                if phase == "filter":
                    if handled is True:
                        router.warning("Message filtered")
                        raise(StopIteration)

                # during the _handle_ phase, apps can return True
                # to "short-circuit" this phase, preventing any
                # further apps from receiving the message
                elif phase == "handle":
                    if handled is True:
                        router.debug("Short-circuited")
                        # mark the message handled to avoid the
                        # default phase firing unnecessarily
                        msg.handled = True
                        db_message.application = app
                        db_message.save()
                        break

                elif phase == "default":
                    # allow default phase of apps to short circuit
                    # for prioritized contextual responses.
                    if handled is True:
                        router.debug("Short-circuited default")
                        break

    except StopIteration:
        pass

    db_message.status = 'H'
    db_message.save()

    db_responses = []

    # now send the message responses
    while msg.responses:
        response = msg.responses.pop(0)
        router.handle_outgoing(response, db_message, db_message.application)

    # we are no longer interested in this message... but some crazy
    # synchronous backends might be, so mark it as processed.
    msg.processed = True

    return db_message
Exemplo n.º 9
0
    def handle_incoming(self, backend, sender, text):
        """
        Handles an incoming message.
        """
        global outgoing_db_lock
        # create our db message for logging
        db_message = self.add_message(backend, sender, text, 'I', 'R')

        # and our rapidsms transient message for processing
        msg = IncomingMessage(db_message.connection, text, db_message.date)

        # add an extra property to IncomingMessage, so httprouter-aware
        # apps can make use of it during the handling phase
        msg.db_message = db_message

        self.info("SMS[%d] IN (%s) : %s" %
                  (db_message.id, msg.connection, msg.text))
        try:
            for phase in self.incoming_phases:
                self.debug("In %s phase" % phase)
                if phase == "default":
                    if msg.handled:
                        self.debug("Skipping phase")
                        break

                for app in self.apps:
                    self.debug("In %s app" % app)
                    handled = False

                    try:
                        func = getattr(app, phase)
                        handled = func(msg)

                    except Exception, err:
                        import traceback
                        traceback.print_exc(err)
                        app.exception()

                    # during the _filter_ phase, an app can return True
                    # to abort ALL further processing of this message
                    if phase == "filter":
                        if handled is True:
                            self.warning("Message filtered")
                            raise (StopIteration)

                    # during the _handle_ phase, apps can return True
                    # to "short-circuit" this phase, preventing any
                    # further apps from receiving the message
                    elif phase == "handle":
                        if handled is True:
                            self.debug("Short-circuited")
                            # mark the message handled to avoid the
                            # default phase firing unnecessarily
                            msg.handled = True
                            break

                    elif phase == "default":
                        # allow default phase of apps to short circuit
                        # for prioritized contextual responses.
                        if handled is True:
                            self.debug("Short-circuited default")
                            break

        except StopIteration:
            pass

        outgoing_db_lock.acquire()
        db_message.status = 'H'
        db_message.save()
        outgoing_db_lock.release()

        db_responses = []

        # now send the message responses
        while msg.responses:
            response = msg.responses.pop(0)
            self.handle_outgoing(response, db_message)

        # we are no longer interested in this message... but some crazy
        # synchronous backends might be, so mark it as processed.
        msg.processed = True

        return db_message
def _get_incoming_message(connection, message):
    incoming_message = IncomingMessage(connection, message)
    incoming_message.db_message = Message.objects.create(direction='I',
                                                         connection=connection,
                                                         text=message)
    return incoming_message
def _get_incoming_message(connection, message):
    incoming_message = IncomingMessage(connection, message)
    incoming_message.db_message = Message.objects.create(direction='I', connection=connection, text=message)
    return incoming_message