本文整理汇总了Python中corehq.form_processor.backends.sql.dbaccessors.LedgerAccessorSQL类的典型用法代码示例。如果您正苦于以下问题:Python LedgerAccessorSQL类的具体用法?Python LedgerAccessorSQL怎么用?Python LedgerAccessorSQL使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LedgerAccessorSQL类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_form_archived
def process_form_archived(self, form):
from corehq.form_processor.parsers.ledgers.form import get_ledger_references_from_stock_transactions
refs_to_rebuild = get_ledger_references_from_stock_transactions(form)
case_ids = list({ref.case_id for ref in refs_to_rebuild})
LedgerAccessorSQL.delete_ledger_transactions_for_form(case_ids, form.form_id)
for ref in refs_to_rebuild:
self.rebuild_ledger_state(**ref._asdict())
示例2: _create_models_for_stock_report_helper
def _create_models_for_stock_report_helper(self, form, stock_report_helper):
processing_result = StockProcessingResult(form, stock_report_helpers=[stock_report_helper])
processing_result.populate_models()
if should_use_sql_backend(self.domain_name):
from corehq.form_processor.backends.sql.dbaccessors import LedgerAccessorSQL
LedgerAccessorSQL.save_ledger_values(processing_result.models_to_save)
else:
processing_result.commit()
示例3: process_form_unarchived
def process_form_unarchived(self, form):
from corehq.apps.commtrack.processing import process_stock
result = process_stock([form])
result.populate_models()
LedgerAccessorSQL.save_ledger_values(result.models_to_save)
for ledger_value in result.models_to_save:
publish_ledger_v2_saved(ledger_value)
refs_to_rebuild = {ledger_value.ledger_reference for ledger_value in result.models_to_save}
for ref in refs_to_rebuild:
self.rebuild_ledger_state(**ref._asdict())
result.finalize()
示例4: delete_all_ledgers
def delete_all_ledgers(domain):
if should_use_sql_backend(domain):
for case_id in CaseAccessorSQL.get_case_ids_in_domain(domain):
transactions = LedgerAccessorSQL.get_ledger_transactions_for_case(case_id)
form_ids = {tx.form_id for tx in transactions}
for form_id in form_ids:
LedgerAccessorSQL.delete_ledger_transactions_for_form([case_id], form_id)
LedgerAccessorSQL.delete_ledger_values(case_id)
else:
from casexml.apps.stock.models import StockReport
from casexml.apps.stock.models import StockTransaction
stock_report_ids = StockReport.objects.filter(domain=domain).values_list('id', flat=True)
StockReport.objects.filter(domain=domain).delete()
StockTransaction.objects.filter(report_id__in=stock_report_ids).delete()
示例5: update_case_transactions_for_form
def update_case_transactions_for_form(case_cache, live_case_updates, deprecated_case_updates,
live_form, deprecated_form):
for case_update in live_case_updates + deprecated_case_updates:
case_id = case_update.id
count, _ = CaseTransaction.objects.partitioned_query(case_id)\
.filter(case_id=case_id, form_id=live_form.form_id).delete()
rebuild_transactions = CaseTransaction.objects.partitioned_query(case_id).filter(
case_id=case_id, type=CaseTransaction.TYPE_REBUILD_FORM_EDIT
)
for transaction in rebuild_transactions:
if transaction.details.get('deprecated_form_id') == deprecated_form.original_form_id:
transaction.delete()
for case_update in live_case_updates:
case_id = case_update.id
case = case_cache.get(case_id)
SqlCaseUpdateStrategy.add_transaction_for_form(case, case_update, live_form)
for case_update in deprecated_case_updates:
case_id = case_update.id
case = case_cache.get(case_id)
SqlCaseUpdateStrategy.add_transaction_for_form(case, case_update, deprecated_form)
stock_result = process_stock([live_form, deprecated_form], case_cache)
stock_result.populate_models()
affected_ledgers = set()
affected_cases = set()
ledger_transactions = []
for ledger_value in stock_result.models_to_save:
affected_ledgers.add(ledger_value.ledger_reference)
affected_cases.add(ledger_value.case_id)
for transaction in ledger_value.get_tracked_models_to_create(LedgerTransaction):
ledger_transactions.append(transaction)
if affected_cases:
LedgerAccessorSQL.delete_ledger_transactions_for_form(list(affected_cases), live_form.form_id)
for transaction in ledger_transactions:
transaction.save()
for case in case_cache.cache.values():
affected_cases.add(case.case_id)
transactions = case.get_tracked_models_to_create(CaseTransaction)
for transaction in transactions:
transaction.case = case
transaction.save()
return affected_cases, affected_ledgers
示例6: test_delete_ledger_values_case_section
def test_delete_ledger_values_case_section(self):
from corehq.apps.commtrack.tests.util import get_single_balance_block
form_id = self._submit_ledgers([
get_single_balance_block(self.case_one.case_id, product_id, 10)
for product_id in [self.product_a._id, self.product_b._id]
])
ledger_values = LedgerAccessorSQL.get_ledger_values_for_case(self.case_one.case_id)
self.assertEqual(2, len(ledger_values))
LedgerAccessorSQL.delete_ledger_transactions_for_form([self.case_one.case_id], form_id)
deleted = LedgerAccessorSQL.delete_ledger_values(self.case_one.case_id, 'stock')
self.assertEqual(2, deleted)
ledger_values = LedgerAccessorSQL.get_ledger_values_for_case(self.case_one.case_id)
self.assertEqual(0, len(ledger_values))
示例7: _get_daily_consumption_for_ledger
def _get_daily_consumption_for_ledger(ledger):
from corehq.apps.commtrack.consumption import get_consumption_for_ledger_json
daily_consumption = get_consumption_for_ledger_json(ledger)
if should_use_sql_backend(ledger['domain']):
from corehq.form_processor.backends.sql.dbaccessors import LedgerAccessorSQL
ledger_value = LedgerAccessorSQL.get_ledger_value(
ledger['case_id'], ledger['section_id'], ledger['entry_id']
)
ledger_value.daily_consumption = daily_consumption
LedgerAccessorSQL.save_ledger_values([ledger_value])
else:
from corehq.apps.commtrack.models import StockState
StockState.objects.filter(pk=ledger['_id']).update(daily_consumption=daily_consumption)
return daily_consumption
示例8: test_delete_ledger_values_case_section_1
def test_delete_ledger_values_case_section_1(self):
from corehq.apps.commtrack.tests import get_single_balance_block
form_id = self._submit_ledgers([
get_single_balance_block(self.case_one.case_id, self.product_a._id, 10, section_id=section_id)
for section_id in ['stock', 'consumption']
])
ledger_values = LedgerAccessorSQL.get_ledger_values_for_case(self.case_one.case_id)
self.assertEqual(2, len(ledger_values))
LedgerAccessorSQL.delete_ledger_transactions_for_form([self.case_one.case_id], form_id)
deleted = LedgerAccessorSQL.delete_ledger_values(self.case_one.case_id, 'stock')
self.assertEqual(1, deleted)
ledger_values = LedgerAccessorSQL.get_ledger_values_for_case(self.case_one.case_id)
self.assertEqual(1, len(ledger_values))
self.assertEqual('consumption', ledger_values[0].section_id)
示例9: get_models_to_update
def get_models_to_update(self, stock_report_helper, ledger_db=None):
latest_values = {}
for stock_trans in stock_report_helper.transactions:
def _lazy_original_balance():
# needs to be in closures because it's zero-argument.
# see compute_ledger_values for more information
if stock_trans.ledger_reference in latest_values:
return latest_values[stock_trans.ledger_reference]
else:
return self.get_current_ledger_value(stock_trans.ledger_reference)
new_ledger_values = compute_ledger_values(
_lazy_original_balance, stock_report_helper.report_type, stock_trans.relative_quantity
)
latest_values[stock_trans.ledger_reference] = new_ledger_values.balance
to_save = []
for touched_ledger_reference, quantity in latest_values.items():
try:
ledger_value = LedgerAccessorSQL.get_ledger_value(**touched_ledger_reference._asdict())
except LedgerValue.DoesNotExist:
ledger_value = LedgerValue(**touched_ledger_reference._asdict())
ledger_value.balance = quantity
to_save.append(ledger_value)
return StockModelUpdateResult(to_save=to_save)
示例10: save_processed_models
def save_processed_models(cls, processed_forms, cases=None, stock_result=None):
with transaction.atomic():
logging.debug('Beginning atomic commit\n')
# Save deprecated form first to avoid ID conflicts
if processed_forms.deprecated:
FormAccessorSQL.save_deprecated_form(processed_forms.deprecated)
FormAccessorSQL.save_new_form(processed_forms.submitted)
if cases:
for case in cases:
CaseAccessorSQL.save_case(case)
if stock_result:
ledgers_to_save = stock_result.models_to_save
LedgerAccessorSQL.save_ledger_values(ledgers_to_save, processed_forms.deprecated)
cls._publish_changes(processed_forms, cases, stock_result)
示例11: save_processed_models
def save_processed_models(cls, processed_forms, cases=None, stock_result=None, publish_to_kafka=True):
db_names = {processed_forms.submitted.db}
if processed_forms.deprecated:
db_names |= {processed_forms.deprecated.db}
if cases:
db_names |= {case.db for case in cases}
if stock_result:
db_names |= {
ledger_value.db for ledger_value in stock_result.models_to_save
}
with ExitStack() as stack:
for db_name in db_names:
stack.enter_context(transaction.atomic(db_name))
# Save deprecated form first to avoid ID conflicts
if processed_forms.deprecated:
FormAccessorSQL.update_form(processed_forms.deprecated, publish_changes=False)
FormAccessorSQL.save_new_form(processed_forms.submitted)
if cases:
for case in cases:
CaseAccessorSQL.save_case(case)
if stock_result:
ledgers_to_save = stock_result.models_to_save
LedgerAccessorSQL.save_ledger_values(ledgers_to_save, stock_result)
if cases:
sort_submissions = toggles.SORT_OUT_OF_ORDER_FORM_SUBMISSIONS_SQL.enabled(
processed_forms.submitted.domain, toggles.NAMESPACE_DOMAIN)
if sort_submissions:
for case in cases:
if SqlCaseUpdateStrategy(case).reconcile_transactions_if_necessary():
CaseAccessorSQL.save_case(case)
if publish_to_kafka:
try:
cls.publish_changes_to_kafka(processed_forms, cases, stock_result)
except Exception as e:
raise KafkaPublishingError(e)
示例12: setUp
def setUp(self):
# can't do this in setUpClass until Django 1.9 since @override_settings
# doesn't apply to classmethods
from corehq.apps.commtrack.tests import get_single_balance_block
factory = CaseFactory(domain=self.domain)
self.case = factory.create_case()
submit_case_blocks([
get_single_balance_block(self.case.case_id, self.product._id, 10)
], self.domain)
ledger_values = LedgerAccessorSQL.get_ledger_values_for_case(self.case.case_id)
self.assertEqual(1, len(ledger_values))
示例13: test_delete_ledger_transactions_for_form
def test_delete_ledger_transactions_for_form(self):
from corehq.apps.commtrack.tests.util import get_single_balance_block
self._set_balance(100, self.case_one.case_id, self.product_a._id)
case_ids = [self.case_one.case_id, self.case_two.case_id]
form_id = self._submit_ledgers([
get_single_balance_block(case_id, product_id, 10)
for case_id in case_ids
for product_id in [self.product_a._id, self.product_b._id]
])
deleted = LedgerAccessorSQL.delete_ledger_transactions_for_form(case_ids, form_id)
self.assertEqual(4, deleted)
self.assertEqual(
1,
len(LedgerAccessorSQL.get_ledger_transactions_for_case(self.case_one.case_id))
)
self.assertEqual(
0,
len(LedgerAccessorSQL.get_ledger_transactions_for_case(self.case_two.case_id))
)
示例14: hard_rebuild_ledgers
def hard_rebuild_ledgers(case_id, section_id=None, entry_id=None):
transactions = LedgerAccessorSQL.get_ledger_transactions_for_case(case_id, section_id, entry_id)
if not transactions:
LedgerAccessorSQL.delete_ledger_values(case_id, section_id, entry_id)
return
ledger_value = LedgerAccessorSQL.get_ledger_value(case_id, section_id, entry_id)
ledger_value = LedgerProcessorSQL._rebuild_ledger_value_from_transactions(ledger_value, transactions)
LedgerAccessorSQL.save_ledger_values([ledger_value])
publish_ledger_v2_saved(ledger_value)
示例15: test_ledgers
def test_ledgers(self):
expected_object_counts = Counter({
XFormInstanceSQL: 3,
BlobMeta: 3,
CommCareCaseSQL: 1,
CaseTransaction: 3,
LedgerValue: 1,
LedgerTransaction: 2
})
case = self.factory.create_case()
submit_case_blocks([
get_single_balance_block(case.case_id, self.product._id, 10)
], self.domain_name)
submit_case_blocks([
get_single_balance_block(case.case_id, self.product._id, 5)
], self.domain_name)
pre_ledger_values = LedgerAccessorSQL.get_ledger_values_for_case(case.case_id)
pre_ledger_transactions = LedgerAccessorSQL.get_ledger_transactions_for_case(case.case_id)
self.assertEqual(1, len(pre_ledger_values))
self.assertEqual(2, len(pre_ledger_transactions))
self._dump_and_load(expected_object_counts)
post_ledger_values = LedgerAccessorSQL.get_ledger_values_for_case(case.case_id)
post_ledger_transactions = LedgerAccessorSQL.get_ledger_transactions_for_case(case.case_id)
self.assertEqual(1, len(post_ledger_values))
self.assertEqual(2, len(post_ledger_transactions))
self.assertEqual(pre_ledger_values[0].ledger_reference, post_ledger_values[0].ledger_reference)
self.assertDictEqual(pre_ledger_values[0].to_json(), post_ledger_values[0].to_json())
pre_ledger_transactions = sorted(pre_ledger_transactions, key=lambda t: t.pk)
post_ledger_transactions = sorted(post_ledger_transactions, key=lambda t: t.pk)
for pre, post in zip(pre_ledger_transactions, post_ledger_transactions):
self.assertEqual(str(pre), str(post))