def testDuplicateCasePropertiesBug(self): """ How do we do when submitting multiple values for the same property in an update block """ self.assertEqual(0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "duplicate_case_properties.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) # before the bug was fixed this call failed process_cases(form) case = CommCareCase.get(form.xpath("form/case/@case_id")) # make sure the property is there, but empty self.assertEqual("", case.foo) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "duplicate_case_properties_2.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) case = CommCareCase.get(form.xpath("form/case/@case_id")) # make sure the property takes the last defined value self.assertEqual("2", case.bar)
def testDuplicateCasePropertiesBug(self): """ How do we do when submitting multiple values for the same property in an update block """ self.assertEqual( 0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "duplicate_case_properties.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) # before the bug was fixed this call failed process_cases(form) case = CommCareCase.get(form.xpath("form/case/@case_id")) # make sure the property is there, but empty self.assertEqual("", case.foo) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "duplicate_case_properties_2.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) case = CommCareCase.get(form.xpath("form/case/@case_id")) # make sure the property takes the last defined value self.assertEqual("2", case.bar)
def _test(custom_format_args): case_id = uuid.uuid4().hex format_args = { 'case_id': case_id, 'form_id': uuid.uuid4().hex, 'user_id': uuid.uuid4().hex, 'case_name': 'data corner cases', 'case_type': 'datatype-check', } format_args.update(custom_format_args) for filename in [ 'bugs_in_case_create_datatypes.xml', 'bugs_in_case_update_datatypes.xml' ]: file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", filename) with open(file_path, "rb") as f: xml_data = f.read() xml_data = xml_data.format(**format_args) form = post_xform_to_couch(xml_data) # before the bug was fixed this call failed process_cases(form) case = CommCareCase.get(case_id) self.assertEqual(format_args['user_id'], case.user_id) self.assertEqual(format_args['case_name'], case.name) self.assertEqual(format_args['case_type'], case.type)
def testMixed(self): self.assertEqual(0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "multicase", "mixed_cases.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) self.assertEqual(4, len(CommCareCase.view("case/by_user", reduce=False).all()))
def _do_submit(self, xml_data, dict_attachments): """ RequestFactory submitter - simulates direct submission to server directly (no need to call process case after fact) """ form = post_xform_to_couch(xml_data, dict_attachments) form.domain = TEST_DOMAIN self.assertEqual(len(dict_attachments.keys()), len(form.attachments)) process_cases(form)
def process_cases(sender, xform, config=None, **kwargs): warnings.warn( 'casexml.apps.case.signals.process_cases has been moved to ' 'casexml.apps.case.process_cases ' 'and the unused `sender` and `**kwargs` parameters have been removed.', DeprecationWarning, ) from casexml.apps.case import process_cases process_cases(xform, config)
def testCasesInRepeats(self): self.assertEqual(0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "multicase", "case_in_repeats.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) cases = self._get_cases() self.assertEqual(3, len(cases)) self._check_ids(form, cases)
def _postWithSyncToken(self, filename, token_id): file_path = os.path.join(os.path.dirname(__file__), "data", filename) with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) # set last sync token on the form before saving form.last_sync_token = token_id process_cases(form) return form
def testMixed(self): self.assertEqual( 0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "multicase", "mixed_cases.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) self.assertEqual( 4, len(CommCareCase.view("case/by_user", reduce=False).all()))
def testTopLevelExclusion(self): """ Entire forms tagged as device logs should be excluded """ self.assertEqual(0, len(CommCareCase.view("case/by_user", include_docs=True, reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "exclusion", "device_report.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) self.assertEqual(0, len(CommCareCase.view("case/by_user", include_docs=True, reduce=False).all()))
def testStringFormatProblems(self): """ If two forms share an ID it's a conflict """ self.assertEqual(0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "string_formatting.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) # before the bug was fixed this call failed process_cases(form)
def testLotsOfSubcases(self): """ How do we do when submitting a form with multiple blocks for the same case? """ self.assertEqual(0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "lots_of_subcases.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) # before the bug was fixed this call failed process_cases(form) self.assertEqual(11, len(CommCareCase.view("case/by_user", reduce=False).all()))
def testCasesInRepeats(self): self.assertEqual( 0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "multicase", "case_in_repeats.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) cases = self._get_cases() self.assertEqual(3, len(cases)) self._check_ids(form, cases)
def testParseClose(self): self.testParseCreate() file_path = os.path.join(os.path.dirname(__file__), "data", "v2", "basic_close.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) case = CommCareCase.get("foo-case-id") self.assertTrue(case.closed) self.assertEqual("bar-user-id", case.closed_by)
def testParseWithIndices(self): self.testParseCreate() user_id = "bar-user-id" for prereq in ["some_referenced_id", "some_other_referenced_id"]: post_case_blocks([ CaseBlock( create=True, case_id=prereq, user_id=user_id, version=V2).as_xml(format_datetime=json_format_datetime) ]) file_path = os.path.join(os.path.dirname(__file__), "data", "v2", "index_update.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) case = CommCareCase.get("foo-case-id") self.assertEqual(2, len(case.indices)) self.assertTrue(case.has_index("foo_ref")) self.assertTrue(case.has_index("baz_ref")) self.assertEqual("bar", case.get_index("foo_ref").referenced_type) self.assertEqual("some_referenced_id", case.get_index("foo_ref").referenced_id) self.assertEqual("bop", case.get_index("baz_ref").referenced_type) self.assertEqual("some_other_referenced_id", case.get_index("baz_ref").referenced_id) # check the action self.assertEqual(2, len(case.actions)) [_, index_action] = case.actions self.assertEqual(const.CASE_ACTION_INDEX, index_action.action_type) self.assertEqual(2, len(index_action.indices)) # quick test for ota restore v2response = phone_views.xml_for_case(HttpRequest(), case.get_id, version="2.0") expected_v2_response = """ <case case_id="foo-case-id" date_modified="2011-12-07T13:42:50Z" user_id="bar-user-id" xmlns="http://commcarehq.org/case/transaction/v2"> <create> <case_type>v2_case_type</case_type> <case_name>test case name</case_name> <owner_id>bar-user-id</owner_id> </create> <index> <baz_ref case_type="bop">some_other_referenced_id</baz_ref> <foo_ref case_type="bar">some_referenced_id</foo_ref> </index> </case>""" check_xml_line_by_line(self, expected_v2_response, v2response.content)
def testStringFormatProblems(self): """ If two forms share an ID it's a conflict """ self.assertEqual( 0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "string_formatting.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) # before the bug was fixed this call failed process_cases(form)
def testNestedExclusion(self): """ Blocks inside forms tagged as device logs should be excluded """ self.assertEqual(0, len(CommCareCase.view("case/by_user", include_docs=True, reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "exclusion", "nested_device_report.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) self.assertEqual(1, len(CommCareCase.view("case/by_user", include_docs=True, reduce=False).all())) case = CommCareCase.get("case_in_form") self.assertEqual("form case", case.name)
def testCantPwnCase(self): form = post_xform_to_couch(ALICE_XML) form.domain = ALICE_DOMAIN (case,) = process_cases(form) case_id = case.case_id form = post_xform_to_couch(EVE_XML) form.domain = EVE_DOMAIN with self.assertRaises(IllegalCaseId): process_cases(form) self.assertFalse(hasattr(CommCareCase.get(case_id), 'plan_to_buy_gun')) form = post_xform_to_couch(ALICE_UPDATE_XML) form.domain = ALICE_DOMAIN process_cases(form) self.assertEqual(CommCareCase.get(case_id).plan_to_buy_gun, 'no')
def testConflictingIds(self): """ If two forms share an ID it's a conflict """ self.assertEqual(0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "id_conflicts.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) try: process_cases(form) self.fail("Previous statement should have raised an exception") except Exception: pass
def reprocess_form_cases(form, config=None): """ For a given form, reprocess all case elements inside it. This operation should be a no-op if the form was sucessfully processed, but should correctly inject the update into the case history if the form was NOT successfully processed. """ from casexml.apps.case import process_cases process_cases(form, config) # mark cleaned up now that we've reprocessed it if form.doc_type != 'XFormInstance': form = XFormInstance.get(form._id) form.doc_type = 'XFormInstance' form.save()
def testEmptyCaseId(self): """ How do we do when submitting an empty case id? """ self.assertEqual(0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "empty_id.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) try: process_cases(form) self.fail("Empty Id should crash") except: pass
def testParseNamedNamespace(self): file_path = os.path.join(os.path.dirname(__file__), "data", "v2", "named_namespace.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) case = CommCareCase.get("14cc2770-2d1c-49c2-b252-22d6ecce385a") self.assertFalse(case.closed) self.assertEqual("d5ce3a980b5b69e793445ec0e3b2138e", case.user_id) self.assertEqual(datetime(2011, 12, 27), case.modified_on) self.assertEqual("cc_bihar_pregnancy", case.type) self.assertEqual("TEST", case.name) self.assertEqual(2, len(case.actions))
def testParseWithIndices(self): self.testParseCreate() user_id = "bar-user-id" for prereq in ["some_referenced_id", "some_other_referenced_id"]: post_case_blocks( [ CaseBlock(create=True, case_id=prereq, user_id=user_id, version=V2).as_xml( format_datetime=json_format_datetime ) ] ) file_path = os.path.join(os.path.dirname(__file__), "data", "v2", "index_update.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) case = CommCareCase.get("foo-case-id") self.assertEqual(2, len(case.indices)) self.assertTrue(case.has_index("foo_ref")) self.assertTrue(case.has_index("baz_ref")) self.assertEqual("bar", case.get_index("foo_ref").referenced_type) self.assertEqual("some_referenced_id", case.get_index("foo_ref").referenced_id) self.assertEqual("bop", case.get_index("baz_ref").referenced_type) self.assertEqual("some_other_referenced_id", case.get_index("baz_ref").referenced_id) # check the action self.assertEqual(2, len(case.actions)) [_, index_action] = case.actions self.assertEqual(const.CASE_ACTION_INDEX, index_action.action_type) self.assertEqual(2, len(index_action.indices)) # quick test for ota restore v2response = phone_views.xml_for_case(HttpRequest(), case.get_id, version="2.0") expected_v2_response = """ <case case_id="foo-case-id" date_modified="2011-12-07T13:42:50Z" user_id="bar-user-id" xmlns="http://commcarehq.org/case/transaction/v2"> <create> <case_type>v2_case_type</case_type> <case_name>test case name</case_name> <owner_id>bar-user-id</owner_id> </create> <index> <baz_ref case_type="bop">some_other_referenced_id</baz_ref> <foo_ref case_type="bar">some_referenced_id</foo_ref> </index> </case>""" check_xml_line_by_line(self, expected_v2_response, v2response.content)
def testLotsOfSubcases(self): """ How do we do when submitting a form with multiple blocks for the same case? """ self.assertEqual( 0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "lots_of_subcases.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) # before the bug was fixed this call failed process_cases(form) self.assertEqual( 11, len(CommCareCase.view("case/by_user", reduce=False).all()))
def reprocess_form_cases(form, config=None): """ For a given form, reprocess all case elements inside it. This operation should be a no-op if the form was sucessfully processed, but should correctly inject the update into the case history if the form was NOT successfully processed. """ from casexml.apps.case import process_cases process_cases(form, config) # mark cleaned up now that we've reprocessed it if form.doc_type != "XFormInstance": form = XFormInstance.get(form._id) form.doc_type = "XFormInstance" form.save()
def testConflictingIds(self): """ If two forms share an ID it's a conflict """ self.assertEqual( 0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "id_conflicts.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) try: process_cases(form) self.fail("Previous statement should have raised an exception") except Exception: pass
def testEmptyCaseId(self): """ How do we do when submitting an empty case id? """ self.assertEqual( 0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "empty_id.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) try: process_cases(form) self.fail("Empty Id should crash") except: pass
def testParseUpdate(self): self.testParseCreate() file_path = os.path.join(os.path.dirname(__file__), "data", "v2", "basic_update.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) case = CommCareCase.get("foo-case-id") self.assertFalse(case.closed) self.assertEqual("bar-user-id", case.user_id) self.assertEqual(datetime(2011, 12, 7, 13, 42, 50), case.modified_on) self.assertEqual("updated_v2_case_type", case.type) self.assertEqual("updated case name", case.name) self.assertEqual("something dynamic", case.dynamic) self.assertEqual(2, len(case.actions)) self.assertEqual("bar-user-id", case.actions[1].user_id)
def testOutOfOrderSubmissions(self): dir = os.path.join(os.path.dirname(__file__), "data", "ordering") forms = [] for fname in ('create_oo.xml', 'update_oo.xml'): with open(os.path.join(dir, fname), "rb") as f: xml_data = f.read() forms.append(post_xform_to_couch(xml_data)) [create, update] = forms # process out of order process_cases(update) process_cases(create) case = CommCareCase.get('30bc51f6-3247-4966-b4ae-994f572e85fe') self.assertEqual('from the update form', case.pupdate) self.assertEqual('from the create form', case.pcreate) self.assertEqual('overridden by the update form', case.pboth)
def testMultipleCaseBlocks(self): """ How do we do when submitting a form with multiple blocks for the same case? """ self.assertEqual(0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "multiple_case_blocks.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) # before the bug was fixed this call failed process_cases(form) case = CommCareCase.get(form.xpath("form/comunidad/case/@case_id")) self.assertEqual('1630005', case.community_code) self.assertEqual('SantaMariaCahabon', case.district_name) self.assertEqual('TAMERLO', case.community_name) ids = case.get_xform_ids_from_couch() self.assertEqual(1, len(ids)) self.assertEqual(form._id, ids[0])
def testParseCreate(self): file_path = os.path.join(os.path.dirname(__file__), "data", "v2", "basic_create.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) case = CommCareCase.get("foo-case-id") self.assertFalse(case.closed) self.assertEqual("bar-user-id", case.user_id) self.assertEqual("bar-user-id", case.opened_by) self.assertEqual(datetime(2011, 12, 6, 13, 42, 50), case.modified_on) self.assertEqual("v2_case_type", case.type) self.assertEqual("test case name", case.name) self.assertEqual(1, len(case.actions)) [action] = case.actions self.assertEqual("http://openrosa.org/case/test/create", action.xform_xmlns) self.assertEqual("v2 create", action.xform_name) self.assertEqual("bar-user-id", case.actions[0].user_id)
def bootstrap_case_from_xml(test_class, filename, case_id_override=None, referral_id_override=None, domain=None): starttime = utcnow_sans_milliseconds() file_path = os.path.join(os.path.dirname(__file__), "data", filename) with open(file_path, "rb") as f: xml_data = f.read() doc_id, uid, case_id, ref_id = replace_ids_and_post(xml_data, case_id_override=case_id_override, referral_id_override=referral_id_override) doc = XFormInstance.get(doc_id) if domain: doc.domain = domain process_cases(doc) case = CommCareCase.get(case_id) test_class.assertTrue(starttime <= case.server_modified_on) test_class.assertTrue(datetime.utcnow() >= case.server_modified_on) test_class.assertEqual(case_id, case.case_id) return case
def submit_error_case(case_id): """ Used if something went wrong creating the real vscan case update. """ xform = render_vscan_error(case_id) lock = create_and_lock_xform(xform, process=None, domain=UTH_DOMAIN) lock.obj.domain = UTH_DOMAIN return process_cases(lock.obj)
def testMultipleCaseBlocks(self): """ How do we do when submitting a form with multiple blocks for the same case? """ self.assertEqual( 0, len(CommCareCase.view("case/by_user", reduce=False).all())) file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", "multiple_case_blocks.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) # before the bug was fixed this call failed process_cases(form) case = CommCareCase.get(form.xpath("form/comunidad/case/@case_id")) self.assertEqual('1630005', case.community_code) self.assertEqual('SantaMariaCahabon', case.district_name) self.assertEqual('TAMERLO', case.community_name) ids = case.get_xform_ids_from_couch() self.assertEqual(1, len(ids)) self.assertEqual(form._id, ids[0])
def post_case_blocks(case_blocks, form_extras=None): """ Post case blocks. Extras is used to add runtime attributes to the form before sending it off to the case (current use case is sync-token pairing) """ from casexml.apps.case import process_cases if form_extras is None: form_extras = {} form = ElementTree.Element("data") form.attrib['xmlns'] = "https://www.commcarehq.org/test/casexml-wrapper" form.attrib['xmlns:jrm'] = "http://openrosa.org/jr/xforms" for block in case_blocks: form.append(block) with create_and_lock_xform(ElementTree.tostring(form)) as xform: for k, v in form_extras.items(): setattr(xform, k, v) process_cases(xform=xform) return xform
def post_case_blocks(case_blocks, form_extras=None): """ Post case blocks. Extras is used to add runtime attributes to the form before sending it off to the case (current use case is sync-token pairing) """ from casexml.apps.case import process_cases if form_extras is None: form_extras = {} form = ElementTree.Element("data") form.attrib["xmlns"] = "https://www.commcarehq.org/test/casexml-wrapper" form.attrib["xmlns:jrm"] = "http://openrosa.org/jr/xforms" for block in case_blocks: form.append(block) with create_and_lock_xform(ElementTree.tostring(form)) as xform: for k, v in form_extras.items(): setattr(xform, k, v) process_cases(xform=xform) return xform
def testRestoreAttributes(self): file_path = os.path.join(os.path.dirname(__file__), "data", "attributes.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) [newcase] = CommCareCase.view("case/by_user", reduce=False, include_docs=True).all() self.assertTrue(isinstance(newcase.adate, dict)) self.assertEqual(date(2012,02,01), newcase.adate["#text"]) self.assertEqual("i am an attribute", newcase.adate["@someattr"]) self.assertTrue(isinstance(newcase.dateattr, dict)) self.assertEqual("this shouldn't break", newcase.dateattr["#text"]) self.assertEqual(date(2012,01,01), newcase.dateattr["@somedate"]) self.assertTrue(isinstance(newcase.stringattr, dict)) self.assertEqual("neither should this", newcase.stringattr["#text"]) self.assertEqual("i am a string", newcase.stringattr["@somestring"]) restore_payload = generate_restore_payload(dummy_user()) # ghetto self.assertTrue('<dateattr somedate="2012-01-01">' in restore_payload) self.assertTrue('<stringattr somestring="i am a string">' in restore_payload)
def _test(custom_format_args): case_id = uuid.uuid4().hex format_args = { 'case_id': case_id, 'form_id': uuid.uuid4().hex, 'user_id': uuid.uuid4().hex, 'case_name': 'data corner cases', 'case_type': 'datatype-check', } format_args.update(custom_format_args) for filename in ['bugs_in_case_create_datatypes.xml', 'bugs_in_case_update_datatypes.xml']: file_path = os.path.join(os.path.dirname(__file__), "data", "bugs", filename) with open(file_path, "rb") as f: xml_data = f.read() xml_data = xml_data.format(**format_args) form = post_xform_to_couch(xml_data) # before the bug was fixed this call failed process_cases(form) case = CommCareCase.get(case_id) self.assertEqual(format_args['user_id'], case.user_id) self.assertEqual(format_args['case_name'], case.name) self.assertEqual(format_args['case_type'], case.type)
def bootstrap_case_from_xml(test_class, filename, case_id_override=None, referral_id_override=None, domain=None): starttime = utcnow_sans_milliseconds() file_path = os.path.join(os.path.dirname(__file__), "data", filename) with open(file_path, "rb") as f: xml_data = f.read() doc_id, uid, case_id, ref_id = replace_ids_and_post( xml_data, case_id_override=case_id_override, referral_id_override=referral_id_override) doc = XFormInstance.get(doc_id) if domain: doc.domain = domain process_cases(doc) case = CommCareCase.get(case_id) test_class.assertTrue(starttime <= case.server_modified_on) test_class.assertTrue(datetime.utcnow() >= case.server_modified_on) test_class.assertEqual(case_id, case.case_id) return case
def attach_images_to_case(case_id, files): """ Handle case submission for the vscan endpoint """ xform = render_vscan_xform(case_id, files) file_dict = {} for f in files: identifier = os.path.split(f)[-1] file_dict[identifier] = UploadedFile(files[f], identifier) lock = create_and_lock_xform(xform, attachments=file_dict, process=None, domain=UTH_DOMAIN) lock.obj.domain = UTH_DOMAIN return process_cases(lock.obj)
def submit_error_case(case_id): """ Used if something went wrong creating the real vscan case update. """ xform = render_vscan_error(case_id) lock = create_and_lock_xform( xform, process=None, domain=UTH_DOMAIN, ) lock.obj.domain = UTH_DOMAIN return process_cases(lock.obj)
def create_case(case_id, files, patient_case_id=None): """ Handle case submission for the sonosite endpoint """ # we already parsed what we need from this, so can just remove it # without worrying we will need it later files.pop("PT_PPS.XML", "") xform = render_sonosite_xform(files, case_id, patient_case_id) file_dict = {} for f in files: file_dict[f] = UploadedFile(files[f], f) lock = create_and_lock_xform(xform, attachments=file_dict, process=None, domain=UTH_DOMAIN) lock.obj.domain = UTH_DOMAIN return process_cases(lock.obj)
def attach_images_to_case(case_id, files): """ Handle case submission for the vscan endpoint """ xform = render_vscan_xform(case_id, files) file_dict = {} for f in files: identifier = os.path.split(f)[-1] file_dict[identifier] = UploadedFile(files[f], identifier) lock = create_and_lock_xform( xform, attachments=file_dict, process=None, domain=UTH_DOMAIN, ) lock.obj.domain = UTH_DOMAIN return process_cases(lock.obj)
def create_case(case_id, files, patient_case_id=None): """ Handle case submission for the sonosite endpoint """ # we already parsed what we need from this, so can just remove it # without worrying we will need it later files.pop('PT_PPS.XML', '') xform = render_sonosite_xform(files, case_id, patient_case_id) file_dict = {} for f in files: file_dict[f] = UploadedFile(files[f], f) lock = create_and_lock_xform( xform, attachments=file_dict, process=None, domain=UTH_DOMAIN, ) lock.obj.domain = UTH_DOMAIN return process_cases(lock.obj)
def testUserRestoreWithCase(self): file_path = os.path.join(os.path.dirname(__file__), "data", "create_short.xml") with open(file_path, "rb") as f: xml_data = f.read() form = post_xform_to_couch(xml_data) process_cases(form) user = dummy_user() # implicit length assertion [newcase] = CommCareCase.view("case/by_user", reduce=False, include_docs=True).all() self.assertEqual(1, len(user.get_case_updates(None).actual_cases_to_sync)) expected_case_block = """ <case> <case_id>asdf</case_id> <date_modified>2010-06-29T13:42:50Z</date_modified> <create> <case_type_id>test_case_type</case_type_id> <user_id>foo</user_id> <case_name>test case name</case_name> <external_id>someexternal</external_id> </create> </case>""" check_xml_line_by_line(self, expected_case_block, xml.get_case_xml(newcase, [case_const.CASE_ACTION_CREATE, case_const.CASE_ACTION_UPDATE])) # check v2 expected_v2_case_block = """ <case case_id="asdf" date_modified="2010-06-29T13:42:50Z" user_id="foo" xmlns="http://commcarehq.org/case/transaction/v2" > <create> <case_type>test_case_type</case_type> <case_name>test case name</case_name> <owner_id>foo</owner_id> </create> <update> <external_id>someexternal</external_id> </update> </case>""" check_xml_line_by_line( self, expected_v2_case_block, xml.get_case_xml( newcase, [case_const.CASE_ACTION_CREATE, case_const.CASE_ACTION_UPDATE], version="2.0", ), ) restore_payload = generate_restore_payload( user=dummy_user(), items=True, ) sync_log_id = SyncLog.view( "phone/sync_logs_by_user", include_docs=True, reduce=False, ).one().get_id check_xml_line_by_line( self, dummy_restore_xml(sync_log_id, expected_case_block, items=4), restore_payload )
def _process_cases(sender, xform, config=None, **kwargs): from casexml.apps.case import process_cases process_cases(xform, config)