Example #1
0
        def load_messages(message_file):
            print "loading messages from %s" % message_file
            print "started"
            with open(message_file, 'r') as f:
                reader = unicode_csv_reader(f, delimiter=',', quotechar='"')
                inbound_count = outbound_count = 0
                inbound_max = outbound_max = 9999999999
                for row in reader:
                    pk1, pk2, pk3, dir, timestamp, text, phone = row
                    parsed_date = string_to_datetime(timestamp)
                    if dir == "I":
                        #print "%s: %s (%s)" % (phone, text, timestamp)
                        inbound_count = inbound_count + 1
                        try:
                            utils.send_test_message(identity=phone,
                                                    text=text,
                                                    timestamp=timestamp)
                        except RouterError, e:
                            print e.code
                            print e.content_type
                            print e.response
                            raise

                        if inbound_count % 100 == 0:
                            print "processed %s inbound and %s outbound messages." % (
                                inbound_count, outbound_count)
                    elif dir == "O":
                        status_type, status_value = guess_status_from_outbound_message(
                            text)
                        if status_type and status_value:
                            outbound_count = outbound_count + 1

                            # this is super janky, but we'll live with it
                            # hack it so that outbound reminders generate the
                            # appropriate supply point statuses in the db
                            notset = False
                            try:
                                connection = Connection.objects.get(
                                    identity=phone, backend__name="migration")
                                if connection.contact and connection.contact.supply_point:
                                    SupplyPointStatus.objects.create(
                                        status_type=status_type,
                                        status_value=status_value,
                                        status_date=parsed_date,
                                        supply_point=connection.contact.
                                        supply_point)
                                else:
                                    notset = True
                            except Connection.DoesNotExist:
                                notset = True
                            if notset:
                                print "No connection, contact, or supply point found for %s, so no status saved" % phone

                    if inbound_count >= inbound_max:
                        break
                    if outbound_count >= outbound_max:
                        break
Example #2
0
 def load_messages(message_file):
     print "loading messages from %s" % message_file
     print "started"
     with open(message_file, 'r') as f:
         reader = unicode_csv_reader(f, delimiter=',', quotechar='"')
         inbound_count = outbound_count = 0
         inbound_max = outbound_max = 9999999999
         for row in reader:
             pk1, pk2, pk3, dir, timestamp, text, phone = row
             parsed_date = string_to_datetime(timestamp)
             if dir == "I":
                 #print "%s: %s (%s)" % (phone, text, timestamp)
                 inbound_count = inbound_count + 1
                 try:
                     utils.send_test_message(identity=phone,
                                             text=text,
                                             timestamp=timestamp)
                 except RouterError, e:
                     print e.code
                     print e.content_type
                     print e.response
                     raise
                     
                 if inbound_count % 100 == 0:
                     print "processed %s inbound and %s outbound messages." % (inbound_count, outbound_count)
             elif dir == "O":
                 status_type, status_value = guess_status_from_outbound_message(text)
                 if status_type and status_value:
                     outbound_count = outbound_count + 1
                 
                     # this is super janky, but we'll live with it
                     # hack it so that outbound reminders generate the
                     # appropriate supply point statuses in the db
                     notset = False
                     try:
                         connection = Connection.objects.get(identity=phone,
                                                             backend__name="migration")
                         if connection.contact and connection.contact.supply_point:
                             SupplyPointStatus.objects.create(status_type=status_type,
                                                              status_value=status_value,
                                                              status_date=parsed_date,
                                                              supply_point=connection.contact.supply_point)
                         else:
                             notset = True
                     except Connection.DoesNotExist:
                         notset = True
                     if notset:
                         print "No connection, contact, or supply point found for %s, so no status saved" % phone
                     
             if inbound_count >= inbound_max:
                 break
             if outbound_count >= outbound_max:
                 break
Example #3
0
 def load_push_messages(message_file):
     print "loading messages from %s" % message_file
     print "started"
     migration_backend = Backend.objects.get_or_create(name="migration")[0]
     with open(message_file, 'r') as f:
         reader = unicode_csv_reader(f, delimiter=',', quotechar='"')
         inbound_count = -1
         max = 9999999999
         for row in reader:
             inbound_count = inbound_count + 1
             if inbound_count == 0:
                 continue # ignore the first line of headers
             
             msg, number, timestamp = row
             
             # if we already have a registered contact here
             # then make sure we associate them with the migration
             # backend before pumping the message through
             try: 
                 push_conn = Connection.objects.get(backend__name="push_backend", 
                                                    identity=number)
                 push_conn.backend = migration_backend
                 push_conn.save()
             except Connection.DoesNotExist:
                 # maybe they're newly created, no problem
                 pass
                 
             parsed_date = datetime.strptime(timestamp, "%d/%m/%Y %H:%M:%S")
             try:
                 utils.send_test_message(identity=number,
                                         text=msg,
                                         timestamp=str(parsed_date))
             except RouterError, e:
                 print e.code
                 print e.content_type
                 print e.response
                 raise
                 
             if inbound_count % 100 == 0:
                 print "processed %s inbound messages." % (inbound_count)
             if inbound_count >= max:
                 break