def setUp(self):
     self.domain = Domain(name='mockdomain')
     self.domain.save()
     SMS.by_domain(self.domain.name).delete()
     self.user = '******'
     self.password = '******'
     self.number = 5555551234
     self.couch_user = WebUser.create(self.domain.name, self.user, self.password)
     self.couch_user.add_phone_number(self.number)
     self.couch_user.save()
     self.message_ascii = 'It Works'
     self.message_utf_hex = '0939093F0928094D092609400020091509300924093E00200939094800200907093800200938092E092F00200915093E092E002009390948003F'
 def setUp(self):
     self.domain = Domain(name='mockdomain')
     self.domain.save()
     SMS.by_domain(self.domain.name).delete()
     self.user = '******'
     self.password = '******'
     self.number = 5555551234
     self.couch_user = WebUser.create(self.domain.name, self.user, self.password)
     self.couch_user.add_phone_number(self.number)
     self.couch_user.save()
     self.message_ascii = 'It Works'
     self.message_utf_hex = '0939093F0928094D092609400020091509300924093E00200939094800200907093800200938092E092F00200915093E092E002009390948003F'
    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()
        SMS.by_domain(generator.TEST_DOMAIN).delete()

        for api_id, backend_id in self.backend_ids.iteritems():
            SQLMobileBackend.load(backend_id, is_couch_id=True).delete()
    def rows(self):
        data = SMS.by_domain(self.domain,
                             start_date=self.datespan.startdate_utc,
                             end_date=self.datespan.enddate_utc).filter(
                                 workflow__iexact=WORKFLOW_DEFAULT).exclude(
                                     direction=OUTGOING,
                                     processed=False,
                                 ).order_by('date')
        result = []

        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }
        reporting_locations_id = self.get_location_filter()

        contact_cache = {}
        for message in data:
            if reporting_locations_id and message.location_id not in reporting_locations_id:
                continue

            doc_info = self.get_recipient_info(message, contact_cache)
            phone_number = message.phone_number
            timestamp = ServerTime(message.date).user_time(
                self.timezone).done()
            result.append([
                self._fmt_timestamp(timestamp),
                self._fmt_contact_link(message, doc_info),
                _fmt(phone_number),
                _fmt(direction_map.get(message.direction, "-")),
                _fmt(message.text)
            ])

        return result
    def handle(self, *args, **options):
        if len(args) == 0:
            raise CommandError("Usage: python manage.py fix_smslog_recipient_doc_type <domain1 domain2 ...>")

        for domain in args:
            print "*** Processing Domain %s ***" % domain
            user_cache = {}
            for msg in SMS.by_domain(domain):
                if msg.couch_recipient:
                    if msg.couch_recipient_doc_type != "CommCareCase":
                        user = None
                        if msg.couch_recipient in user_cache:
                            user = user_cache[msg.couch_recipient]
                        else:
                            try:
                                user = CouchUser.get_by_user_id(msg.couch_recipient)
                            except Exception:
                                user = None
                            if user is None:
                                print "Could not find user %s" % msg.couch_recipient
                        user_cache[msg.couch_recipient] = user
                        if user and msg.couch_recipient_doc_type != user.doc_type:
                            msg.couch_recipient_doc_type = user.doc_type
                            msg.save()
                else:
                    if msg.couch_recipient_doc_type is not None or msg.couch_recipient is not None:
                        msg.couch_recipient = None
                        msg.couch_recipient_doc_type = None
                        msg.save()
 def handle(self, domains, **options):
     for domain in domains:
         print("*** Processing Domain %s ***" % domain)
         user_cache = {}
         for msg in SMS.by_domain(domain):
             if msg.couch_recipient:
                 if msg.couch_recipient_doc_type != "CommCareCase":
                     user = None
                     if msg.couch_recipient in user_cache:
                         user = user_cache[msg.couch_recipient]
                     else:
                         try:
                             user = CouchUser.get_by_user_id(msg.couch_recipient)
                         except Exception:
                             user = None
                         if user is None:
                             print("Could not find user %s" % msg.couch_recipient)
                     user_cache[msg.couch_recipient] = user
                     if user and msg.couch_recipient_doc_type != user.doc_type:
                         msg.couch_recipient_doc_type = user.doc_type
                         msg.save()
             else:
                 if msg.couch_recipient_doc_type is not None or msg.couch_recipient is not None:
                     msg.couch_recipient = None
                     msg.couch_recipient_doc_type = None
                     msg.save()
 def handle(self, domains, **options):
     for domain in domains:
         print("*** Processing Domain %s ***" % domain)
         user_cache = {}
         for msg in SMS.by_domain(domain):
             if msg.couch_recipient:
                 if msg.couch_recipient_doc_type != "CommCareCase":
                     user = None
                     if msg.couch_recipient in user_cache:
                         user = user_cache[msg.couch_recipient]
                     else:
                         try:
                             user = CouchUser.get_by_user_id(
                                 msg.couch_recipient)
                         except Exception:
                             user = None
                         if user is None:
                             print("Could not find user %s" %
                                   msg.couch_recipient)
                     user_cache[msg.couch_recipient] = user
                     if user and msg.couch_recipient_doc_type != user.doc_type:
                         msg.couch_recipient_doc_type = user.doc_type
                         msg.save()
             else:
                 if msg.couch_recipient_doc_type is not None or msg.couch_recipient is not None:
                     msg.couch_recipient = None
                     msg.couch_recipient_doc_type = None
                     msg.save()
    def handle(self, *args, **options):
        if len(args) == 0:
            raise CommandError(
                "Usage: python manage.py fix_smslog_recipient_doc_type <domain1 domain2 ...>"
            )

        for domain in args:
            print "*** Processing Domain %s ***" % domain
            user_cache = {}
            for msg in SMS.by_domain(domain):
                if msg.couch_recipient:
                    if msg.couch_recipient_doc_type != "CommCareCase":
                        user = None
                        if msg.couch_recipient in user_cache:
                            user = user_cache[msg.couch_recipient]
                        else:
                            try:
                                user = CouchUser.get_by_user_id(
                                    msg.couch_recipient)
                            except Exception:
                                user = None
                            if user is None:
                                print "Could not find user %s" % msg.couch_recipient
                        user_cache[msg.couch_recipient] = user
                        if user and msg.couch_recipient_doc_type != user.doc_type:
                            msg.couch_recipient_doc_type = user.doc_type
                            msg.save()
                else:
                    if msg.couch_recipient_doc_type is not None or msg.couch_recipient is not None:
                        msg.couch_recipient = None
                        msg.couch_recipient_doc_type = None
                        msg.save()
