Esempio n. 1
0
    def test_get_json_payload_unicode(self, get_timestamp_mock):
        get_timestamp_mock.return_value = datetime(2018, 9, 7, 12, 34, 56)

        backend = AirtelTCLBackend()
        backend.set_extra_fields(
            user_name='abc',
            sender_id='def',
            circle_name='ghi',
            campaign_name='jkl',
        )

        msg_obj = QueuedSMS(text='\u0928\u092e\u0938\u094d\u0924\u0947',
                            phone_number='+916123456789')
        payload = backend.get_json_payload(msg_obj)
        self.assertEqual(
            payload, {
                'timeStamp':
                '07092018123456',
                'keyword':
                'ICDS',
                'dataSet': [{
                    'MSISDN': '6123456789',
                    'OA': 'def',
                    'CIRCLE_NAME': 'ghi',
                    'CAMPAIGN_NAME': 'jkl',
                    'MESSAGE': 'नमस्ते',
                    'USER_NAME': 'abc',
                    'CHANNEL': 'SMS',
                    'LANG_ID': '2',
                }],
            })
Esempio n. 2
0
    def test_get_json_payload_ascii(self, get_timestamp_mock):
        get_timestamp_mock.return_value = datetime(2018, 9, 7, 12, 34, 56)

        backend = AirtelTCLBackend()
        backend.set_extra_fields(
            user_name='abc',
            sender_id='def',
            circle_name='ghi',
            campaign_name='jkl',
        )

        msg_obj = QueuedSMS(text='message', phone_number='+910123456789')
        payload = backend.get_json_payload(msg_obj)
        self.assertEqual(
            payload, {
                'timeStamp':
                '07092018123456',
                'keyword':
                'ICDS',
                'dataSet': [{
                    'MSISDN': '0123456789',
                    'OA': 'def',
                    'CIRCLE_NAME': 'ghi',
                    'CAMPAIGN_NAME': 'jkl',
                    'MESSAGE': 'message',
                    'USER_NAME': 'abc',
                    'CHANNEL': 'SMS',
                }],
            })
Esempio n. 3
0
    def test_get_json_payload_ascii(self, get_timestamp_mock):
        get_timestamp_mock.return_value = datetime(2018, 9, 7, 12, 34, 56)

        backend = AirtelTCLBackend()
        backend.set_extra_fields(
            user_name='abc',
            sender_id='def',
            circle_name='ghi',
            campaign_name='jkl',
        )

        msg_obj = QueuedSMS(text='message', phone_number='+910123456789')
        payload = backend.get_json_payload(msg_obj)
        self.assertEqual(
            payload,
            {
                'timeStamp': '07092018123456',
                'keyword': 'ICDS',
                'dataSet': [
                    {
                        'MSISDN': '0123456789',
                        'OA': 'def',
                        'CIRCLE_NAME': 'ghi',
                        'CAMPAIGN_NAME': 'jkl',
                        'MESSAGE': 'message',
                        'USER_NAME': 'abc',
                        'CHANNEL': 'SMS',
                    }
                ],
            }
        )
Esempio n. 4
0
 def test_get_text_and_lang_id(self):
     self.assertEqual(
         AirtelTCLBackend.get_text_and_lang_id(QueuedSMS(text='abc')),
         ('abc', None))
     self.assertEqual(
         AirtelTCLBackend.get_text_and_lang_id(
             QueuedSMS(text='\u0928\u092e\u0938\u094d\u0924\u0947')),
         ('नमस्ते', '2'))
 def test_get_url(self):
     backend = AirtelTCLBackend()
     backend.set_extra_fields(
         host_and_port='localhost:8000',
     )
     self.assertEqual(
         backend.get_url(),
         'https://localhost:8000/BULK_API/InstantJsonPush'
     )
