def register_from_message(modelBase, message): potential_registrant = Respondent.objects.filter(phone_number=message.connection.identity) if potential_registrant: return potential_registrant[0] else: # have to create a Reporter and PersistentConnection # TODO: spl = message.text.partition(" ") reporter = Reporter(alias=message.connection.identity, first_name=spl[0], last_name=spl[2]) reporter.save() be = PersistantBackend.from_message(message) be.save() conn = PersistantConnection.from_message(message) conn.reporter = reporter conn.save() conn.seen() resp = Respondent( phone_number=message.connection.identity, registered_at=datetime.now(), reporter=reporter, connection=conn) resp.save() return resp
def outgoing (self, message): # I'm going to assume that I've gotten the reporter's connection con = PersistantConnection.from_message(message) # determine if the user is due for a recharge time_period = time() - self._frequency try: last_airtime = AirtimePins.objects.filter(time_used__gt=strftime('%Y-%m-%d %H:%M:%S', localtime(time_period)), connection=con).latest('time_used') except (AirtimePins.DoesNotExist): try: # The user is due hence let's obtain a recharge pin airtime = AirtimePins.vend_airtime(con.identity) airtime.used = True airtime.time_used = datetime.now() airtime.connection = con airtime.save() msg_text = "Thanks for using RapidSMS. Please load this credit: %s" % airtime.pin msg = message.connection.backend.message(con.identity, msg_text) msg.send() except (AirtimePins.DoesNotExist): # There probably should be a more elegant way of handling this self.error("No airtime pins available!") pass
def _save_flat(self, message, incoming): if not hasattr(message, "persistant_connection"): conn = PersistantConnection.from_message(message) else: conn = message.persistant_connection persistent_msg = Message.objects.create(connection=conn, text=message.text, is_incoming=incoming) message.persistent_msg = persistent_msg self.debug(persistent_msg)
def outgoing (self, message): # I'm going to assume that I've gotten the reporter's connection if hasattr(message, 'is_airtime'): return True con = PersistantConnection.from_message(message) # determine if the user is due for a recharge time_period = time() - self._frequency try: last_airtime = AirtimePins.objects.filter(time_used__gt=strftime('%Y-%m-%d %H:%M:%S', localtime(time_period)), connection=con).latest('time_used') try: # The user is due hence let's obtain a recharge pin airtime = AirtimePins.vend_airtime(con.identity) airtime.used = True airtime.time_used = datetime.now() airtime.connection = con airtime.save() msg_text = "Thanks for using RapidSMS. Please load this credit: %s" % airtime.pin msg = message.connection.backend.message(con.identity, msg_text) # The is_airtime attribute/flag added to the msg object is required for airtime checks on outgoing messages on a call to outgoing method in order to avoid an infinite loop/call msg.is_airtime = True msg.send() except (AirtimePins.DoesNotExist): # There probably should be a more elegant way of handling this self.error("No airtime pins available!") pass except (AirtimePins.DoesNotExist): try: # The user is due hence let's obtain a recharge pin airtime = AirtimePins.vend_airtime(con.identity) airtime.used = True airtime.time_used = datetime.now() airtime.connection = con airtime.save() msg_text = "Thanks for using RapidSMS. Please load this credit: %s" % airtime.pin msg = message.connection.backend.message(con.identity, msg_text) # The is_airtime attribute/flag added to the msg object is required for airtime checks on outgoing messages on a call to outgoing method in order to avoid an infinite loop/call msg.is_airtime = True msg.send() except (AirtimePins.DoesNotExist): # There probably should be a more elegant way of handling this self.error("No airtime pins available!") pass return True
def _who(self, msg): return PersistantConnection.from_message(msg).dict