Esempio n. 9
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()
        SMS.by_domain(generator.TEST_DOMAIN).delete()

        for api_id, backend_id in six.iteritems(self.backend_ids):
            SQLMobileBackend.load(backend_id, is_couch_id=True).delete()

        FakeTwilioMessageFactory.backend_message_id_to_price = {}

        super(TestGatewayFee, self).tearDown()
Esempio n. 10
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()
        SMS.by_domain(generator.TEST_DOMAIN).delete()

        for api_id, backend_id in self.backend_ids.iteritems():
            SQLMobileBackend.load(backend_id, is_couch_id=True).delete()

        FakeTwilioMessageFactory.backend_message_id_to_price = {}

        super(TestGatewayFee, self).tearDown()
Esempio n. 11
0
    def rows(self):
        data = SMS.by_domain(
            self.domain,
            start_date=self.datespan.startdate_utc,
            end_date=self.datespan.enddate_utc
        ).exclude(
            direction=OUTGOING,
            processed=False
        ).order_by('date')

        if self.show_only_survey_traffic():
            data = data.filter(
                xforms_session_couch_id__isnull=False
            )

        result = []
        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }
        message_bank_messages = get_message_bank(self.domain, for_comparing=True)

        FormProcessorInterface(self.domain).casedb_cache(
            domain=self.domain, strip_history=False, deleted_ok=True
        )
        user_cache = UserCache()

        for message in data:
            # Add metadata from the message bank if it has not been added already
            if (message.direction == OUTGOING) and (not message.fri_message_bank_lookup_completed):
                add_metadata(message, message_bank_messages)

            if message.couch_recipient_doc_type == "CommCareCase":
                recipient = case_cache.get(message.couch_recipient)
            else:
                recipient = user_cache.get(message.couch_recipient)

            if message.chat_user_id:
                sender = user_cache.get(message.chat_user_id)
            else:
                sender = None

            study_arm = None
            if message.couch_recipient_doc_type == "CommCareCase":
                study_arm = case_cache.get(message.couch_recipient).get_case_property("study_arm")

            timestamp = ServerTime(message.date).user_time(self.timezone).done()
            result.append([
                self._fmt(self._participant_id(recipient)),
                self._fmt(study_arm or "-"),
                self._fmt(self._originator(message, recipient, sender)),
                self._fmt_timestamp(timestamp),
                self._fmt(message.text),
                self._fmt(message.fri_id or "-"),
                self._fmt(direction_map.get(message.direction,"-")),
            ])
        return result