Esempio n. 6
0
 def test_get_text_and_lang_id(self):
     self.assertEqual(
         AirtelTCLBackend.get_text_and_lang_id(QueuedSMS(text='abc')),
         ('abc', None)
     )
     self.assertEqual(
         AirtelTCLBackend.get_text_and_lang_id(QueuedSMS(text='\u0928\u092e\u0938\u094d\u0924\u0947')),
         ('नमस्ते', '2')
     )
Esempio n. 7
0
 def test_get_url(self):
     backend = AirtelTCLBackend()
     backend.set_extra_fields(
         host_and_port='localhost:8000',
     )
     self.assertEqual(
         backend.get_url(),
         'https://localhost:8000/BULK_API/InstantJsonPush'
     )
Esempio n. 8
0
 def unicode_to_decimal_ncr(self):
     self.assertEqual(
         AirtelTCLBackend.unicode_to_decimal_ncr(
             '\u0928\u092e\u0938\u094d\u0924\u0947 \u0928\u092e\u0938\u094d\u0924\u0947'
         ),
         'नमस्ते नमस्ते'
     )
Esempio n. 9
0
 def unicode_to_decimal_ncr(self):
     self.assertEqual(
         AirtelTCLBackend.unicode_to_decimal_ncr(
             '\u0928\u092e\u0938\u094d\u0924\u0947 \u0928\u092e\u0938\u094d\u0924\u0947'
         ),
         'नमस्ते नमस्ते'
     )
Esempio n. 10
0
    def test_get_phone_number(self):
        self.assertEqual(AirtelTCLBackend.get_phone_number('+918123456789'), '8123456789')
        self.assertEqual(AirtelTCLBackend.get_phone_number('916123456789'), '6123456789')

        with self.assertRaises(InvalidDestinationNumber):
            AirtelTCLBackend.get_phone_number('+999123456')

        with self.assertRaises(InvalidDestinationNumber):
            AirtelTCLBackend.get_phone_number('+91')

        with self.assertRaises(InvalidDestinationNumber):
            AirtelTCLBackend.get_phone_number('+910123456789')
Esempio n. 11
0
    def test_get_phone_number(self):
        self.assertEqual(AirtelTCLBackend.get_phone_number('+910123456789'), '0123456789')
        self.assertEqual(AirtelTCLBackend.get_phone_number('910123456789'), '0123456789')

        with self.assertRaises(InvalidDestinationNumber):
            AirtelTCLBackend.get_phone_number('+999123456')

        with self.assertRaises(InvalidDestinationNumber):
            AirtelTCLBackend.get_phone_number('+91')
Esempio n. 12
0
    def test_handle_response(self):
        with self.assertRaises(AirtelTCLError):
            AirtelTCLBackend.handle_response(500, 'true')

        with self.assertRaises(AirtelTCLError):
            AirtelTCLBackend.handle_response(200, 'false')

        # No exception raised
        AirtelTCLBackend.handle_response(200, 'true')
Esempio n. 13
0
    def test_handle_response(self):
        with self.assertRaises(AirtelTCLError):
            AirtelTCLBackend.handle_response(500, 'true')

        with self.assertRaises(AirtelTCLError):
            AirtelTCLBackend.handle_response(200, 'false')

        # No exception raised
        AirtelTCLBackend.handle_response(200, 'true')
Esempio n. 14
0
    def handle(self, domain, **options):
        for sms in SMS.objects.filter(
            domain=domain,
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
            date__lt=datetime(2017, 6, 26),
        ):
            if sms.custom_metadata:
                continue

            slug, num_matches = self.get_indicator_slug(sms)
            if num_matches == 1:
                sms.custom_metadata = {'icds_indicator': slug}
                sms.save()
Esempio n. 15
0
    def handle(self, domain, **options):
        for sms in SMS.objects.filter(
                domain=domain,
                backend_api=AirtelTCLBackend.get_api_id(),
                direction='O',
                processed=True,
                date__lt=datetime(2017, 6, 26),
        ):
            if sms.custom_metadata:
                continue

            slug, num_matches = self.get_indicator_slug(sms)
            if num_matches == 1:
                sms.custom_metadata = {'icds_indicator': slug}
                sms.save()
