Пример #1
0
    def testSMSLogSync(self):
        self.deleteAllLogs()
        self.assertEqual(self.getSMSLogCount(), 0)
        self.assertEqual(self.getSMSCount(), 0)

        # Test Create
        smslog = SMSLog()
        self.setRandomSMSLogValues(smslog)
        smslog.save()

        sleep(1)
        self.assertEqual(self.getSMSLogCount(), 1)
        self.assertEqual(self.getSMSCount(), 1)

        sms = SMS.objects.get(couch_id=smslog._id)
        self.checkFieldValues(smslog, sms, SMSLog._migration_get_fields())
        self.assertTrue(SMSLog.get_db().get_rev(smslog._id).startswith('1-'))

        # Test Update
        self.setRandomSMSLogValues(smslog)
        smslog.save()

        sleep(1)
        self.assertEqual(self.getSMSLogCount(), 1)
        self.assertEqual(self.getSMSCount(), 1)
        sms = SMS.objects.get(couch_id=smslog._id)
        self.checkFieldValues(smslog, sms, SMSLog._migration_get_fields())
        self.assertTrue(SMSLog.get_db().get_rev(smslog._id).startswith('2-'))
Пример #2
0
    def testSMSLogSync(self):
        prev_couch_count = self.getSMSLogCount()
        prev_sql_count = self.getSMSCount()

        # Test Create
        smslog = SMSLog()
        self.setRandomSMSLogValues(smslog)
        smslog.save()

        sleep(1)
        self.assertEqual(self.getSMSLogCount(), prev_couch_count + 1)
        self.assertEqual(self.getSMSCount(), prev_sql_count + 1)

        sms = SMS.objects.get(couch_id=smslog._id)
        self.checkFieldValues(smslog, sms, SMSLog._migration_get_fields())
        self.assertTrue(SMSLog.get_db().get_rev(smslog._id).startswith('1-'))

        # Test Update
        self.setRandomSMSLogValues(smslog)
        smslog.save()

        sleep(1)
        self.assertEqual(self.getSMSLogCount(), prev_couch_count + 1)
        self.assertEqual(self.getSMSCount(), prev_sql_count + 1)
        sms = SMS.objects.get(couch_id=smslog._id)
        self.checkFieldValues(smslog, sms, SMSLog._migration_get_fields())
        self.assertTrue(SMSLog.get_db().get_rev(smslog._id).startswith('2-'))
Пример #3
0
    def tearDown(self):
        SmsBillable.objects.all().delete()
        SmsGatewayFee.objects.all().delete()
        SmsGatewayFeeCriteria.objects.all().delete()
        SmsUsageFee.objects.all().delete()
        SmsUsageFeeCriteria.objects.all().delete()

        self.currency_usd.delete()
        self.other_currency.delete()
        SMSLog.get_db().delete_docs(
            SMSLog.by_domain_asc(generator.TEST_DOMAIN).all())

        SMSBackend.get_db().delete_docs(SMSBackend.get_db().all_docs(
            keys=self.backend_ids.values(), include_docs=True).all())
Пример #4
0
    def tearDown(self):
        SmsBillable.objects.all().delete()
        SmsGatewayFee.objects.all().delete()
        SmsGatewayFeeCriteria.objects.all().delete()
        SmsUsageFee.objects.all().delete()
        SmsUsageFeeCriteria.objects.all().delete()

        self.currency_usd.delete()
        self.other_currency.delete()
        SMSLog.get_db().delete_docs(
            SMSLog.by_domain_asc(generator.TEST_DOMAIN).all()
        )

        for api_id, backend_id in self.backend_ids.iteritems():
            SQLMobileBackend.load(backend_id, is_couch_id=True).delete()