Esempio n. 12
0
    def rows(self):
        data = SMS.by_domain(self.domain,
                             start_date=self.datespan.startdate_utc,
                             end_date=self.datespan.enddate_utc).exclude(
                                 direction=OUTGOING,
                                 processed=False).order_by('date')

        if self.show_only_survey_traffic():
            data = data.filter(xforms_session_couch_id__isnull=False)

        result = []
        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }
        message_bank_messages = get_message_bank(self.domain,
                                                 for_comparing=True)

        FormProcessorInterface(self.domain).casedb_cache(domain=self.domain,
                                                         strip_history=False,
                                                         deleted_ok=True)
        user_cache = UserCache()

        for message in data:
            # Add metadata from the message bank if it has not been added already
            if (message.direction == OUTGOING) and (
                    not message.fri_message_bank_lookup_completed):
                add_metadata(message, message_bank_messages)

            if message.couch_recipient_doc_type == "CommCareCase":
                recipient = case_cache.get(message.couch_recipient)
            else:
                recipient = user_cache.get(message.couch_recipient)

            if message.chat_user_id:
                sender = user_cache.get(message.chat_user_id)
            else:
                sender = None

            study_arm = None
            if message.couch_recipient_doc_type == "CommCareCase":
                study_arm = case_cache.get(
                    message.couch_recipient).get_case_property("study_arm")

            timestamp = ServerTime(message.date).user_time(
                self.timezone).done()
            result.append([
                self._fmt(self._participant_id(recipient)),
                self._fmt(study_arm or "-"),
                self._fmt(self._originator(message, recipient, sender)),
                self._fmt_timestamp(timestamp),
                self._fmt(message.text),
                self._fmt(message.fri_id or "-"),
                self._fmt(direction_map.get(message.direction, "-")),
            ])
        return result
Esempio n. 13
0
 def rows(self):
     data = (SMS.by_domain(self.config['domain'])
             .filter(location_id=self.config['location_id'])
             .order_by('date'))
     messages = []
     for message in data:
         timestamp = ServerTime(message.date).user_time(self.config['timezone']).done()
         messages.append([
             _fmt_timestamp(timestamp),
             _fmt(message.direction),
             _fmt(message.text),
         ])
     return messages
Esempio n. 14
0
 def rows(self):
     data = (SMS.by_domain(self.config['domain']).filter(
         location_id=self.config['location_id']).exclude(
             processed=False, direction=OUTGOING).order_by('-date'))
     messages = []
     for message in data:
         recipient = message.recipient
         timestamp = ServerTime(message.date).user_time(
             self.config['timezone']).done()
         messages.append([
             _fmt_timestamp(timestamp),
             recipient.full_name,
             message.phone_number,
             _fmt(message.direction),
             _fmt(message.text),
         ])
     return messages