Esempio n. 16
0
    def get_records(self, domain, start_timestamp, end_timestamp, indicator_filter=None, state_filter=None):
        indicator_filter = indicator_filter or []
        state_filter = state_filter or []

        if not isinstance(indicator_filter, list):
            raise TypeError("Expected list for indicator_filter")

        if not isinstance(state_filter, list):
            raise TypeError("Expected list for state_filter")

        for sms in SMS.objects.filter(
            domain=domain,
            date__gt=start_timestamp,
            date__lte=end_timestamp,
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
        ).order_by('date'):
            recipient_details = self.get_recipient_details(sms)
            location_details = self.get_location_details(recipient_details['location_id'])
            indicator_slug = self.get_indicator_slug(sms)

            if indicator_filter and indicator_slug not in indicator_filter:
                continue

            if state_filter and location_details['state'].get('site_code') not in state_filter:
                continue

            yield (
                self.format_timestamp(sms.date),
                sms.phone_number,
                recipient_details['name'],
                location_details['state'].get('name'),
                location_details['district'].get('name'),
                location_details['block'].get('name'),
                location_details['supervisor'].get('name'),
                location_details['awc'].get('name'),
                sms.text,
                recipient_details['type'],
                sms.couch_recipient,
                indicator_slug,
                location_details['state'].get('location_id'),
                location_details['district'].get('location_id'),
                location_details['block'].get('location_id'),
                location_details['supervisor'].get('location_id'),
                location_details['awc'].get('location_id'),
            )
Esempio n. 17
0
    def get_records(self, domain, start_timestamp, end_timestamp, indicator_filter=None, state_filter=None):
        indicator_filter = indicator_filter or []
        state_filter = state_filter or []

        if not isinstance(indicator_filter, list):
            raise TypeError("Expected list for indicator_filter")

        if not isinstance(state_filter, list):
            raise TypeError("Expected list for state_filter")

        for sms in SMS.objects.filter(
            domain=domain,
            date__gt=start_timestamp,
            date__lte=end_timestamp,
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
        ).order_by('date'):
            recipient_details = self.get_recipient_details(sms)
            location_details = self.get_location_details(recipient_details['location_id'])
            indicator_slug = self.get_indicator_slug(sms)

            if indicator_filter and indicator_slug not in indicator_filter:
                continue

            if state_filter and location_details['state'].get('site_code') not in state_filter:
                continue

            yield (
                self.format_timestamp(sms.date),
                sms.phone_number,
                recipient_details['name'],
                location_details['state'].get('name'),
                location_details['district'].get('name'),
                location_details['block'].get('name'),
                location_details['supervisor'].get('name'),
                location_details['awc'].get('name'),
                sms.text,
                recipient_details['type'],
                sms.couch_recipient,
                indicator_slug,
                location_details['state'].get('location_id'),
                location_details['district'].get('location_id'),
                location_details['block'].get('location_id'),
                location_details['supervisor'].get('location_id'),
                location_details['awc'].get('location_id'),
            )
