def make_supply_point_product(supply_point_case, product_uuid, owner_id=None): domain = supply_point_case.domain id = uuid.uuid4().hex user_id = get_commtrack_user_id(domain) owner_id = owner_id or get_owner_id(supply_point_case) or user_id username = const.COMMTRACK_USERNAME product_name = Product.get(product_uuid).name caseblock = CaseBlock(case_id=id, create=True, version=V2, case_name=product_name, user_id=user_id, owner_id=owner_id, case_type=const.SUPPLY_POINT_PRODUCT_CASE_TYPE, update={"product": product_uuid}, index={ const.PARENT_CASE_REF: (const.SUPPLY_POINT_CASE_TYPE, supply_point_case._id), }) casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, domain, username, user_id, xmlns=const.COMMTRACK_SUPPLY_POINT_PRODUCT_XMLNS) return SupplyPointProductCase.get(id)
def make_supply_point_product(supply_point_case, product_uuid): domain = supply_point_case.domain id = uuid.uuid4().hex user_id = get_commtrack_user_id(domain) username = const.COMMTRACK_USERNAME caseblock = CaseBlock( case_id=id, create=True, version=V2, user_id=user_id, case_type=const.SUPPLY_POINT_PRODUCT_CASE_TYPE, update={ "product": product_uuid }, index={ const.PARENT_CASE_REF: (const.SUPPLY_POINT_CASE_TYPE, supply_point_case._id), } ) casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, domain, username, user_id) pc = CommCareCase.get(id) pc.location_ = supply_point_case.location_ pc.save() return pc
def make_supply_point_product(supply_point_case, product_uuid, owner_id=None): domain = supply_point_case.domain id = uuid.uuid4().hex user_id = get_commtrack_user_id(domain) owner_id = owner_id or get_owner_id(supply_point_case) or user_id username = const.COMMTRACK_USERNAME product_name = Product.get(product_uuid).name caseblock = CaseBlock( case_id=id, create=True, version=V2, case_name=product_name, user_id=user_id, owner_id=owner_id, case_type=const.SUPPLY_POINT_PRODUCT_CASE_TYPE, update={ "product": product_uuid }, index={ const.PARENT_CASE_REF: (const.SUPPLY_POINT_CASE_TYPE, supply_point_case._id), } ) casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, domain, username, user_id, xmlns=const.COMMTRACK_SUPPLY_POINT_PRODUCT_XMLNS) return SupplyPointProductCase.get(id)
def testBadIndexReference(self): CASE_ID = 'test-bad-index-case' block = CaseBlock(create=True, case_id=CASE_ID, user_id=USER_ID, version=V2, index={'bad': ('bad-case', 'not-an-existing-id')}) try: post_case_blocks([block.as_xml()]) self.fail("Submitting against a bad case in an index should fail!") except Exception: pass
def sync_user_cases(commcare_user): from casexml.apps.case.tests.util import CaseBlock domain = commcare_user.project if not (domain and domain.call_center_config.enabled): return # language or phone_number can be null and will break # case submission fields = { 'name': commcare_user.name, 'email': commcare_user.email, 'language': commcare_user.language or '', 'phone_number': commcare_user.phone_number or '' } # fields comes second to prevent custom user data overriding fields = dict(commcare_user.user_data, **fields) found = False try: case = get_case_by_domain_hq_user_id(domain.name, commcare_user._id, include_docs=True) found = bool(case) except MultipleResultsFound: return close = commcare_user.to_be_deleted() or not commcare_user.is_active if found: caseblock = CaseBlock( create=False, case_id=case._id, version=V2, owner_id=domain.call_center_config.case_owner_id, case_type=domain.call_center_config.case_type, close=close, update=fields ) else: fields['hq_user_id'] = commcare_user._id caseblock = CaseBlock( create=True, case_id=uuid.uuid4().hex, owner_id=domain.call_center_config.case_owner_id, user_id=commcare_user._id, version=V2, case_type=domain.call_center_config.case_type, update=fields ) casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, domain, commcare_user.username, commcare_user._id)
def to_xml(self): caseblock = CaseBlock( case_id=self.id, create=self.create, version=V2, user_id=self.user_id, case_type=const.REQUISITION_CASE_TYPE, update={ "amount_requested": str(self.amount_requested) }, index={ const.PARENT_CASE_REF: (const.SUPPLY_POINT_PRODUCT_CASE_TYPE, self.product_stock_case_id), } ) return ElementTree.tostring(caseblock.as_xml())
def make_supply_point(domain, location): # a supply point is currently just a case with a special type id = uuid.uuid4().hex user_id = get_commtrack_user_id(domain) username = const.COMMTRACK_USERNAME caseblock = CaseBlock( case_id=id, create=True, version=V2, user_id=user_id, case_type=const.SUPPLY_POINT_CASE_TYPE, ) casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, domain, username, user_id) c = CommCareCase.get(id) c.bind_to_location(location) c.save() return c
def make_supply_point(domain, location, owner_id=None): # a supply point is currently just a case with a special type id = uuid.uuid4().hex user_id = get_commtrack_user_id(domain) owner_id = owner_id or user_id username = const.COMMTRACK_USERNAME caseblock = CaseBlock( case_id=id, create=True, version=V2, case_name=location.name, user_id=user_id, owner_id=owner_id, case_type=const.SUPPLY_POINT_CASE_TYPE, update={"location_id": location._id}, ) casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, domain, username, user_id, xmlns=const.COMMTRACK_SUPPLY_POINT_XMLNS) return SupplyPointCase.get(id)
def to_xml(self): extras = {} if self.owner_id: extras["owner_id"] = self.owner_id if self.create: extras["case_name"] = self.product_stock_case.name extras["index"] = { const.PARENT_CASE_REF: (const.SUPPLY_POINT_PRODUCT_CASE_TYPE, self.product_stock_case._id) } caseblock = CaseBlock( case_id=self.id, create=self.create, version=V2, user_id=self.user_id, case_type=const.REQUISITION_CASE_TYPE, update=copy(self.custom_fields), close=self.close, **extras ) return ElementTree.tostring(caseblock.as_xml(format_datetime=json_format_datetime))
def to_xml(self): extras = {} if self.owner_id: extras['owner_id'] = self.owner_id if self.create: extras['case_name'] = self.product_stock_case.name extras['index'] = { const.PARENT_CASE_REF: (const.SUPPLY_POINT_PRODUCT_CASE_TYPE, self.product_stock_case._id), } caseblock = CaseBlock(case_id=self.id, create=self.create, version=V2, user_id=self.user_id, case_type=const.REQUISITION_CASE_TYPE, update=copy(self.custom_fields), close=self.close, **extras) return ElementTree.tostring( caseblock.as_xml(format_datetime=json_format_datetime))
def testParseWithIndices(self): self._test_parse_create() 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).as_xml() ]) 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() _, _, [case] = submit_form_locally(xml_data, 'test-domain') 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) if not settings.TESTS_SHOULD_USE_SQL_BACKEND: # 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 = xml.get_case_xml( case, [const.CASE_ACTION_CREATE, const.CASE_ACTION_UPDATE], V2) expected_v2_response = """ <case case_id="foo-case-id" date_modified="2011-12-07T13:42:50.000000Z" 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> <update> <date_opened>2011-12-06</date_opened> </update> <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)
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 get_update_block(self, case_id, owner_id=None, update=None, version=V1): update = update or {} case_block = CaseBlock( case_id=case_id, update=update, owner_id=owner_id or CaseBlock.undefined, version=version, ).as_xml(format_datetime=date_to_xml_string) return case_block
def make_supply_point(domain, location, owner_id=None): # a supply point is currently just a case with a special type id = uuid.uuid4().hex user_id = get_commtrack_user_id(domain) owner_id = owner_id or user_id username = const.COMMTRACK_USERNAME caseblock = CaseBlock(case_id=id, create=True, version=V2, case_name=location.name, user_id=user_id, owner_id=owner_id, case_type=const.SUPPLY_POINT_CASE_TYPE, update={ 'location_id': location._id, }) casexml = ElementTree.tostring(caseblock.as_xml()) submit_case_blocks(casexml, domain, username, user_id, xmlns=const.COMMTRACK_SUPPLY_POINT_XMLNS) return SupplyPointCase.get(id)
def to_case_block(self, user_id=None): def convert_prop(val): return str(val) if val is not None else '' props = ['current_stock', 'stocked_out_since', 'last_reported'] case_update = CaseBlock(version=V2, case_id=self.case._id, user_id=user_id or 'FIXME', update=dict((k, convert_prop(getattr(self, k))) for k in props)).as_xml() # convert xml.etree to lxml case_update = etree.fromstring( legacy_etree.ElementTree.tostring(case_update)) return case_update
def get_create_block(self, case_id, type, user_id, owner_id, name=None, version=V1, **kwargs): name = name or case_id case_block = CaseBlock( create=True, case_id=case_id, case_name=name, case_type=type, user_id=user_id, external_id=case_id, owner_id=owner_id, version=version, **kwargs).as_xml(format_datetime=date_to_xml_string) return case_block