本文整理汇总了Python中casexml.apps.case.models.CommCareCase.save方法的典型用法代码示例。如果您正苦于以下问题:Python CommCareCase.save方法的具体用法?Python CommCareCase.save怎么用?Python CommCareCase.save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类casexml.apps.case.models.CommCareCase
的用法示例。
在下文中一共展示了CommCareCase.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_date_case_properties_for_inequality
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def test_date_case_properties_for_inequality(self):
case = CommCareCase(
domain=self.domain,
type='test-case-type-2',
)
self.rule2.automaticupdaterulecriteria_set = [
AutomaticUpdateRuleCriteria(
property_name='property1',
property_value='2016-02-24',
match_type=AutomaticUpdateRuleCriteria.MATCH_NOT_EQUAL,
),
]
case.set_case_property('property1', '2016-02-24')
case.save()
case = CommCareCase.get(case.get_id)
case.server_modified_on = datetime(2015, 1, 1)
self.assertFalse(self.rule2.rule_matches_case(case, datetime(2016, 1, 1)))
case.set_case_property('property1', '2016-02-25')
case.save()
case = CommCareCase.get(case.get_id)
case.server_modified_on = datetime(2015, 1, 1)
self.assertTrue(self.rule2.rule_matches_case(case, datetime(2016, 1, 1)))
case.delete()
示例2: UtilTestCase
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
class UtilTestCase(TestCase):
def setUp(self):
self.case = CommCareCase(domain='test-domain', name='test-case')
self.case.save()
self.user = CommCareUser.create('test-domain', 'test-user', '123')
def tearDown(self):
self.case.delete()
self.user.delete()
def testCleanPhoneNumber(self):
phone_number = " 324 23-23421241"
cleaned = clean_phone_number(phone_number)
self.assertEquals(cleaned, "+3242323421241")
def test_get_contact(self):
contact = get_contact(self.case.get_id)
self.assertEqual(contact.get_id, self.case.get_id)
self.assertTrue(isinstance(contact, CommConnectCase))
contact = get_contact(self.user.get_id)
self.assertEqual(contact.get_id, self.user.get_id)
self.assertTrue(isinstance(contact, CommCareUser))
with self.assertRaises(ContactNotFoundException):
get_contact('this-id-should-not-be-found')
def test_apply_leniency(self):
self.assertEqual('16175551234', apply_leniency(' 1 (617) 555-1234 '))
self.assertEqual('16175551234', apply_leniency(' 1.617.555.1234 '))
self.assertEqual('16175551234', apply_leniency(' +1 617 555 1234 '))
示例3: setUp
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def setUp(self):
self.now = datetime(2015, 10, 22, 0, 0)
self.domain = 'auto-update-test'
self.rule = AutomaticUpdateRule(
domain=self.domain,
name='test-rule',
case_type='test-case-type',
active=True,
server_modified_boundary=30,
)
self.rule.save()
self.rule.automaticupdaterulecriteria_set = [
AutomaticUpdateRuleCriteria(
property_name='last_visit_date',
property_value='30',
match_type=AutomaticUpdateRuleCriteria.MATCH_DAYS_SINCE,
),
]
self.rule.automaticupdateaction_set = [
AutomaticUpdateAction(
action=AutomaticUpdateAction.ACTION_UPDATE,
property_name='update_flag',
property_value='Y',
),
AutomaticUpdateAction(
action=AutomaticUpdateAction.ACTION_CLOSE,
),
]
self.rule2 = AutomaticUpdateRule(
domain=self.domain,
name='test-rule-2',
case_type='test-case-type-2',
active=True,
server_modified_boundary=30,
)
self.rule2.save()
self.rule2.automaticupdateaction_set = [
AutomaticUpdateAction(
action=AutomaticUpdateAction.ACTION_CLOSE,
),
]
self.rule3 = AutomaticUpdateRule(
domain=self.domain,
name='test-rule-3',
case_type='test-case-type-2',
active=True,
server_modified_boundary=50,
)
self.rule3.save()
self.rule3.automaticupdateaction_set = [
AutomaticUpdateAction(
action=AutomaticUpdateAction.ACTION_CLOSE,
),
]
case = CommCareCase(domain=self.domain, type='test-case-type')
case.save()
self.case_id = case.get_id
示例4: test_date_case_properties_for_equality
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def test_date_case_properties_for_equality(self):
"""
Date case properties are automatically converted from string to date
when fetching from the db, so here we want to make sure this doesn't
interfere with our ability to compare dates for equality.
"""
case = CommCareCase(
domain=self.domain,
type='test-case-type-2',
)
self.rule2.automaticupdaterulecriteria_set = [
AutomaticUpdateRuleCriteria(
property_name='property1',
property_value='2016-02-24',
match_type=AutomaticUpdateRuleCriteria.MATCH_EQUAL,
),
]
case.set_case_property('property1', '2016-02-24')
case.save()
case = CommCareCase.get(case.get_id)
case.server_modified_on = datetime(2015, 1, 1)
self.assertTrue(self.rule2.rule_matches_case(case, datetime(2016, 1, 1)))
case.set_case_property('property1', '2016-02-25')
case.save()
case = CommCareCase.get(case.get_id)
case.server_modified_on = datetime(2015, 1, 1)
self.assertFalse(self.rule2.rule_matches_case(case, datetime(2016, 1, 1)))
case.delete()
示例5: hard_rebuild_case
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def hard_rebuild_case(domain, case_id, detail):
try:
case = CommCareCase.get(case_id)
assert case.domain == domain
found = True
except CaseNotFound:
case = CommCareCase()
case.case_id = case_id
case.domain = domain
found = False
forms = FormProcessorCouch.get_case_forms(case_id)
filtered_forms = [f for f in forms if f.is_normal]
sorted_forms = sorted(filtered_forms, key=lambda f: f.received_on)
actions = _get_actions_from_forms(domain, sorted_forms, case_id)
if not found and case.domain is None:
case.domain = domain
rebuild_case_from_actions(case, actions)
# todo: should this move to case.rebuild?
if not case.xform_ids:
if not found:
return None
# there were no more forms. 'delete' the case
case.doc_type = 'CommCareCase-Deleted'
# add a "rebuild" action
case.actions.append(_rebuild_action())
case.save()
return case
示例6: test_get_list
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def test_get_list(self):
"""
Any case in the appropriate domain should be in the list from the API.
"""
# The actual infrastructure involves saving to CouchDB, having PillowTop
# read the changes and write it to ElasticSearch.
# the pillow is set to offline mode - elasticsearch not needed to validate
fake_case_es = FakeXFormES()
v0_4.MOCK_CASE_ES = fake_case_es
modify_date = datetime.utcnow()
backend_case = CommCareCase(server_modified_on=modify_date, domain=self.domain.name)
backend_case.save()
self.addCleanup(backend_case.delete)
translated_doc = transform_case_for_elasticsearch(backend_case.to_json())
fake_case_es.add_doc(translated_doc['_id'], translated_doc)
response = self._assert_auth_get_resource(self.list_endpoint)
self.assertEqual(response.status_code, 200)
api_cases = json.loads(response.content)['objects']
self.assertEqual(len(api_cases), 1)
api_case = api_cases[0]
self.assertEqual(api_case['server_date_modified'], json_format_datetime(backend_case.server_modified_on))
示例7: TestUtilFunctions
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
class TestUtilFunctions(TestCase):
def setUp(self):
self.case = CommCareCase(domain='test-domain', name='test-case')
self.case.save()
self.user = CommCareUser.create('test-domain', 'test-user', '123')
def test_get_contact(self):
contact = get_contact(self.case.get_id)
self.assertEqual(contact.get_id, self.case.get_id)
self.assertTrue(isinstance(contact, CommConnectCase))
contact = get_contact(self.user.get_id)
self.assertEqual(contact.get_id, self.user.get_id)
self.assertTrue(isinstance(contact, CommCareUser))
try:
get_contact('this-id-should-not-be-found')
except Exception:
pass
else:
self.assertTrue(False)
def test_apply_leniency(self):
self.assertEqual('16175551234', apply_leniency(' 1 (617) 555-1234 '))
self.assertEqual('16175551234', apply_leniency(' 1.617.555.1234 '))
self.assertEqual('16175551234', apply_leniency(' +1 617 555 1234 '))
def tearDown(self):
self.case.delete()
self.user.delete()
示例8: test_get_list
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def test_get_list(self):
"""
Any case in the appropriate domain should be in the list from the API.
"""
# The actual infrastructure involves saving to CouchDB, having PillowTop
# read the changes and write it to ElasticSearch.
#the pillow is set to offline mode - elasticsearch not needed to validate
pillow = CasePillow(online=False)
fake_case_es = FakeXFormES()
v0_4.MOCK_CASE_ES = fake_case_es
modify_date = datetime.utcnow()
backend_case = CommCareCase(server_modified_on=modify_date, domain=self.domain.name)
backend_case.save()
translated_doc = pillow.change_transform(backend_case.to_json())
fake_case_es.add_doc(translated_doc['_id'], translated_doc)
self.client.login(username=self.username, password=self.password)
response = self.client.get(self.list_endpoint)
self.assertEqual(response.status_code, 200)
api_cases = simplejson.loads(response.content)['objects']
self.assertEqual(len(api_cases), 1)
api_case = api_cases[0]
self.assertEqual(dateutil.parser.parse(api_case['server_date_modified']), backend_case.server_modified_on)
backend_case.delete()
示例9: rebuild_case
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def rebuild_case(case_id):
"""
Given a case ID, rebuild the entire case state based on all existing forms
referencing it. Useful when things go wrong or when you need to manually
rebuild a case after archiving / deleting it
"""
try:
case = CommCareCase.get(case_id)
found = True
except ResourceNotFound:
case = CommCareCase()
case._id = case_id
found = False
# clear actions, xform_ids, close state, and all dynamic properties
dynamic_properties = set([k for action in case.actions for k in action.updated_unknown_properties.keys()])
for k in dynamic_properties:
try:
delattr(case, k)
except KeyError:
pass
# already deleted means it was explicitly set to "deleted",
# as opposed to getting set to that because it has no actions
already_deleted = case.doc_type == 'CommCareCase-Deleted' and primary_actions(case)
if not already_deleted:
case.doc_type = 'CommCareCase'
case.xform_ids = []
case.actions = []
case.closed = False
case.closed_on = None
case.closed_by = ''
form_ids = get_case_xform_ids(case_id)
forms = [fetch_and_wrap_form(id) for id in form_ids]
filtered_forms = [f for f in forms if f.doc_type == "XFormInstance"]
sorted_forms = sorted(filtered_forms, key=lambda f: f.received_on)
for form in sorted_forms:
if not found and case.domain is None:
case.domain = form.domain
assert form.domain == case.domain
case_updates = get_case_updates(form)
filtered_updates = [u for u in case_updates if u.id == case_id]
for u in filtered_updates:
case.update_from_case_update(u, form)
case.xform_ids = [f._id for f in sorted_forms]
if not case.xform_ids:
if not found:
return None
# there were no more forms. 'delete' the case
case.doc_type = 'CommCareCase-Deleted'
# add a "rebuild" action
case.actions.append(_rebuild_action())
case.save()
return case
示例10: TestReverseIndexedCases
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
class TestReverseIndexedCases(TestCase):
def setUp(self):
super(TestReverseIndexedCases, self).setUp()
self.domain = 'domain'
self.factory = CaseFactory(self.domain)
self.indexed_case_id = uuid.uuid4().hex
self.index = CommCareCaseIndex(
identifier="host",
referenced_type="host",
relationship=CASE_INDEX_EXTENSION,
referenced_id=self.indexed_case_id
)
self.case = CommCareCase(domain=self.domain, indices=[self.index])
self.case.save()
def tearDown(self):
FormProcessorTestUtils.delete_all_cases()
FormProcessorTestUtils.delete_all_xforms()
super(TestReverseIndexedCases, self).tearDown()
def _delete_relationship(self):
del self.case.indices[0].relationship
self.case.save()
def test_legacy_reverse_index(self):
"""Test that cases with indices without a relationship are still returned"""
self.assertEqual(
[self.case._id],
[c._id for c in
get_reverse_indexed_cases(self.domain, [self.indexed_case_id], relationship=CASE_INDEX_EXTENSION)])
# remove the relationship and make sure the case is still returned when asking for child indexes
self._delete_relationship()
self.assertEqual(
[self.case._id],
[c._id for c in get_reverse_indexed_cases(self.domain, [self.indexed_case_id])])
# make sure it doesn't show up if we are asking for extension indexes
self.assertEqual(
[],
[c._id for c in
get_reverse_indexed_cases(self.domain, [self.indexed_case_id], CASE_INDEX_EXTENSION)])
def test_legacy_reverse_index_json(self):
expected_returned_json = [{
'doc_type': 'CommCareCaseIndex',
'identifier': self.index.identifier,
'relationship': self.index.relationship,
'referenced_type': self.index.referenced_type,
'referenced_id': self.case._id
}]
self.assertEqual(
expected_returned_json,
get_reverse_indices_json(self.domain, self.indexed_case_id))
self._delete_relationship()
# it should now be a child relationship
expected_returned_json[0]['relationship'] = CASE_INDEX_CHILD
self.assertEqual(expected_returned_json, get_reverse_indices_json(self.domain, self.indexed_case_id))
示例11: saving
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def saving(request):
xform = XFormInstance(_attachments={'form.xml': {'data': '-'}})
xform.save()
case = CommCareCase()
case.save()
xform.initial_processing_complete = True
xform.save()
case.delete()
xform.delete()
return HttpResponse('Thanks for submitting', status=201)
示例12: RebuildStockStateTest
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
class RebuildStockStateTest(TestCase):
def setUp(self):
self.domain = 'asldkjf-domain'
self.case = CommCareCase(domain=self.domain)
self.case.save()
self.product = make_product(self.domain, 'Product Name', 'prodcode')
self._stock_state_key = dict(
section_id='stock',
case_id=self.case.get_id,
product_id=self.product.get_id
)
def _get_stats(self):
stock_state = StockState.objects.get(**self._stock_state_key)
latest_txn = StockTransaction.latest(**self._stock_state_key)
all_txns = StockTransaction.get_ordered_transactions_for_stock(
**self._stock_state_key)
return stock_state, latest_txn, all_txns
def _submit_ledgers(self, ledger_blocks):
submit_case_blocks(ledger_blocks.format(**self._stock_state_key),
self.domain)
def test_simple(self):
self._submit_ledgers(LEDGER_BLOCKS_SIMPLE)
stock_state, latest_txn, all_txns = self._get_stats()
self.assertEqual(stock_state.stock_on_hand, 100)
self.assertEqual(latest_txn.stock_on_hand, 100)
self.assertEqual(all_txns.count(), 2)
rebuild_stock_state(**self._stock_state_key)
stock_state, latest_txn, all_txns = self._get_stats()
self.assertEqual(stock_state.stock_on_hand, 200)
self.assertEqual(latest_txn.stock_on_hand, 200)
self.assertEqual(all_txns.count(), 2)
def test_inferred(self):
self._submit_ledgers(LEDGER_BLOCKS_INFERRED)
stock_state, latest_txn, all_txns = self._get_stats()
# this is weird behavior:
# it just doesn't process the second one
# even though knowing yesterday's certainly changes the meaning
# of today's transfer
self.assertEqual(stock_state.stock_on_hand, 50)
self.assertEqual(latest_txn.stock_on_hand, 50)
self.assertEqual(all_txns.count(), 3)
rebuild_stock_state(**self._stock_state_key)
stock_state, latest_txn, all_txns = self._get_stats()
self.assertEqual(stock_state.stock_on_hand, 150)
self.assertEqual(latest_txn.stock_on_hand, 150)
self.assertEqual(all_txns.count(), 2)
示例13: test_recursive_indexes
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def test_recursive_indexes(self):
c = CommCareCase(
_id='infinite-recursion',
name='infinite_recursion',
type='bug',
indices=[CommCareCaseIndex(identifier='self', referenced_type='bug', referenced_id='infinite-recursion')],
)
c.save()
# this call used to fail with infinite recursion
hierarchy = get_case_hierarchy(c, {})
self.assertEqual(1, len(hierarchy['case_list']))
示例14: test_get_doc_count_in_domain_by_class
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def test_get_doc_count_in_domain_by_class(self):
case = CommCareCase(domain=self.domain, opened_on=datetime.datetime(2000, 1, 1))
case.save()
self.addCleanup(case.delete)
case2 = CommCareCase(domain=self.domain, opened_on=datetime.datetime(2001, 1, 1))
case2.save()
self.addCleanup(case2.delete)
get = functools.partial(
get_doc_count_in_domain_by_class, self.domain, CommCareCase)
self.assertEqual(get(), 2)
示例15: testCaseLookupTypeCheck
# 需要导入模块: from casexml.apps.case.models import CommCareCase [as 别名]
# 或者: from casexml.apps.case.models.CommCareCase import save [as 别名]
def testCaseLookupTypeCheck(self):
case = CommCareCase(domain=self.domain, type='nonmatch-type')
case.save()
self.assertEqual(1, len(get_case_ids_in_domain(self.domain)))
config = self._config(self.default_headers)
file = MockExcelFile(header_columns=self.default_headers, num_rows=3,
row_generator=id_match_generator(case._id))
res = do_import(file, config, self.domain)
# because the type is wrong these shouldn't match
self.assertEqual(3, res['created_count'])
self.assertEqual(0, res['match_count'])
self.assertEqual(4, len(get_case_ids_in_domain(self.domain)))