def _is_match(potential_match, encounter): """ Whether a pregnancy matches an encounter. """ # if it's open and the visit is before the end date it's a clear match # this assumes you're always looking at the last pregnancy and a recent form # which is valid as long as people aren't entering crazy backlogged data. if not potential_match.closed and potential_match.get_end_date() > encounter.visit_date: return True # if it's closed, but was only closed in the last two weeks it's a match elif potential_match.closed and \ potential_match.get_end_date() + timedelta(14) > encounter.visit_date: return True elif potential_match.pregnancy_dates_set(): # the dates were set and we didn't match. no match. return False else: # if the dates weren't set, cross check against _our_ dates edd = get_edd(encounter) if edd: end_date = edd + timedelta(days=DAYS_AFTER_EDD_END) start_date = lmp_from_edd(edd) - timedelta(days=DAYS_BEFORE_LMP_START) if start_date < potential_match.get_first_visit_date() < end_date and \ start_date < potential_match.get_last_visit_date() < end_date: return True elif start_date < potential_match.get_first_visit_date() < end_date or \ start_date < potential_match.get_last_visit_date() < end_date: # what to do about this corner case? Half the visits fall in # the other half don't. # just add it for now logging.error("Pregnancy dates out of whack for visit, trying our best " "to deal with it (xform: %s)" % encounter.get_xform().get_id) return True else: # dates clearly not in range return False else: # if the visit date is < 9 months (270 days) from the first known # visit date, count it return potential_match.get_first_visit_date() < encounter.visit_date < potential_match.get_first_visit_date() + timedelta(days=270)
def lmp(self): if self.edd: return lmp_from_edd(self.edd) raise PregnancyDatesNotSetException()