def get_open_cases_to_send(clinic_id, zone, last_sync): """ Given a list of patients, get the open/updated cases since the last sync operation. This returns tuples phone_case objects, and flags that say whether or not they should be created """ to_return = [] case_ids = [] # find all relevant cases for the CHW and send them to the phone, unless # they have already been sent and haven't changed. potential_case_list = PatientCase.view_with_patient("case/open_for_chw_for_phone", key=[clinic_id, zone]) last_sync_date = datetime.min if not last_sync else last_sync.date for case in potential_case_list: # keep a running list of case ids sent down because the phone doesn't # deal well with duplicates. There shouldn't be duplicates, but they # can come up with bugs, so arbitrarily only send down the first case # if there are any duplicates if case.get_id not in case_ids: phone_case = PhoneCase.from_bhoma_case(case) if phone_case is None: # we don't expect to get into this scenario, but it can happen # don't fail hard with null reference below continue previously_synced = case_previously_synced(phone_case.case_id, last_sync) if phone_case and phone_case.is_started() and not phone_case.is_over(): # this is an active case, so send it unless it's a duplicate # or was already synced if phone_case.case_id in case_ids: logging.warning("Found a duplicate case for %s. Will not be sent to phone." % phone_case.case_id) elif previously_synced and phone_case.date_modified < last_sync_date: logging.debug("Case %s already sent to phone and no changes. Won't be sent again" % phone_case.case_id) else: case_ids.append(phone_case.case_id) to_return.append((phone_case, not previously_synced)) return to_return
def patient_case_xml(request, patient_id): """ Case xml for a single patient. This is just a debug method, and as such ignores the start date flag and always returns the full case xml block """ template = \ """<cases>%s </cases>""" return HttpResponse(template % "".join([xml.get_case_xml(PhoneCase.from_bhoma_case(case)) \ for case in cases_for_patient(patient_id)]), mimetype="text/xml")