Esempio n. 15
0
 def rows(self):
     data = (SMS.by_domain(self.config['domain'])
             .filter(location_id=self.config['location_id'])
             .exclude(processed=False, direction=OUTGOING)
             .order_by('-date'))
     messages = []
     for message in data:
         recipient = message.recipient
         timestamp = ServerTime(message.date).user_time(self.config['timezone']).done()
         messages.append([
             _fmt_timestamp(timestamp),
             recipient.full_name,
             message.phone_number,
             _fmt(message.direction),
             _fmt(message.text),
         ])
     return messages
Esempio n. 16
0
    def handle(self, start_datetime, end_datetime, **options):
        num_sms = 0

        for domain in Domain.get_all():
            result = SMS.by_domain(
                domain.name,
                start_date=start_datetime,
                end_date=end_datetime,
            )

            for sms_log in result:
                if options.get('create', False):
                    SmsBillable.create(sms_log)
                    print('Created billable for SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date))
                else:
                    print('Found SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date))
                num_sms += 1

        print('Number of SMSs in datetime range: %d' % num_sms)
Esempio n. 17
0
    def handle(self, start_datetime, end_datetime, **options):
        num_sms = 0

        for domain in Domain.get_all():
            result = SMS.by_domain(
                domain.name,
                start_date=start_datetime,
                end_date=end_datetime,
            )

            for sms_log in result:
                if options.get('create', False):
                    SmsBillable.create(sms_log)
                    print('Created billable for SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date))
                else:
                    print('Found SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date))
                num_sms += 1

        print('Number of SMSs in datetime range: %d' % num_sms)
Esempio n. 18
0
    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():
            result = SMS.by_domain(domain.name,
                                   start_date=start_datetime,
                                   end_date=end_datetime)

            for sms_log in result:
                if options.get('create', False):
                    SmsBillable.create(sms_log)
                    print 'Created billable for SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date)
                else:
                    print 'Found SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date)
                num_sms += 1

        print 'Number of SMSs in datetime range: %d' % num_sms
Esempio n. 19
0
    def rows(self):
        data = SMS.by_domain(
            self.domain,
            start_date=self.datespan.startdate_utc,
            end_date=self.datespan.enddate_utc
        ).filter(
            workflow__iexact=WORKFLOW_DEFAULT
        ).exclude(
            direction=OUTGOING,
            processed=False,
        ).order_by('date')
        result = []

        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }
        reporting_locations_id = self.get_location_filter()

        contact_cache = {}
        for message in data:
            if reporting_locations_id and message.location_id not in reporting_locations_id:
                continue

            doc_info = self.get_recipient_info(message, contact_cache)
            phone_number = message.phone_number
            timestamp = ServerTime(message.date).user_time(self.timezone).done()
            result.append([
                self._fmt_timestamp(timestamp),
                self._fmt_contact_link(message, doc_info),
                _fmt(phone_number),
                _fmt(direction_map.get(message.direction, "-")),
                _fmt(message.text)
            ])

        return result
Esempio n. 20
0
    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():
            result = SMS.by_domain(
                domain.name,
                start_date=start_datetime,
                end_date=end_datetime
            )

            for sms_log in result:
                if options.get('create', False):
                    SmsBillable.create(sms_log)
                    print 'Created billable for SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date)
                else:
                    print 'Found SMS %s in domain %s from %s' \
                          % (sms_log.couch_id, domain.name, sms_log.date)
                num_sms += 1

        print 'Number of SMSs in datetime range: %d' % num_sms
Esempio n. 21
0
 def setUp(self):
     SMS.by_domain(self.domain.name).delete()
Esempio n. 22
0
 def setUp(self):
     SMS.by_domain(self.domain.name).delete()
     self.client = Client()
Esempio n. 23
0
 def setUp(self):
     SMS.by_domain(self.domain.name).delete()