本文整理汇总了Python中corehq.form_processor.interfaces.processor.FormProcessorInterface.casedb_cache方法的典型用法代码示例。如果您正苦于以下问题:Python FormProcessorInterface.casedb_cache方法的具体用法?Python FormProcessorInterface.casedb_cache怎么用?Python FormProcessorInterface.casedb_cache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类corehq.form_processor.interfaces.processor.FormProcessorInterface
的用法示例。
在下文中一共展示了FormProcessorInterface.casedb_cache方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _perfom_post_save_actions
# 需要导入模块: from corehq.form_processor.interfaces.processor import FormProcessorInterface [as 别名]
# 或者: from corehq.form_processor.interfaces.processor.FormProcessorInterface import casedb_cache [as 别名]
def _perfom_post_save_actions(form, save=True):
interface = FormProcessorInterface(form.domain)
cache = interface.casedb_cache(
domain=form.domain, lock=False, deleted_ok=True, xforms=[form],
load_src="reprocess_form_post_save",
)
with cache as casedb:
case_stock_result = SubmissionPost.process_xforms_for_cases([form], casedb)
case_models = case_stock_result.case_models
if interface.use_sql_domain:
forms = ProcessedForms(form, None)
stock_result = case_stock_result.stock_result
try:
FormProcessorSQL.publish_changes_to_kafka(forms, case_models, stock_result)
except Exception:
error_message = "Error publishing to kafka"
return ReprocessingResult(form, None, None, error_message)
try:
save and SubmissionPost.do_post_save_actions(casedb, [form], case_stock_result)
except PostSaveError:
error_message = "Error performing post save operations"
return ReprocessingResult(form, None, None, error_message)
return ReprocessingResult(form, case_models, None, None)
示例2: CaseDbCacheCouchOnlyTest
# 需要导入模块: from corehq.form_processor.interfaces.processor import FormProcessorInterface [as 别名]
# 或者: from corehq.form_processor.interfaces.processor.FormProcessorInterface import casedb_cache [as 别名]
class CaseDbCacheCouchOnlyTest(TestCase):
def setUp(self):
super(CaseDbCacheCouchOnlyTest, self).setUp()
self.interface = FormProcessorInterface()
def testDocTypeCheck(self):
id = uuid.uuid4().hex
CommCareCase.get_db().save_doc({
"_id": id,
"doc_type": "AintNoCasesHere"
})
doc_back = CommCareCase.get_db().get(id)
self.assertEqual("AintNoCasesHere", doc_back['doc_type'])
cache = CaseDbCacheCouch()
try:
cache.get(id)
self.fail('doc type security check failed to raise exception')
except IllegalCaseId:
pass
def test_nowrap(self):
case_ids = _make_some_cases(1)
cache = self.interface.casedb_cache(wrap=False)
case = cache.get(case_ids[0])
self.assertTrue(isinstance(case, dict))
self.assertFalse(isinstance(case, CommCareCase))
示例3: _get_case_and_ledger_updates
# 需要导入模块: from corehq.form_processor.interfaces.processor import FormProcessorInterface [as 别名]
# 或者: from corehq.form_processor.interfaces.processor.FormProcessorInterface import casedb_cache [as 别名]
def _get_case_and_ledger_updates(domain, sql_form):
"""
Get a CaseStockProcessingResult with the appropriate cases and ledgers to
be saved.
See SubmissionPost.process_xforms_for_cases and methods it calls for the equivalent
section of the form-processing code.
"""
from corehq.apps.commtrack.processing import process_stock
interface = FormProcessorInterface(domain)
assert sql_form.domain
xforms = [sql_form]
with interface.casedb_cache(
domain=domain, lock=False, deleted_ok=True, xforms=xforms,
load_src="couchsqlmigration",
) as case_db:
touched_cases = FormProcessorInterface(domain).get_cases_from_forms(case_db, xforms)
extensions_to_close = get_all_extensions_to_close(domain, list(touched_cases.values()))
case_result = CaseProcessingResult(
domain,
[update.case for update in touched_cases.values()],
[], # ignore dirtiness_flags,
extensions_to_close
)
for case in case_result.cases:
case_db.post_process_case(case, sql_form)
case_db.mark_changed(case)
stock_result = process_stock(xforms, case_db)
cases = case_db.get_cases_for_saving(sql_form.received_on)
stock_result.populate_models()
return CaseStockProcessingResult(
case_result=case_result,
case_models=cases,
stock_result=stock_result,
)
示例4: CaseDbCacheTest
# 需要导入模块: from corehq.form_processor.interfaces.processor import FormProcessorInterface [as 别名]
# 或者: from corehq.form_processor.interfaces.processor.FormProcessorInterface import casedb_cache [as 别名]
class CaseDbCacheTest(TestCase):
"""
Tests the functionality of the CaseDbCache object
"""
def setUp(self):
self.interface = FormProcessorInterface()
@run_with_all_backends
def testDomainCheck(self):
id = uuid.uuid4().hex
post_case_blocks([
CaseBlock(
create=True, case_id=id,
user_id='some-user'
).as_xml()
], {'domain': 'good-domain'}
)
bad_cache = self.interface.casedb_cache(domain='bad-domain')
try:
bad_cache.get(id)
self.fail('domain security check failed to raise exception')
except IllegalCaseId:
pass
good_cache = self.interface.casedb_cache(domain='good-domain')
case = good_cache.get(id)
self.assertEqual('some-user', case.user_id) # just sanity check it's the right thing
def testDocTypeCheck(self):
id = uuid.uuid4().hex
CommCareCase.get_db().save_doc({
"_id": id,
"doc_type": "AintNoCasesHere"
})
doc_back = CommCareCase.get_db().get(id)
self.assertEqual("AintNoCasesHere", doc_back['doc_type'])
cache = CaseDbCacheCouch()
try:
cache.get(id)
self.fail('doc type security check failed to raise exception')
except IllegalCaseId:
pass
@run_with_all_backends
def testGetPopulatesCache(self):
case_ids = _make_some_cases(3)
cache = self.interface.casedb_cache()
for id in case_ids:
self.assertFalse(cache.in_cache(id))
for i, id in enumerate(case_ids):
case = cache.get(id)
self.assertEqual(str(i), case.dynamic_case_properties()['my_index'])
for id in case_ids:
self.assertTrue(cache.in_cache(id))
@run_with_all_backends
def testSetPopulatesCache(self):
case_ids = _make_some_cases(3)
cache = self.interface.casedb_cache()
for id in case_ids:
self.assertFalse(cache.in_cache(id))
for id in case_ids:
cache.set(id, CaseAccessors().get_case(id))
for i, id in enumerate(case_ids):
self.assertTrue(cache.in_cache(id))
case = cache.get(id)
self.assertEqual(str(i), case.dynamic_case_properties()['my_index'])
@run_with_all_backends
def testPopulate(self):
case_ids = _make_some_cases(3)
cache = self.interface.casedb_cache()
for id in case_ids:
self.assertFalse(cache.in_cache(id))
cache.populate(case_ids)
for id in case_ids:
self.assertTrue(cache.in_cache(id))
# sanity check
for i, id in enumerate(case_ids):
case = cache.get(id)
self.assertEqual(str(i), case.dynamic_case_properties()['my_index'])
def testStripHistory(self):
case_ids = _make_some_cases(3)
history_cache = self.interface.casedb_cache()
for i, id in enumerate(case_ids):
self.assertFalse(history_cache.in_cache(id))
case = history_cache.get(id)
self.assertEqual(str(i), case.my_index)
self.assertTrue(len(case.actions) > 0)
nohistory_cache = self.interface.casedb_cache(strip_history=True)
for i, id in enumerate(case_ids):
#.........这里部分代码省略.........
示例5: CaseDbCacheTest
# 需要导入模块: from corehq.form_processor.interfaces.processor import FormProcessorInterface [as 别名]
# 或者: from corehq.form_processor.interfaces.processor.FormProcessorInterface import casedb_cache [as 别名]
class CaseDbCacheTest(TestCase):
"""
Tests the functionality of the CaseDbCache object
"""
def setUp(self):
super(CaseDbCacheTest, self).setUp()
self.interface = FormProcessorInterface()
def testDomainCheck(self):
id = uuid.uuid4().hex
post_case_blocks([
CaseBlock(
create=True, case_id=id,
user_id='some-user'
).as_xml()
], {'domain': 'good-domain'}
)
bad_cache = self.interface.casedb_cache(domain='bad-domain')
try:
bad_cache.get(id)
self.fail('domain security check failed to raise exception')
except IllegalCaseId:
pass
good_cache = self.interface.casedb_cache(domain='good-domain')
case = good_cache.get(id)
self.assertEqual('some-user', case.user_id) # just sanity check it's the right thing
def testGetPopulatesCache(self):
case_ids = _make_some_cases(3)
cache = self.interface.casedb_cache()
for id in case_ids:
self.assertFalse(cache.in_cache(id))
for i, id in enumerate(case_ids):
case = cache.get(id)
self.assertEqual(str(i), case.dynamic_case_properties()['my_index'])
for id in case_ids:
self.assertTrue(cache.in_cache(id))
def testSetPopulatesCache(self):
case_ids = _make_some_cases(3)
cache = self.interface.casedb_cache()
for id in case_ids:
self.assertFalse(cache.in_cache(id))
for id in case_ids:
cache.set(id, CaseAccessors().get_case(id))
for i, id in enumerate(case_ids):
self.assertTrue(cache.in_cache(id))
case = cache.get(id)
self.assertEqual(str(i), case.dynamic_case_properties()['my_index'])
def testPopulate(self):
case_ids = _make_some_cases(3)
cache = self.interface.casedb_cache()
for id in case_ids:
self.assertFalse(cache.in_cache(id))
cache.populate(case_ids)
for id in case_ids:
self.assertTrue(cache.in_cache(id))
# sanity check
for i, id in enumerate(case_ids):
case = cache.get(id)
self.assertEqual(str(i), case.dynamic_case_properties()['my_index'])
示例6: CaseDbCacheCouchOnlyTest
# 需要导入模块: from corehq.form_processor.interfaces.processor import FormProcessorInterface [as 别名]
# 或者: from corehq.form_processor.interfaces.processor.FormProcessorInterface import casedb_cache [as 别名]
class CaseDbCacheCouchOnlyTest(TestCase):
def setUp(self):
super(CaseDbCacheCouchOnlyTest, self).setUp()
self.interface = FormProcessorInterface()
def testDocTypeCheck(self):
id = uuid.uuid4().hex
CommCareCase.get_db().save_doc({
"_id": id,
"doc_type": "AintNoCasesHere"
})
doc_back = CommCareCase.get_db().get(id)
self.assertEqual("AintNoCasesHere", doc_back['doc_type'])
cache = CaseDbCacheCouch()
try:
cache.get(id)
self.fail('doc type security check failed to raise exception')
except IllegalCaseId:
pass
def testStripHistory(self):
case_ids = _make_some_cases(3)
history_cache = self.interface.casedb_cache()
for i, id in enumerate(case_ids):
self.assertFalse(history_cache.in_cache(id))
case = history_cache.get(id)
self.assertEqual(str(i), case.my_index)
self.assertTrue(len(case.actions) > 0)
nohistory_cache = self.interface.casedb_cache(strip_history=True)
for i, id in enumerate(case_ids):
self.assertFalse(nohistory_cache.in_cache(id))
case = nohistory_cache.get(id)
self.assertEqual(str(i), case.my_index)
self.assertTrue(len(case.actions) == 0)
more_case_ids = _make_some_cases(3)
history_cache.populate(more_case_ids)
nohistory_cache.populate(more_case_ids)
for i, id in enumerate(more_case_ids):
self.assertTrue(history_cache.in_cache(id))
case = history_cache.get(id)
self.assertEqual(str(i), case.my_index)
self.assertTrue(len(case.actions) > 0)
for i, id in enumerate(more_case_ids):
self.assertTrue(nohistory_cache.in_cache(id))
case = nohistory_cache.get(id)
self.assertEqual(str(i), case.my_index)
self.assertTrue(len(case.actions) == 0)
def test_nowrap(self):
case_ids = _make_some_cases(1)
cache = self.interface.casedb_cache(wrap=False)
case = cache.get(case_ids[0])
self.assertTrue(isinstance(case, dict))
self.assertFalse(isinstance(case, CommCareCase))
示例7: SubmissionPost
# 需要导入模块: from corehq.form_processor.interfaces.processor import FormProcessorInterface [as 别名]
# 或者: from corehq.form_processor.interfaces.processor.FormProcessorInterface import casedb_cache [as 别名]
#.........这里部分代码省略.........
errors = self.process_signals(instance)
response = self._get_open_rosa_response(instance, errors)
return response, instance, cases
def save_processed_models(self, xforms, case_stock_result):
from casexml.apps.case.signals import case_post_save
instance = xforms[0]
with unfinished_submission(instance) as unfinished_submission_stub:
self.interface.save_processed_models(
xforms,
case_stock_result.case_models,
case_stock_result.stock_result
)
unfinished_submission_stub.saved = True
unfinished_submission_stub.save()
case_stock_result.case_result.commit_dirtiness_flags()
case_stock_result.stock_result.finalize()
for case in case_stock_result.case_models:
case_post_save.send(case.__class__, case=case)
case_stock_result.case_result.close_extensions()
def process_xforms_for_cases(self, xforms):
from casexml.apps.case.xform import get_and_check_xform_domain
from casexml.apps.case.xform import process_cases_with_casedb
from corehq.apps.commtrack.processing import process_stock
instance = xforms[0]
domain = get_and_check_xform_domain(instance)
with self.interface.casedb_cache(domain=domain, lock=True, deleted_ok=True, xforms=xforms) as case_db:
case_result = process_cases_with_casedb(xforms, case_db)
stock_result = process_stock(xforms, case_db)
cases = case_db.get_cases_for_saving(instance.received_on)
stock_result.populate_models()
return CaseStockProcessingResult(
case_result=case_result,
case_models=cases,
stock_result=stock_result,
)
def get_response(self):
response, _, _ = self.run()
return response
def process_signals(self, instance):
feedback = successful_form_received.send_robust(None, xform=instance)
errors = []
for func, resp in feedback:
if resp and isinstance(resp, Exception):
error_message = unicode(resp)
logging.error((
u"Receiver app: problem sending "
u"post-save signal %s for xform %s: %s: %s"
) % (func, instance.form_id, type(resp).__name__, error_message))
errors.append(error_message)
if errors:
self.interface.xformerror_from_xform_instance(instance, ", ".join(errors))
self.formdb.update_form_problem_and_state(instance)
return errors
示例8: SubmissionPost
# 需要导入模块: from corehq.form_processor.interfaces.processor import FormProcessorInterface [as 别名]
# 或者: from corehq.form_processor.interfaces.processor.FormProcessorInterface import casedb_cache [as 别名]
#.........这里部分代码省略.........
return "\n\n".join(messages)
def run(self):
self.track_load()
failure_response = self._handle_basic_failure_modes()
if failure_response:
return FormProcessingResult(failure_response, None, [], [], 'known_failures')
result = process_xform_xml(self.domain, self.instance, self.attachments, self.auth_context.to_json())
submitted_form = result.submitted_form
self._post_process_form(submitted_form)
self._invalidate_caches(submitted_form)
if submitted_form.is_submission_error_log:
self.formdb.save_new_form(submitted_form)
response = self.get_exception_response_and_log(submitted_form, self.path)
return FormProcessingResult(response, None, [], [], 'submission_error_log')
if submitted_form.xmlns == DEVICE_LOG_XMLNS:
return self.process_device_log(submitted_form)
cases = []
ledgers = []
submission_type = 'unknown'
openrosa_kwargs = {}
with result.get_locked_forms() as xforms:
if len(xforms) > 1:
self.track_load(len(xforms) - 1)
if self.case_db:
case_db_cache = self.case_db
case_db_cache.cached_xforms.extend(xforms)
else:
case_db_cache = self.interface.casedb_cache(
domain=self.domain, lock=True, deleted_ok=True,
xforms=xforms, load_src="form_submission",
)
with case_db_cache as case_db:
instance = xforms[0]
if instance.is_duplicate:
with tracer.trace('submission.process_duplicate'):
submission_type = 'duplicate'
existing_form = xforms[1]
stub = UnfinishedSubmissionStub.objects.filter(
domain=instance.domain,
xform_id=existing_form.form_id
).first()
result = None
if stub:
from corehq.form_processor.reprocess import reprocess_unfinished_stub_with_form
result = reprocess_unfinished_stub_with_form(stub, existing_form, lock=False)
elif existing_form.is_error:
from corehq.form_processor.reprocess import reprocess_form
result = reprocess_form(existing_form, lock_form=False)
if result and result.error:
submission_type = 'error'
openrosa_kwargs['error_message'] = result.error
if existing_form.is_error:
openrosa_kwargs['error_nature'] = ResponseNature.PROCESSING_FAILURE
else:
openrosa_kwargs['error_nature'] = ResponseNature.POST_PROCESSING_FAILURE
else:
self.interface.save_processed_models([instance])
示例9: reprocess_form
# 需要导入模块: from corehq.form_processor.interfaces.processor import FormProcessorInterface [as 别名]
# 或者: from corehq.form_processor.interfaces.processor.FormProcessorInterface import casedb_cache [as 别名]
def reprocess_form(form, save=True, lock_form=True):
if lock_form:
# track load if locking; otherise it will be tracked elsewhere
form_load_counter("reprocess_form", form.domain)()
interface = FormProcessorInterface(form.domain)
lock = interface.acquire_lock_for_xform(form.form_id) if lock_form else None
with LockManager(form, lock):
logger.info('Reprocessing form: %s (%s)', form.form_id, form.domain)
# reset form state prior to processing
if should_use_sql_backend(form.domain):
form.state = XFormInstanceSQL.NORMAL
else:
form.doc_type = 'XFormInstance'
cache = interface.casedb_cache(
domain=form.domain, lock=True, deleted_ok=True, xforms=[form],
load_src="reprocess_form",
)
with cache as casedb:
try:
case_stock_result = SubmissionPost.process_xforms_for_cases([form], casedb)
except (IllegalCaseId, UsesReferrals, MissingProductId,
PhoneDateValueError, InvalidCaseIndex, CaseValueError) as e:
error_message = '{}: {}'.format(type(e).__name__, six.text_type(e))
form = interface.xformerror_from_xform_instance(form, error_message)
return ReprocessingResult(form, [], [], error_message)
form.initial_processing_complete = True
form.problem = None
stock_result = case_stock_result.stock_result
assert stock_result.populated
cases = case_stock_result.case_models
_log_changes(cases, stock_result.models_to_save, stock_result.models_to_delete)
ledgers = []
if should_use_sql_backend(form.domain):
cases_needing_rebuild = _get_case_ids_needing_rebuild(form, cases)
ledgers = stock_result.models_to_save
ledgers_updated = {ledger.ledger_reference for ledger in ledgers if ledger.is_saved()}
if save:
for case in cases:
CaseAccessorSQL.save_case(case)
LedgerAccessorSQL.save_ledger_values(ledgers)
FormAccessorSQL.update_form_problem_and_state(form)
FormProcessorSQL.publish_changes_to_kafka(ProcessedForms(form, None), cases, stock_result)
# rebuild cases and ledgers that were affected
for case in cases:
if case.case_id in cases_needing_rebuild:
logger.info('Rebuilding case: %s', case.case_id)
if save:
# only rebuild cases that were updated
detail = FormReprocessRebuild(form_id=form.form_id)
interface.hard_rebuild_case(case.case_id, detail, lock=False)
for ledger in ledgers:
if ledger.ledger_reference in ledgers_updated:
logger.info('Rebuilding ledger: %s', ledger.ledger_reference)
if save:
# only rebuild updated ledgers
interface.ledger_processor.rebuild_ledger_state(**ledger.ledger_reference._asdict())
else:
if save:
interface.processor.save_processed_models([form], cases, stock_result)
from casexml.apps.stock.models import StockTransaction
ledgers = [
model
for model in stock_result.models_to_save
if isinstance(model, StockTransaction)
]
for ledger in ledgers:
interface.ledger_processor.rebuild_ledger_state(**ledger.ledger_reference._asdict())
save and SubmissionPost.do_post_save_actions(casedb, [form], case_stock_result)
return ReprocessingResult(form, cases, ledgers, None)