Beispiel #1
0
    def handle(self, domain, **options):
        batch_size = 100
        updates = []
        errors = []

        case_ids = get_all_episode_ids(domain)
        cases = iter_all_active_person_episode_cases(domain, case_ids)

        for person, episode in with_progress_bar(cases,
                                                 len(case_ids),
                                                 oneline=False):
            try:
                update_json = self.updater(domain, person,
                                           episode).update_json()
            except Exception as e:
                errors.append(
                    [person.case_id, episode.case_id, episode.domain, e])
                continue

            if update_json:
                updates.append((episode.case_id, update_json, False))
            if len(updates) >= batch_size:
                if options['commit']:
                    bulk_update_cases(domain, updates, self.__module__)
                updates = []

        if len(updates) > 0:
            if options['commit']:
                bulk_update_cases(domain, updates, self.__module__)

        self.write_errors(errors)
    def handle(self, domain, **options):

        ca = CaseAccessors(domain)
        case_ids = get_all_episode_ids(domain)
        cases = iter_all_active_person_episode_cases(domain,
                                                     case_ids,
                                                     sector='private')

        bad_episodes = {}

        for person, _ in with_progress_bar(cases, length=len(case_ids)):
            occurrence_cases = get_all_occurrence_cases_from_person(
                domain, person.case_id)
            for occurrence_case in occurrence_cases:
                episode_cases = ca.get_reverse_indexed_cases(
                    [occurrence_case.case_id])
                open_episode_cases = [
                    case for case in episode_cases
                    if not case.closed and case.type == 'episode'
                    and case.dynamic_case_properties().get(
                        'episode_type') == "confirmed_tb"
                ]
                if len(open_episode_cases) > 1:
                    bad_episodes[occurrence_case] = [
                        c.case_id for c in open_episode_cases
                    ]

        print(len(bad_episodes), " bad episodes")

        filename = 'bad_episodes-{}.csv'.format(
            datetime.datetime.now().strftime('%Y-%m-%d_%H.%M.%S'))
        with open(filename, 'w') as f:
            writer = csv.writer(f)
            writer.writerow(['occurrence_id', 'episode_ids'])
            for occurrence_id, bad_cases in six.iteritems(bad_episodes):
                bad_cases.insert(0, occurrence_id)
                writer.writerow(bad_cases)

        print("Output saved in: {}".format(filename))
    def handle(self, domain, **options):
        commit = options['commit']

        filename = "reassign_from_facility-{}.csv".format(
            datetime.datetime.now().strftime('%Y-%m-%d_%H.%M.%S'))
        columns = [
            'case_id', 'facility_assigned_to', 'owner_id',
            'last_owner_id_changed', 'last_facility_assigned_to_changed',
            'note'
        ]

        case_ids = get_all_episode_ids(domain)
        cases = iter_all_active_person_episode_cases(domain,
                                                     case_ids,
                                                     sector='private')
        bad_cases = []
        to_update = []
        for person, _ in with_progress_bar(cases, length=len(case_ids)):
            facility_assigned_to = person.get_case_property(
                'facility_assigned_to')
            owner_id = person.owner_id
            if facility_assigned_to == owner_id:
                continue
            if not facility_assigned_to and owner_id in [MJK, ALERT_INDIA]:
                # cases with a blank facility and owned by MJK or Alert-India are known about already
                continue

            owner_id_changes = sorted(get_all_changes_to_case_property(
                person, 'owner_id'),
                                      key=lambda c: c.modified_on,
                                      reverse=True)
            facility_id_changes = sorted(get_all_changes_to_case_property(
                person, 'facility_assigned_to'),
                                         key=lambda c: c.modified_on,
                                         reverse=True)

            case_dict = {
                'case_id': person.case_id,
                'facility_assigned_to': facility_assigned_to,
                'owner_id': owner_id,
            }
            try:
                case_dict['last_owner_id_changed'] = owner_id_changes[
                    0].modified_on
                case_dict[
                    'last_facility_assigned_to_changed'] = facility_id_changes[
                        0].modified_on
                if owner_id_changes[0].modified_on < facility_id_changes[
                        0].modified_on:
                    case_dict['note'] = 'updated'
                    to_update.append((person.case_id, {
                        "owner_id": facility_assigned_to
                    }, False))
                else:
                    case_dict['note'] = 'not updated'
            except IndexError as e:
                case_dict['last_owner_id_changed'] = None
                case_dict['last_facility_assigned_to_changed'] = None
                case_dict['note'] = 'no changes found: {}'.format(
                    six.text_type(e))

            bad_cases.append(case_dict)

        if commit:
            print("Updating: ", len(to_update), " cases")
            for update in chunked(to_update, 100):
                bulk_update_cases(domain, update, self.__module__)
        else:
            print("Would have updated: ", len(to_update), " cases")

        with open(filename, 'w') as f:
            writer = csv.DictWriter(f, fieldnames=columns)
            writer.writeheader()
            for case in bad_cases:
                writer.writerow(case)