<commit_before>from collections import namedtuple
import logging
from django.db import transaction
from django.utils.translation import ugettext as _
from casexml.apps.case.const import CASE_ACTION_COMMTRACK
from casexml.apps.case.exceptions import IllegalCaseId
from casexml.apps.case.xform import is_device_report, CaseDbCache
from casexml.apps.stock.models import StockTransaction, StockReport
from corehq.apps.commtrack.parsing import unpack_commtrack
from couchforms.util import is_deprecation
from dimagi.utils.decorators.log_exception import log_exception
from casexml.apps.case.models import CommCareCaseAction
from casexml.apps.case.xml.parser import AbstractAction
from casexml.apps.stock import const as stockconst


logger = logging.getLogger('commtrack.incoming')

COMMTRACK_LEGACY_REPORT_XMLNS = 'http://commtrack.org/legacy/stock_report'


class StockProcessingResult(object):
    """
    An object used to collect the changes that are made during stock
    processing so that they can be made more atomic
    """

    def __init__(self, xform, relevant_cases=None, stock_report_helpers=None):
        self.domain = xform.domain
        self.xform = xform
        self.relevant_cases = relevant_cases or []
        self.stock_report_helpers = stock_report_helpers or []

    @transaction.atomic
    def commit(self):
        """
        Commit changes to the database
        """
        # if cases were changed we should purge the sync token cache
        # this ensures that ledger updates will sync back down
        if self.relevant_cases and self.xform.get_sync_token():
            self.xform.get_sync_token().invalidate_cached_payloads()

        # create the django models
        for stock_report_helper in self.stock_report_helpers:
            if stock_report_helper.deprecated:
                delete_models_for_stock_report(self.domain, stock_report_helper)
            else:
                create_models_for_stock_report(self.domain, stock_report_helper)


@transaction.atomic
def delete_models_for_stock_report(domain, stock_report_helper):
    """
    Delete all stock reports and stock transaction models associated with the helper from the database.
    """
    assert stock_report_helper.domain == domain
    StockReport.objects.filter(domain=domain, form_id=stock_report_helper.form_id).delete()


@transaction.atomic
@transaction.atomic
def create_models_for_stock_report(domain, stock_report_helper):
    """
    Save stock report and stock transaction models to the database.<commit_msg>remove redundant decorator<commit_after>from collections import namedtuple
import logging
from django.db import transaction
from django.utils.translation import ugettext as _
from casexml.apps.case.const import CASE_ACTION_COMMTRACK
from casexml.apps.case.exceptions import IllegalCaseId
from casexml.apps.case.xform import is_device_report, CaseDbCache
from casexml.apps.stock.models import StockTransaction, StockReport
from corehq.apps.commtrack.parsing import unpack_commtrack
from couchforms.util import is_deprecation
from dimagi.utils.decorators.log_exception import log_exception
from casexml.apps.case.models import CommCareCaseAction
from casexml.apps.case.xml.parser import AbstractAction
from casexml.apps.stock import const as stockconst


logger = logging.getLogger('commtrack.incoming')

COMMTRACK_LEGACY_REPORT_XMLNS = 'http://commtrack.org/legacy/stock_report'


class StockProcessingResult(object):
    """
    An object used to collect the changes that are made during stock
    processing so that they can be made more atomic
    """

    def __init__(self, xform, relevant_cases=None, stock_report_helpers=None):
        self.domain = xform.domain
        self.xform = xform
        self.relevant_cases = relevant_cases or []
        self.stock_report_helpers = stock_report_helpers or []

    @transaction.atomic
    def commit(self):
        """
        Commit changes to the database
        """
        # if cases were changed we should purge the sync token cache
        # this ensures that ledger updates will sync back down
        if self.relevant_cases and self.xform.get_sync_token():
            self.xform.get_sync_token().invalidate_cached_payloads()

        # create the django models
        for stock_report_helper in self.stock_report_helpers:
            if stock_report_helper.deprecated:
                delete_models_for_stock_report(self.domain, stock_report_helper)
            else:
                create_models_for_stock_report(self.domain, stock_report_helper)


@transaction.atomic
def delete_models_for_stock_report(domain, stock_report_helper):
    """
    Delete all stock reports and stock transaction models associated with the helper from the database.
    """
    assert stock_report_helper.domain == domain
    StockReport.objects.filter(domain=domain, form_id=stock_report_helper.form_id).delete()


@transaction.atomic
def create_models_for_stock_report(domain, stock_report_helper):
    """
    Save stock report and stock transaction models to the database.
{'fraction_non_alphanumeric': 0.06342913776015857, 'fraction_numerical': 0.0, 'mean_word_length': 4.0109235352532275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4650838', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1351, 'n_words': 376}