Esempio n. 18
0
    def handle(self, domain, start_date, end_date, **options):
        start_timestamp, end_timestamp = self.get_start_and_end_timestamps(
            start_date, end_date)
        self.recipient_id_to_location_id = {}
        self.location_id_to_location = {}
        self.location_id_to_state_code = {}
        self.state_code_to_name = {'unknown': 'Unknown'}

        data = {}

        filename = 'icds-sms-usage--%s--%s.xlsx' % (
            start_date.strftime('%Y-%m-%d'),
            end_date.strftime('%Y-%m-%d'),
        )

        for sms in SMS.objects.filter(
                domain=domain,
                date__gt=start_timestamp,
                date__lte=end_timestamp,
                backend_api=AirtelTCLBackend.get_api_id(),
                direction='O',
                processed=True,
        ):
            location = self.get_location(sms)
            state_code = self.get_state_code(location)
            if state_code not in data:
                data[state_code] = {}

            indicator_slug = self.get_indicator_slug(sms)
            if indicator_slug not in data[state_code]:
                data[state_code][indicator_slug] = 0

            data[state_code][indicator_slug] += 1

        with open(filename, 'wb') as f:
            headers = ('State Code', 'State Name', 'Indicator', 'SMS Count')
            excel_data = []

            for state_code, state_data in data.items():
                for indicator_slug, count in state_data.items():
                    excel_data.append(
                        (state_code, self.state_code_to_name[state_code],
                         indicator_slug, count))

            export_raw((('icds-sms-usage', headers), ),
                       (('icds-sms-usage', excel_data), ), f)
Esempio n. 19
0
    def handle(self, domain, start_date, end_date, **options):
        start_timestamp, end_timestamp = self.get_start_and_end_timestamps(start_date, end_date)
        self.recipient_id_to_location_id = {}
        self.location_id_to_location = {}
        self.location_id_to_state_code = {}
        self.state_code_to_name = {'unknown': 'Unknown'}

        data = {}

        filename = 'icds-sms-usage--%s--%s.xlsx' % (
            start_date.strftime('%Y-%m-%d'),
            end_date.strftime('%Y-%m-%d'),
        )

        for sms in SMS.objects.filter(
            domain=domain,
            date__gt=start_timestamp,
            date__lte=end_timestamp,
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
        ):
            location = self.get_location(sms)
            state_code = self.get_state_code(location)
            if state_code not in data:
                data[state_code] = {}

            indicator_slug = self.get_indicator_slug(sms)
            if indicator_slug not in data[state_code]:
                data[state_code][indicator_slug] = 0

            data[state_code][indicator_slug] += 1

        with open(filename, 'wb') as f:
            headers = ('State Code', 'State Name', 'Indicator', 'SMS Count')
            excel_data = []

            for state_code, state_data in data.items():
                for indicator_slug, count in state_data.items():
                    excel_data.append((state_code, self.state_code_to_name[state_code], indicator_slug, count))

            export_raw(
                (('icds-sms-usage', headers), ),
                (('icds-sms-usage', excel_data), ),
                f
            )
Esempio n. 20
0
    def setUpClass(cls):
        super(GetICDSSmsUsageTest, cls).setUpClass()
        cls.domain_obj = create_domain(cls.domain)

        def make_user(name, location):
            user = CommCareUser.create(cls.domain, name, 'password')
            user.set_location(location)
            return user

        cls.loc_types = setup_location_types(cls.domain, ['state', 'district', 'block', 'supervisor', 'awc'])
        cls.states = (
            make_loc('state', type='state', domain=cls.domain),
            make_loc('state2', type='state', domain=cls.domain),
        )
        cls.districts = (
            make_loc('district', type='district', domain=cls.domain, parent=cls.states[0]),
            make_loc('district2', type='district', domain=cls.domain, parent=cls.states[1]),
        )
        cls.blocks = (
            make_loc('block', type='block', domain=cls.domain, parent=cls.districts[0]),
            make_loc('block2', type='block', domain=cls.domain, parent=cls.districts[1]),
        )
        cls.supervisors = (
            make_loc('supervisor', type='supervisor', domain=cls.domain, parent=cls.blocks[0]),
            make_loc('supervisor2', type='supervisor', domain=cls.domain, parent=cls.blocks[1]),
        )
        cls.awcs = (
            make_loc('awc', type='awc', domain=cls.domain, parent=cls.supervisors[0]),
            make_loc('awc2', type='awc', domain=cls.domain, parent=cls.supervisors[1]),
        )
        cls.users = (
            make_user('user', cls.awcs[0]),
            make_user('user2', cls.awcs[1]),
        )

        cls.sms_list.append(SMS.objects.create(
            domain=cls.domain,
            date=date(2017, 4, 10),
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
            couch_recipient=cls.users[0]._id,
            custom_metadata={'icds_indicator': 'xxx'}
        ))
        cls.sms_list.append(SMS.objects.create(
            domain=cls.domain,
            date=date(2017, 4, 10),
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
            couch_recipient=cls.users[0]._id,
            custom_metadata={'icds_indicator': 'xxx'}
        ))
        cls.sms_list.append(SMS.objects.create(
            domain=cls.domain,
            date=date(2017, 4, 10),
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
            couch_recipient=cls.users[1]._id,
            custom_metadata={'icds_indicator': 'xxx'}
        ))
        cls.sms_list.append(SMS.objects.create(
            domain=cls.domain,
            date=date(2017, 4, 10),
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
            couch_recipient=cls.users[1]._id,
            custom_metadata={'icds_indicator': 'aaa'}
        ))
        date_start = '2017-04-01'
        date_end = '2017-05-01'
        call_command("get_icds_sms_usage", cls.domain, date_start, date_end)
        cls.workbook_name = 'icds-sms-usage--{0}--{1}.xlsx'.format(date_start, date_end)
        cls.workbook = openpyxl.load_workbook(cls.workbook_name)
