Beispiel #1
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()
Beispiel #2
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()
Beispiel #3
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'),
            )
    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'),
            )
    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
            )
Beispiel #6
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)
Beispiel #7
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)
Beispiel #8
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)
    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