def associate_revocation_svrs_with_ips(
        merged_persons: List[schema.StatePerson]):
    """
    For each person in the provided |merged_persons|, attempts to associate
    StateSupervisionViolationResponses that result in revocation with their
    corresponding StateIncarcerationPeriod.
    """
    for person in merged_persons:
        svrs = get_all_entities_of_cls(
            [person], schema.StateSupervisionViolationResponse)
        ips = get_all_entities_of_cls([person],
                                      schema.StateIncarcerationPeriod)

        revocation_svrs: List[schema.StateSupervisionViolationResponse] = []
        for svr in svrs:
            svr = cast(schema.StateSupervisionViolationResponse, svr)
            if revoked_to_prison(svr) and svr.response_date:
                revocation_svrs.append(svr)
        revocation_ips: List[schema.StateIncarcerationPeriod] = []
        for ip in ips:
            ip = cast(schema.StateIncarcerationPeriod, ip)
            admission_reason = (StateIncarcerationPeriodAdmissionReason.
                                parse_from_canonical_string(
                                    ip.admission_reason))
            if isinstance(admission_reason,
                          StateIncarcerationPeriodAdmissionReason):
                if (is_commitment_from_supervision(
                        admission_reason, allow_ingest_only_enum_values=True)
                        and ip.admission_date):
                    revocation_ips.append(ip)

        if not revocation_svrs or not revocation_ips:
            continue

        sorted_svrs = sorted(revocation_svrs, key=lambda x: x.response_date)

        seen: Set[int] = set()
        for svr in sorted_svrs:
            closest_ip = _get_closest_matching_incarceration_period(
                svr, revocation_ips)
            if closest_ip and id(closest_ip) not in seen:
                seen.add(id(closest_ip))
                closest_ip.source_supervision_violation_response = svr
Exemple #2
0
 def test_completeEnumSet_revokedToPrison(self):
     svr = schema.StateSupervisionViolationResponse()
     for revocation_type in StateSupervisionViolationResponseRevocationType:
         svr.revocation_type = revocation_type.value
         revoked_to_prison(svr)