Example #1
0
    def testSignal(self):
        global archive_counter, restore_counter
        archive_counter = 0
        restore_counter = 0

        def count_archive(**kwargs):
            global archive_counter
            archive_counter += 1

        def count_unarchive(**kwargs):
            global restore_counter
            restore_counter += 1

        xform_archived.connect(count_archive)
        xform_unarchived.connect(count_unarchive)

        xml_data = self.get_xml('basic')
        result = submit_form_locally(
            xml_data,
            'test-domain',
        )

        self.assertEqual(0, archive_counter)
        self.assertEqual(0, restore_counter)

        result.xform.archive()
        self.assertEqual(1, archive_counter)
        self.assertEqual(0, restore_counter)

        xform = self.formdb.get_form(result.xform.form_id)
        xform.unarchive()
        self.assertEqual(1, archive_counter)
        self.assertEqual(1, restore_counter)
Example #2
0
    def testSignal(self):
        global archive_counter, restore_counter
        archive_counter = 0
        restore_counter = 0

        def count_archive(**kwargs):
            global archive_counter
            archive_counter += 1

        def count_unarchive(**kwargs):
            global restore_counter
            restore_counter += 1

        xform_archived.connect(count_archive)
        xform_unarchived.connect(count_unarchive)

        xml_data = self.get_xml('basic')
        result = submit_form_locally(
            xml_data,
            'test-domain',
        )

        self.assertEqual(0, archive_counter)
        self.assertEqual(0, restore_counter)

        result.xform.archive()
        self.assertEqual(1, archive_counter)
        self.assertEqual(0, restore_counter)

        xform = self.formdb.get_form(result.xform.form_id)
        xform.unarchive()
        self.assertEqual(1, archive_counter)
        self.assertEqual(1, restore_counter)
Example #3
0
    def testSignal(self):
        global archive_counter, restore_counter
        archive_counter = 0
        restore_counter = 0

        def count_archive(**kwargs):
            global archive_counter
            archive_counter += 1

        def count_unarchive(**kwargs):
            global restore_counter
            restore_counter += 1


        xform_archived.connect(count_archive)
        xform_unarchived.connect(count_unarchive)

        form = XFormInstance(form={'foo': 'bar'})
        form.save()
        self.assertEqual(0, archive_counter)
        self.assertEqual(0, restore_counter)

        form.archive()
        self.assertEqual(1, archive_counter)
        self.assertEqual(0, restore_counter)

        form.unarchive()
        self.assertEqual(1, archive_counter)
        self.assertEqual(1, restore_counter)
Example #4
0
    def testSignal(self):
        global archive_counter, restore_counter
        archive_counter = 0
        restore_counter = 0

        def count_archive(**kwargs):
            global archive_counter
            archive_counter += 1

        def count_unarchive(**kwargs):
            global restore_counter
            restore_counter += 1

        xform_archived.connect(count_archive)
        xform_unarchived.connect(count_unarchive)

        form = XFormInstance(form={'foo': 'bar'})
        form.save()
        self.assertEqual(0, archive_counter)
        self.assertEqual(0, restore_counter)

        form.archive()
        self.assertEqual(1, archive_counter)
        self.assertEqual(0, restore_counter)

        form.unarchive()
        self.assertEqual(1, archive_counter)
        self.assertEqual(1, restore_counter)
Example #5
0
    def testSignal(self):
        global archive_counter, restore_counter
        archive_counter = 0
        restore_counter = 0

        def count_archive(**kwargs):
            global archive_counter
            archive_counter += 1

        def count_unarchive(**kwargs):
            global restore_counter
            restore_counter += 1

        xform_archived.connect(count_archive)
        xform_unarchived.connect(count_unarchive)

        xml_data = self.get_xml('basic')
        response, xform, cases = self.interface.submit_form_locally(
            xml_data,
            'test-domain',
        )

        self.assertEqual(0, archive_counter)
        self.assertEqual(0, restore_counter)

        xform.archive()
        self.assertEqual(1, archive_counter)
        self.assertEqual(0, restore_counter)

        xform.unarchive()
        self.assertEqual(1, archive_counter)
        self.assertEqual(1, restore_counter)
Example #6
0
    def testSignal(self):
        global archive_counter, restore_counter
        archive_counter = 0
        restore_counter = 0

        def count_archive(**kwargs):
            global archive_counter
            archive_counter += 1

        def count_unarchive(**kwargs):
            global restore_counter
            restore_counter += 1

        xform_archived.connect(count_archive)
        xform_unarchived.connect(count_unarchive)

        xform = FormProcessorInterface.create_from_generic(
            GenericXFormInstance(form={'foo': 'bar'}),
            GenericFormAttachment(name='form.xml', content='<data/>')
        )

        self.assertEqual(0, archive_counter)
        self.assertEqual(0, restore_counter)

        FormProcessorInterface.archive_xform(xform)
        self.assertEqual(1, archive_counter)
        self.assertEqual(0, restore_counter)

        FormProcessorInterface.unarchive_xform(xform)
        self.assertEqual(1, archive_counter)
        self.assertEqual(1, restore_counter)
