def handle(self, *args, **options): translation.activate(settings.DEFAULT_LOCALE) # Arguments are sender message if len(args) != 2: logger.warning(u"No message or senderID provided\n" \ u"Format is senderID \"message text\"") return False msg_sender = unicode(args[0]) msg_str = unicode(args[1]) message = Message(identity=msg_sender, \ text=msg_str, status=Message.STATUS_CREATED, \ direction=Message.DIRECTION_INCOMING) message.save() message.date = datetime.now() message.save() try: handler_func = import_path(settings.NOSMS_HANDLER) except AttributeError: message.status = Message.STATUS_ERROR message.save() logger.error(u"NO SMS_HANDLER defined while receiving SMS") except Exception as e: message.status = Message.STATUS_ERROR message.save() logger.error(u"Unable to call SMS_HANDLER with %r" % e) else: try: handler_func(message) except Exception as e: message.status = Message.STATUS_ERROR message.save() logger.error(u"SMS handler failed on %s with %r" \ % (message, e)) translation.deactivate()
def handle(self, *args, **options): translation.activate(settings.DEFAULT_LOCALE) # Message ID in DB is provided as first argument if len(args) != 1: #logger.warning(u"No message ID provided") print(u"WARN: No message ID provided") return False try: sql_id = int(args[0]) except: sql_id = None if not isinstance(sql_id, int): #logger.error(u"Provided ID (%s) is not an int." % sql_id) print(u"ERROR: Provided ID (%s) is not an int." % sql_id) return False # open up smsd DB cursor = connections['smsd'].cursor() cursor.execute("SELECT ReceivingDateTime, SenderNumber, " \ "TextDecoded FROM inbox WHERE " \ "ID = %s AND Processed = %s", [sql_id, 'false']) msg_data = dictfetchone(cursor) if not msg_data: #logger.warning(u"No unprocessed row in DB for ID %d" % sql_id) print(u"WARN: No unprocessed row in DB for ID %d" % sql_id) return False message = Message(identity=msg_data['SenderNumber'], \ text=msg_data['TextDecoded'], status=Message.STATUS_CREATED, \ direction=Message.DIRECTION_INCOMING) message.save() message.date = msg_data['ReceivingDateTime'] message.save() # for some reason it's buggy #process_incoming_message(message) try: handler_func = import_path(settings.NOSMS_HANDLER) except AttributeError: message.status = Message.STATUS_ERROR message.save() #logger.error(u"NO SMS_HANDLER defined while receiving SMS") print(u"ERROR: NO SMS_HANDLER defined while receiving SMS") except Exception as e: message.status = Message.STATUS_ERROR message.save() #logger.error(u"Unbale to call SMS_HANDLER with %r" % e) print(u"ERROR: Unbale to call SMS_HANDLER with %r" % e) else: try: #thread.start_new_thread(handler_func, (message,)) handler_func(message) except Exception as e: message.status = Message.STATUS_ERROR message.save() #logger.error(u"SMS handler failed on %s with %r" \ # % (message, e)) print(u"ERROR: SMS handler failed on %s with %r" \ % (message, e)) cursor.execute("UPDATE inbox SET Processed = 'true' " \ "WHERE ID = %s", [sql_id]) transaction.commit_unless_managed(using='smsd') translation.deactivate()