Пример #5
0
def _sms_count(user, startdate, enddate, message_type='SMSLog'):
    """
    Returns a dictionary of messages seen for a given type, user, and date
    range of the format:
    {
        I: inbound_count,
        O: outbound_count
    }
    """
    # utilizable if we want to stick it somewhere else
    start_timestamp = json_format_datetime(startdate)
    end_timestamp = json_format_datetime(enddate)
    ret = {}
    for direction in [INCOMING, OUTGOING]:
        results = SMSLog.get_db().view("sms/by_recipient",
                                       startkey=[
                                           user.doc_type, user._id,
                                           message_type, direction,
                                           start_timestamp
                                       ],
                                       endkey=[
                                           user.doc_type, user._id,
                                           message_type, direction,
                                           end_timestamp
                                       ],
                                       reduce=True).all()
        ret[direction] = results[0]['value'] if results else 0

    return ret
    def handle(self, *args, **options):
        num_sms = 0

        start_datetime = datetime.datetime(*str_to_int_tuple(args[0:6]))
        end_datetime = datetime.datetime(*str_to_int_tuple(args[6:12]))

        for domain in Domain.get_all():
            key = [domain.name, 'SMSLog']
            sms_docs = SMSLog.get_db().view('sms/by_domain',
                                            reduce=False,
                                            startkey=key + [start_datetime.isoformat()],
                                            endkey=key + [end_datetime.isoformat(), {}],
            )

            for sms_doc in sms_docs:
                sms_log = SMSLog.get(sms_doc['id'])
                if options.get('create', False):
                    SmsBillable.create(sms_log)
                    print 'Created billable for SMSLog %s in domain %s from %s' \
                          % (sms_doc['id'], domain.name, sms_log.date)
                else:
                    print 'Found SMSLog %s in domain %s from %s' \
                          % (sms_doc['id'], domain.name, sms_log.date)
                num_sms += 1

        print 'Number of SMSs in datetime range: %d' % num_sms
Пример #7
0
def _sms_count(user, startdate, enddate, message_type="SMSLog"):
    """
    Returns a dictionary of messages seen for a given type, user, and date
    range of the format:
    {
        I: inbound_count,
        O: outbound_count
    }
    """
    # utilizable if we want to stick it somewhere else
    start_timestamp = json_format_datetime(startdate)
    end_timestamp = json_format_datetime(enddate)
    ret = {}
    for direction in [INCOMING, OUTGOING]:
        results = (
            SMSLog.get_db()
            .view(
                "sms/by_recipient",
                startkey=[user.doc_type, user._id, message_type, direction, start_timestamp],
                endkey=[user.doc_type, user._id, message_type, direction, end_timestamp],
                reduce=True,
            )
            .all()
        )
        ret[direction] = results[0]["value"] if results else 0

    return ret
Пример #8
0
 def handle(self, *args, **options):
     billables_created = 0
     for domain in Domain.get_all():
         key = [domain.name, 'SMSLog']
         start_date = [datetime.datetime(2014, 1, 1).isoformat()]
         end_date = [datetime.datetime(2014, 1, 24).isoformat()]
         sms_docs = SMSLog.get_db().view('sms/by_domain',
                                         reduce=False,
                                         startkey=key + start_date,
                                         endkey=key + end_date + [{}])
         for sms_doc in sms_docs:
             sms_log = SMSLog.get(sms_doc['id'])
             try:
                 if sms_log.phone_number is not None:
                     parse_phone_number(sms_log.phone_number)
             except PhoneNumberParseException:
                 billables = SmsBillable.objects.filter(log_id=sms_log._id)
                 if len(billables) == 0:
                     SmsBillable.create(sms_log)
                     billables_created += 1
                     print 'created SmsBillable for invalid number %s in domain %s, id=%s'\
                           % (sms_log.phone_number, domain.name, sms_log._id)
                 elif len(billables) > 1:
                     print "Warning: >1 SmsBillable exists for SMSLog with id=%" % sms_log._id
     print 'Number of SmsBillables created: %d' % billables_created
     print 'Completed retrobilling.'
