def process_outgoing_phases(self, outgoing): """ Passes the passed in message through the outgoing phase for all our configured SMS apps. Apps have the opportunity to cancel messages in this phase by returning False when called with the message. In that case this method will also return False """ # create a RapidSMS outgoing message try: outgoing.connection[0] connections = [outgoing.connection] except TypeError: connections = outgoing.connection msg = OutgoingMessage(connections, outgoing.text.replace('%','%%')) msg.db_message = outgoing send_msg = True for phase in self.outgoing_phases: logger.debug("Out %s phase" % phase) # call outgoing phases in the opposite order of the incoming # phases, so the first app called with an incoming message # is the last app called with an outgoing message for app in reversed(self.apps): logger.debug("Out %s app" % app) try: func = getattr(app, phase) keep_sending = func(msg) # we have to do things this way because by default apps return # None from outgoing() if keep_sending is False: send_msg = False except Exception, err: app.exception() # during any outgoing phase, an app can return True to # abort ALL further processing of this message if not send_msg: outgoing.status = 'C' outgoing.save() logger.warning("Message cancelled") send_msg = False break
def process_outgoing_phases(self, outgoing): """ Passes the passed in message through the outgoing phase for all our configured SMS apps. Apps have the opportunity to cancel messages in this phase by returning False when called with the message. In that case this method will also return False """ # create a RapidSMS outgoing message msg = OutgoingMessage(outgoing.connection, outgoing.text.replace('%', '%%')) msg.db_message = outgoing send_msg = True for phase in self.outgoing_phases: self.debug("Out %s phase" % phase) # call outgoing phases in the opposite order of the incoming # phases, so the first app called with an incoming message # is the last app called with an outgoing message for app in reversed(self.apps): self.debug("Out %s app" % app) try: func = getattr(app, phase) keep_sending = func(msg) # we have to do things this way because by default apps return # None from outgoing() if keep_sending is False: send_msg = False except Exception, err: app.exception() # during any outgoing phase, an app can return True to # abort ALL further processing of this message if not send_msg: outgoing.status = 'C' outgoing_db_lock.acquire() outgoing.save() outgoing_db_lock.release() self.warning("Message cancelled") send_msg = False break
def queue_messages_task(): """ Queue batched messages TODO: Ensure that batches that were queued before are taken care of first """ from .models import Message, MessageBatch from rapidsms.messages.outgoing import OutgoingMessage batches = MessageBatch.objects.filter(status='Q') for batch in batches: try: messages = Message.objects.filter(batch=batch, status='P', direction='O')[0:settings.CHUNK_SIZE] # create a RapidSMS outgoing message for outgoing in messages: msg = OutgoingMessage(outgoing.connection, outgoing.text.replace('%','%%')) msg.db_message = outgoing if msg: outgoing.status = 'Q' outgoing.save() except IndexError: pass