A seemingly better function is in https://github.com/dimagi/casexml/pull/64 though it still seems quite crufty. We should offline about more details of the relationships between forms and cases as I'm starting to believe what we are trying to do with the API is not rich enough.