Esempio n. 21
0
 def test_get_formatted_timestamp(self):
     self.assertEqual(
         AirtelTCLBackend.get_formatted_timestamp(datetime(2018, 9, 7, 12, 34, 56)),
         '07092018123456'
     )
    def handle(self, domain, start_date, end_date, **options):
        if end_date < start_date:
            raise CommandError("Can not have end date before start date")

        self.recipient_id_to_location_id = {}
        self.location_id_to_state_code = {}
        self.location_id_to_district_code = {}
        self.state_code_to_name = {'unknown': 'Unknown'}
        self.district_code_to_name = {'unknown': 'Unknown'}

        state_level_data = {}
        district_level_data = {}

        filename = 'icds-sms-usage--%s--%s.xlsx' % (
            start_date.strftime('%Y-%m-%d'),
            end_date.strftime('%Y-%m-%d'),
        )

        on_date = start_date
        while on_date <= end_date:
            start_timestamp, end_timestamp = self.get_start_and_end_timestamps(
                on_date)

            for sms in SMS.objects.filter(
                    domain=domain,
                    processed_timestamp__gt=start_timestamp,
                    processed_timestamp__lte=end_timestamp,
                    backend_api=AirtelTCLBackend.get_api_id(),
                    direction='O',
                    processed=True,
            ):
                location_id = self.get_location_id(sms)
                state_code = self.get_state_code(domain, location_id)
                district_code = self.get_district_code(domain, location_id)
                if state_code not in district_level_data:
                    state_level_data[state_code] = {}
                    district_level_data[state_code] = {}
                if district_code not in district_level_data[state_code]:
                    district_level_data[state_code][district_code] = {}

                indicator_slug = self.get_indicator_slug(sms)
                if indicator_slug not in state_level_data[state_code]:
                    state_level_data[state_code][indicator_slug] = 0
                if indicator_slug not in district_level_data[state_code][
                        district_code]:
                    district_level_data[state_code][district_code][
                        indicator_slug] = 0

                district_level_data[state_code][district_code][
                    indicator_slug] += 1
                state_level_data[state_code][indicator_slug] += 1

            on_date = on_date + timedelta(days=1)

        with open(filename, 'wb') as excel_file:
            state_headers = ('State Code', 'State Name', 'Indicator',
                             'SMS Count')
            district_headers = ('State Code', 'State Name', 'District Code',
                                'District Name', 'Indicator', 'SMS Count')
            excel_state_data = []
            excel_district_data = []

            for state_code, state_data in sorted(state_level_data.items()):
                for indicator_slug, count in sorted(state_data.items()):
                    excel_state_data.append(
                        (state_code, self.state_code_to_name[state_code],
                         indicator_slug, count))

            for state_code, state_data in sorted(district_level_data.items()):
                for district_code, district_data in sorted(state_data.items()):
                    for indicator_slug, count in sorted(district_data.items()):
                        excel_district_data.append(
                            (state_code, self.state_code_to_name[state_code],
                             district_code,
                             self.district_code_to_name[district_code],
                             indicator_slug, count))

            export_raw((('icds-sms-usage', state_headers),
                        ('icds-sms-usage-by-district', district_headers)),
                       (('icds-sms-usage', excel_state_data),
                        ('icds-sms-usage-by-district', excel_district_data)),
                       excel_file)
        return filename
