def get_monthly_case_list(chw, startdate, enddate): """ Like get_monthly_case_breakdown but return lists of the actual CommCareCase objects. Also the dates are different. A case is included in the date range based on the first date the case is synced to the phone, regardless of due date. The return value is: { date: [(case_id, casedoc), (case_id, casedoc), ...], date: [(case_id, casedoc), (case_id, casedoc), ...], ... casedocs can be null, which would typically indicate that the case or patient has since been deleted. """ data = get_db().view("phone/cases_sent_to_chws", group=True, group_level=2, reduce=True, startkey=[chw.get_id], endkey=[chw.get_id, {}]) monthly_breakdown = defaultdict(lambda: []) for row in data: case_id = row["key"][1] first_synced = string_to_datetime(row["value"]) first_synced = datetime(first_synced.year, first_synced.month, first_synced.day) if startdate <= first_synced and first_synced < enddate: try: monthly_breakdown[datetime(first_synced.year, first_synced.month, 1)]\ .append((case_id, CommCareCase.get_by_id(case_id))) except MultipleResultsFound: logging.error("Multiple results found for case id %s in chw pi report. this is really weird." % case_id) return monthly_breakdown
def followup_made(case_id): """ For a given case id, return if a followup was made against it """ # NOTE: Due to the way the system works the only time a case has more # than one form submitted against it is when a CHW makes a follow up. # Clinic forms _only_ create new case or _manually close_ existing cases # (not with a form). Therefore a proxy for this logic is that the case # has 2 or more forms submitted against it. rows = get_db().view("case/xform_case", key=case_id, reduce=False).all() casedoc = CommCareCase.get_by_id(case_id) if not casedoc: logging.warning(("Couldn't find commcare case with id %s. " "This may mean the patient was deleted.") % case_id) return False for row in rows: # get the forms and if the namespace is a followup, then a followup # was made form = CXFormInstance.get(row["value"]) if form.namespace == config.CHW_FOLLOWUP_NAMESPACE: return True return False