Пример #9
0
    def testFRISMSLogSync(self):
        prev_couch_count = self.getSMSLogCount()
        prev_sql_count = self.getSMSCount()

        # Test Create
        smslog = SMSLog()
        self.setRandomSMSLogValues(smslog)
        smslog.save()

        sleep(1)
        smslog = FRISMSLog.get(smslog._id)
        self.setRandomFRISMSLogValues(smslog)
        smslog.save()

        sleep(1)
        self.assertEqual(self.getSMSLogCount(), prev_couch_count + 1)
        self.assertEqual(self.getSMSCount(), prev_sql_count + 1)

        sms = SMS.objects.get(couch_id=smslog._id)
        self.checkFieldValues(smslog, sms, FRISMSLog._migration_get_fields())
        self.assertTrue(FRISMSLog.get_db().get_rev(smslog._id).startswith('2-'))

        # Test Update
        self.setRandomSMSLogValues(smslog)
        self.setRandomFRISMSLogValues(smslog)
        smslog.save()

        sleep(1)
        self.assertEqual(self.getSMSLogCount(), prev_couch_count + 1)
        self.assertEqual(self.getSMSCount(), prev_sql_count + 1)
        sms = SMS.objects.get(couch_id=smslog._id)
        self.checkFieldValues(smslog, sms, FRISMSLog._migration_get_fields())
        self.assertTrue(SMSLog.get_db().get_rev(smslog._id).startswith('3-'))
    def handle(self, *labels, **options):
        db = SMSLog.get_db()

        # active_domains = db.view(
        #     "sms/by_domain",
        #     reduce=True,
        #     group_level=1,
        # ).all()
        # active_domains = [d['key'][0] for d in active_domains]
        active_domains = ['pathfinder']
        startkey = lambda d: [d, "SMSLog", "2013-08-01"]
        endkey = lambda d: [d, "SMSLog", "2013-10-15"]

        for domain in active_domains:
            data = db.view(
                "sms/by_domain",
                reduce=False,
                startkey=startkey(domain),
                endkey=endkey(domain),
            ).all()
            sms_ids = [d['id'] for d in data]
            for doc in iter_docs(db, sms_ids):
                sms_log = SMSLog.wrap(doc)
                if not sms_log.billed and sms_log.backend_api in [
                    'MACH',
                    # 'TROPO',
                    # 'UNICEL',
                ]:
                    # we're going to assume the SMS messages were sent successfully
                    # at the time they were actually sent
                    successful_responses = {
                        'MACH': "MACH RESPONSE +OK 01 message queued (dest=%s)" % sms_log.phone_number,
                        'TROPO': "<success>true</success>",
                        'UNICEL': "success",
                    }
                    print "Retroactively billing SMLog %s in domain %s" % (sms_log._id, sms_log.domain)
                    try:
                        create_billable_for_sms(
                            sms_log,
                            sms_log.backend_api,
                            delay=False,
                            response=successful_responses[sms_log.backend_api],
                        )
                    except Exception as e:
                        print "Retroactive bill was not successful due to error: %s" % e
                        logging.exception(e)
    def delete_models(self, delete_interval):
        print 'Deleting SMSLogs...'
        count = iter_bulk_delete_with_doc_type_verification(SMSLog.get_db(), self.get_sms_couch_ids(), 'SMSLog',
            wait_time=delete_interval, max_fetch_attempts=5)
        print 'Deleted %s documents' % count

        print 'Deleting CallLogs...'
        count = iter_bulk_delete_with_doc_type_verification(CallLog.get_db(), self.get_call_couch_ids(), 'CallLog',
            wait_time=delete_interval, max_fetch_attempts=5)
        print 'Deleted %s documents' % count

        print 'Deleting ExpectedCallbackEventLogs...'
        count = iter_bulk_delete_with_doc_type_verification(ExpectedCallbackEventLog.get_db(),
            self.get_callback_couch_ids(), 'ExpectedCallbackEventLog', wait_time=delete_interval,
            max_fetch_attempts=5)
        print 'Deleted %s documents' % count

        print 'Deleting LastReadMessages...'
        count = iter_bulk_delete_with_doc_type_verification(LastReadMessage.get_db(),
            self.get_lastreadmessage_couch_ids(), 'LastReadMessage', wait_time=delete_interval,
            max_fetch_attempts=5)
        print 'Deleted %s documents' % count
Пример #12
0
    def delete_models(self, delete_interval):
        print('Deleting SMSLogs...')
        count = iter_bulk_delete_with_doc_type_verification(SMSLog.get_db(), self.get_sms_couch_ids(), 'SMSLog',
            wait_time=delete_interval, max_fetch_attempts=5)
        print('Deleted %s documents' % count)

        print('Deleting CallLogs...')
        count = iter_bulk_delete_with_doc_type_verification(CallLog.get_db(), self.get_call_couch_ids(), 'CallLog',
            wait_time=delete_interval, max_fetch_attempts=5)
        print('Deleted %s documents' % count)

        print('Deleting ExpectedCallbackEventLogs...')
        count = iter_bulk_delete_with_doc_type_verification(ExpectedCallbackEventLog.get_db(),
            self.get_callback_couch_ids(), 'ExpectedCallbackEventLog', wait_time=delete_interval,
            max_fetch_attempts=5)
        print('Deleted %s documents' % count)

        print('Deleting LastReadMessages...')
        count = iter_bulk_delete_with_doc_type_verification(LastReadMessage.get_db(),
            self.get_lastreadmessage_couch_ids(), 'LastReadMessage', wait_time=delete_interval,
            max_fetch_attempts=5)
        print('Deleted %s documents' % count)