Example #7
0
    def handle(self, log_file, **options):
        to_archive = []
        cases_affected = set()
        episode_case_ids = CaseAccessors(domain).get_case_ids_in_domain(
            "episode")
        if options.get('limit'):
            episode_case_ids = episode_case_ids[:options.get('limit')]
        for episode_case_id in with_progress_bar(episode_case_ids):
            nikshay_to_archive = []
            dots_99_to_archvie = []
            case_forms = FormProcessorInterface(domain).get_case_forms(
                episode_case_id)
            for form in case_forms:
                if form.user_id in ("system", "", None
                                    ) and form.metadata.username == "system":
                    updates = get_case_updates(form)
                    update_actions = [
                        update.get_update_action() for update in updates
                        if update.id == episode_case_id
                    ]
                    for action in update_actions:
                        if isinstance(action, CaseUpdateAction):
                            if set(action.dynamic_properties.keys()) == {
                                    "nikshay_registered", "nikshay_error"
                            }:
                                nikshay_to_archive.append(form)
                                cases_affected.add(episode_case_id)
                            elif set(action.dynamic_properties.keys()) == {
                                    "dots_99_registered", "dots_99_error"
                            }:
                                cases_affected.add(episode_case_id)
                                dots_99_to_archvie.append(form)

            # get_case_updates() returns the forms in the correct order, but sorting is probably a good idea in case
            # get_case_updates ever changes.
            nikshay_to_archive.sort(key=lambda f: f.received_on)
            dots_99_to_archvie.sort(key=lambda f: f.received_on)
            nikshay_to_archive = nikshay_to_archive[:-1]
            dots_99_to_archvie = dots_99_to_archvie[:-1]

            to_archive.extend(nikshay_to_archive)
            to_archive.extend(dots_99_to_archvie)

        print("Will archive {} forms".format(len(to_archive)))

        xform_archived.disconnect(rebuild_form_cases)
        with open(log_file, "w") as f:
            for form in with_progress_bar(to_archive):
                f.write(form.form_id + "\n")
                f.flush()
                if options['commit']:
                    form.archive(user_id="remove_duplicate_forms_script")
        xform_archived.connect(rebuild_form_cases)

        print("Will rebuild {} cases".format(len(cases_affected)))
        for case_id in with_progress_bar(cases_affected):
            rebuild_case_from_forms(
                domain, case_id,
                UserRequestedRebuild(user_id="remove_duplicate_forms_script"))
Example #8
0
    def handle(self, log_file, **options):
        to_archive = []
        case_id = options.get('case_id')
        case_forms = FormProcessorInterface(domain).get_case_forms(case_id)
        for form in case_forms:
            if form.user_id in ("system", "",
                                None) and form.metadata.username == "system":
                updates = get_case_updates(form)
                if options.get('case_property') is not None:
                    update_actions = [
                        update.get_update_action() for update in updates
                        if update.id == case_id
                    ]
                    for action in update_actions:
                        if isinstance(action, CaseUpdateAction):
                            if options.get('case_property') in set(
                                    action.dynamic_properties.keys()):
                                to_archive.append(form)
                else:
                    to_archive.append(form)

        to_archive.sort(key=lambda f: f.received_on)
        to_archive = to_archive[:-1]

        print("Will archive {} forms".format(len(to_archive)))

        xform_archived.disconnect(rebuild_form_cases)
        with open(log_file, "w") as f:
            for form in with_progress_bar(to_archive):
                f.write(form.form_id + "\n")
                f.flush()
                if options['commit']:
                    form.archive(
                        user_id="archive_single_case_repeater_forms_script")
        xform_archived.connect(rebuild_form_cases)

        rebuild_case_from_forms(
            domain, case_id,
            UserRequestedRebuild(
                user_id="archive_single_case_repeater_forms_script"))
Example #9
0
from casexml.apps.stock.models import StockTransaction
from corehq.form_processor.models import FormArchiveRebuild
from couchforms.signals import xform_archived, xform_unarchived


def rebuild_form_cases(sender, xform, *args, **kwargs):
    from casexml.apps.case.xform import get_case_ids_from_form
    from casexml.apps.case.cleanup import rebuild_case_from_forms
    from corehq.form_processor.parsers.ledgers.form import get_case_ids_from_stock_transactions

    domain = xform.domain
    case_ids = get_case_ids_from_form(
        xform) | get_case_ids_from_stock_transactions(xform)
    detail = FormArchiveRebuild(form_id=xform.form_id,
                                archived=xform.is_archived)
    for case_id in case_ids:
        rebuild_case_from_forms(domain, case_id, detail)


xform_archived.connect(rebuild_form_cases)
xform_unarchived.connect(rebuild_form_cases)

# any time a case is saved
case_post_save = Signal(providing_args=["case"])

# only when one or more cases are updated as the result of an xform submission
# the contract of this signal is that you should modify the form and cases in
# place but NOT save them. this is so that we can avoid multiple redundant writes
# to the database in a row. we may want to revisit this if it creates problems.
cases_received = Signal(providing_args=["xform", "cases"])
Example #10
0
    process_cases(xform, config)


@log_exception()
def _process_cases(sender, xform, config=None, **kwargs):
    from casexml.apps.case import process_cases
    process_cases(xform, config)


successful_form_received.connect(_process_cases)


def rebuild_form_cases(sender, xform, *args, **kwargs):
    from casexml.apps.case.xform import get_case_ids_from_form
    from casexml.apps.case.cleanup import rebuild_case
    for case_id in get_case_ids_from_form(xform):
        rebuild_case(case_id)


xform_archived.connect(rebuild_form_cases)
xform_unarchived.connect(rebuild_form_cases)

# any time a case is saved
case_post_save = Signal(providing_args=["case"])

# only when one or more cases are updated as the result of an xform submission
# the contract of this signal is that you should modify the form and cases in
# place but NOT save them. this is so that we can avoid multiple redundant writes
# to the database in a row. we may want to revisit this if it creates problems.
cases_received = Signal(providing_args=["xform", "cases"])