Esempio n. 23
0
    def setUpClass(cls):
        super(GetICDSSmsUsageTest, cls).setUpClass()
        cls.domain_obj = create_domain(cls.domain)

        def make_user(name, location):
            user = CommCareUser.create(cls.domain, name, 'password')
            user.set_location(location)
            return user

        cls.loc_types = setup_location_types(cls.domain, ['state', 'district', 'block', 'supervisor', 'awc'])
        cls.states = (
            make_loc('state', type='state', domain=cls.domain),
            make_loc('state2', type='state', domain=cls.domain),
        )
        cls.districts = (
            make_loc('district', type='district', domain=cls.domain, parent=cls.states[0]),
            make_loc('district2', type='district', domain=cls.domain, parent=cls.states[1]),
        )
        cls.blocks = (
            make_loc('block', type='block', domain=cls.domain, parent=cls.districts[0]),
            make_loc('block2', type='block', domain=cls.domain, parent=cls.districts[1]),
        )
        cls.supervisors = (
            make_loc('supervisor', type='supervisor', domain=cls.domain, parent=cls.blocks[0]),
            make_loc('supervisor2', type='supervisor', domain=cls.domain, parent=cls.blocks[1]),
        )
        cls.awcs = (
            make_loc('awc', type='awc', domain=cls.domain, parent=cls.supervisors[0]),
            make_loc('awc2', type='awc', domain=cls.domain, parent=cls.supervisors[1]),
        )
        cls.users = (
            make_user('user', cls.awcs[0]),
            make_user('user2', cls.awcs[1]),
        )

        cls.sms_list.append(SMS.objects.create(
            domain=cls.domain,
            date=date(2017, 4, 10),
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
            couch_recipient=cls.users[0]._id,
            custom_metadata={'icds_indicator': 'xxx'}
        ))
        cls.sms_list.append(SMS.objects.create(
            domain=cls.domain,
            date=date(2017, 4, 10),
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
            couch_recipient=cls.users[0]._id,
            custom_metadata={'icds_indicator': 'xxx'}
        ))
        cls.sms_list.append(SMS.objects.create(
            domain=cls.domain,
            date=date(2017, 4, 10),
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
            couch_recipient=cls.users[1]._id,
            custom_metadata={'icds_indicator': 'xxx'}
        ))
        cls.sms_list.append(SMS.objects.create(
            domain=cls.domain,
            date=date(2017, 4, 10),
            backend_api=AirtelTCLBackend.get_api_id(),
            direction='O',
            processed=True,
            couch_recipient=cls.users[1]._id,
            custom_metadata={'icds_indicator': 'aaa'}
        ))
        date_start = '2017-04-01'
        date_end = '2017-05-01'
        call_command("get_icds_sms_usage", cls.domain, date_start, date_end)
        cls.workbook_name = 'icds-sms-usage--{0}--{1}.xlsx'.format(date_start, date_end)
        cls.workbook = openpyxl.load_workbook(cls.workbook_name)
Esempio n. 24
0
 def test_get_formatted_timestamp(self):
     self.assertEqual(
         AirtelTCLBackend.get_formatted_timestamp(
             datetime(2018, 9, 7, 12, 34, 56)), '07092018123456')