@@ -0,0 +1,2460 @@
+from __future__ import absolute_import%0Afrom __future__ import unicode_literals%0A%0Aimport csv%0A%0Afrom django.core.management import BaseCommand%0A%0Afrom corehq.form_processor.interfaces.dbaccessors import CaseAccessors%0Afrom corehq.form_processor.models import CommCareCaseSQL%0Afrom corehq.sql_db.util import get_db_aliases_for_partitioned_query%0A%0Afrom dimagi.utils.chunked import chunked%0A%0A%0Aclass Command(BaseCommand):%0A    %22%22%22https://manage.dimagi.com/default.asp?265190%0A%0A    Some ccs_record cases have evidence of a delivery occurring, but do not%0A    have an associated delivery form. The implications on the case are that an%0A    add property is not set and the schedule phase is 2.%0A    %22%22%22%0A%0A    def add_arguments(self, parser):%0A        parser.add_argument(%0A            'csv_file',%0A            help=%22File path for csv file%22,%0A        )%0A%0A    def handle(self, csv_file, **options):%0A        self.domain = 'icds-cas'%0A        self.case_accessor = CaseAccessors(self.domain)%0A        with open(csv_file, %22w%22) as csv_file:%0A            field_names = %5B%0A                'case_id', 'owner_id', 'modified_on', 'server_modified_on',%0A                'add', 'edd', 'ccs_phase', 'num_pnc_visits', 'current_schedule_phase'%0A            %5D%0A%0A            csv_writer = csv.DictWriter(csv_file, field_names, extrasaction='ignore')%0A            csv_writer.writeheader()%0A%0A            for ccs_case in self._get_cases():%0A                properties = ccs_case.case_json%0A%0A                if 'add' in properties:%0A                    continue%0A%0A                if properties.get('current_schedule_phase') != 2:%0A                    continue%0A%0A                properties.update(%7B%0A                    'case_id': ccs_case.case_id,%0A                    'owner_id': ccs_case.owner_id,%0A                    'modified_on': ccs_case.modified_on,%0A                    'server_modified_on': ccs_case.modified_on%0A                %7D)%0A                csv_writer.write_row(properties)%0A%0A    def _get_cases(self):%0A        dbs = get_db_aliases_for_partitioned_query()%0A        for db in dbs:%0A            ccs_record_case_ids = (%0A                CommCareCaseSQL.objects%0A                .using(db)%0A                .filter(domain=self.domain, type='ccs_record', closed=False)%0A                .values_list('case_id', flat=True)%0A            )%0A%0A            for case_ids in chunked(ccs_record_case_ids, 100):%0A                cases = self.case_accessor.get_cases(case_ids)%0A                for case in cases:%0A                    yield case%0A

Add management command to find ccs record cases without delivery form