Ejemplo n.º 1
0
    def get_datum_meta_assertions_advanced(self, module, form):
        def get_target_module(case_type, module_id, with_product_details=False):
            if module_id:
                if module_id == module.unique_id:
                    return module

                from corehq.apps.app_manager.models import ModuleNotFoundException
                try:
                    target = module.get_app().get_module_by_unique_id(module_id,
                             error=_("Could not find target module used by form '{}'").format(form.default_name()))
                    if target.case_type != case_type:
                        raise ParentModuleReferenceError(
                            _("Form '%s' in module '%s' references a module with an incorrect case type: "
                              "module '%s' expected '%s', found '%s'") % (
                                form.default_name(),
                                module.default_name(),
                                target.default_name(),
                                case_type,
                                target.case_type,
                            )
                        )
                    if with_product_details and not hasattr(target, 'product_details'):
                        raise ParentModuleReferenceError(
                            "Module with ID %s has no product details configuration" % module_id
                        )
                    return target
                except ModuleNotFoundException as ex:
                    raise ParentModuleReferenceError(ex.message)
            else:
                if case_type == module.case_type:
                    return module

                target_modules = [
                    mod for mod in module.get_app().modules
                    if mod.case_type == case_type and (not with_product_details or hasattr(mod, 'product_details'))
                ]
                try:
                    return target_modules[0]
                except IndexError:
                    raise ParentModuleReferenceError(
                        "Module with case type %s in app %s not found" % (case_type, self.app)
                    )

        def get_manual_datum(action_, parent_filter_=''):
            target_module_ = get_target_module(action_.case_type, action_.details_module)
            referenced_by = form.actions.actions_meta_by_parent_tag.get(action_.case_tag)
            filter_xpath = EntriesHelper.get_filter_xpath(target_module_)

            return SessionDatum(
                id=action_.case_session_var,
                nodeset=(EntriesHelper.get_nodeset_xpath(action_.case_type, filter_xpath=filter_xpath)
                         + parent_filter_),
                value="./@case_id",
                detail_select=self.details_helper.get_detail_id_safe(target_module_, 'case_short'),
                detail_confirm=(
                    self.details_helper.get_detail_id_safe(target_module_, 'case_long')
                    if not referenced_by or referenced_by['type'] != 'load' else None
                ),
                detail_persistent=self.get_detail_persistent_attr(target_module_, target_module_, "case_short"),
                detail_inline=self.get_detail_inline_attr(target_module_, target_module_, "case_short"),
                autoselect=target_module_.auto_select_case,
            )

        datums = []
        assertions = []
        for action in form.actions.get_load_update_actions():
            auto_select = action.auto_select
            load_case_from_fixture = action.load_case_from_fixture
            if auto_select and auto_select.mode:
                datum, assertions = EntriesHelper.get_auto_select_datums_and_assertions(action, auto_select, form)
                datums.append(FormDatumMeta(
                    datum=datum,
                    case_type=None,
                    requires_selection=False,
                    action=action
                ))
            elif load_case_from_fixture:
                target_module = get_target_module(action.case_type, action.details_module)
                datums.extend(self.get_load_case_from_fixture_datums(action, target_module, form))
            else:
                if action.case_index.tag:
                    parent_action = form.actions.actions_meta_by_tag[action.case_index.tag]['action']
                    parent_filter = EntriesHelper.get_parent_filter(
                        action.case_index.reference_id,
                        parent_action.case_session_var
                    )
                else:
                    parent_filter = ''
                datums.append(FormDatumMeta(
                    datum=get_manual_datum(action, parent_filter),
                    case_type=action.case_type,
                    requires_selection=True,
                    action=action
                ))

        if module.get_app().commtrack_enabled:
            try:
                last_action = list(form.actions.get_load_update_actions())[-1]
                if last_action.show_product_stock:
                    nodeset = ProductInstanceXpath().instance()
                    if last_action.product_program:
                        nodeset = nodeset.select('program_id', last_action.product_program)

                    target_module = get_target_module(last_action.case_type, last_action.details_module, True)

                    datums.append(FormDatumMeta(
                        datum=SessionDatum(
                            id='product_id',
                            nodeset=nodeset,
                            value="./@id",
                            detail_select=self.details_helper.get_detail_id_safe(target_module, 'product_short'),
                            detail_persistent=self.get_detail_persistent_attr(
                                target_module, target_module, "product_short"
                            ),
                            detail_inline=self.get_detail_inline_attr(
                                target_module, target_module, "product_short"
                            ),
                        ),
                        case_type=None,
                        requires_selection=True,
                        action=None
                    ))
            except IndexError:
                pass

        self.add_parent_datums(datums, module)

        return datums, assertions
Ejemplo n.º 2
0
    def get_datum_meta_assertions_advanced(self, module, form):
        def get_target_module(case_type, module_id, with_product_details=False):
            if module_id:
                if module_id == module.unique_id:
                    return module

                from corehq.apps.app_manager.models import ModuleNotFoundException

                try:
                    target = module.get_app().get_module_by_unique_id(module_id)
                    if target.case_type != case_type:
                        raise ParentModuleReferenceError("Module with ID %s has incorrect case type" % module_id)
                    if with_product_details and not hasattr(target, "product_details"):
                        raise ParentModuleReferenceError(
                            "Module with ID %s has no product details configuration" % module_id
                        )
                    return target
                except ModuleNotFoundException as ex:
                    raise ParentModuleReferenceError(ex.message)
            else:
                if case_type == module.case_type:
                    return module

                target_modules = [
                    mod
                    for mod in module.get_app().modules
                    if mod.case_type == case_type and (not with_product_details or hasattr(mod, "product_details"))
                ]
                try:
                    return target_modules[0]
                except IndexError:
                    raise ParentModuleReferenceError(
                        "Module with case type %s in app %s not found" % (case_type, self.app)
                    )

        def get_manual_datum(action_, parent_filter_=""):
            target_module_ = get_target_module(action_.case_type, action_.details_module)
            referenced_by = form.actions.actions_meta_by_parent_tag.get(action_.case_tag)
            filter_xpath = EntriesHelper.get_filter_xpath(target_module_)
            detail_persistent, detail_inline = self.get_case_tile_datum_attrs(target_module_, target_module_)

            return SessionDatum(
                id=action_.case_session_var,
                nodeset=(
                    EntriesHelper.get_nodeset_xpath(action_.case_type, filter_xpath=filter_xpath) + parent_filter_
                ),
                value="./@case_id",
                detail_select=self.details_helper.get_detail_id_safe(target_module_, "case_short"),
                detail_confirm=(
                    self.details_helper.get_detail_id_safe(target_module_, "case_long")
                    if not referenced_by or referenced_by["type"] != "load"
                    else None
                ),
                detail_persistent=detail_persistent,
                detail_inline=detail_inline,
            )

        datums = []
        assertions = []
        for action in form.actions.get_load_update_actions():
            auto_select = action.auto_select
            if auto_select and auto_select.mode:
                datum, assertions = EntriesHelper.get_auto_select_datums_and_assertions(action, auto_select, form)
                datums.append(FormDatumMeta(datum=datum, case_type=None, requires_selection=False, action=action))
            else:
                if action.case_index.tag:
                    parent_action = form.actions.actions_meta_by_tag[action.case_index.tag]["action"]
                    parent_filter = EntriesHelper.get_parent_filter(
                        action.case_index.reference_id, parent_action.case_session_var
                    )
                else:
                    parent_filter = ""
                datums.append(
                    FormDatumMeta(
                        datum=get_manual_datum(action, parent_filter),
                        case_type=action.case_type,
                        requires_selection=True,
                        action=action,
                    )
                )

        if module.get_app().commtrack_enabled:
            try:
                last_action = list(form.actions.get_load_update_actions())[-1]
                if last_action.show_product_stock:
                    nodeset = ProductInstanceXpath().instance()
                    if last_action.product_program:
                        nodeset = nodeset.select("program_id", last_action.product_program)

                    target_module = get_target_module(last_action.case_type, last_action.details_module, True)

                    datums.append(
                        FormDatumMeta(
                            datum=SessionDatum(
                                id="product_id",
                                nodeset=nodeset,
                                value="./@id",
                                detail_select=self.details_helper.get_detail_id_safe(target_module, "product_short"),
                            ),
                            case_type=None,
                            requires_selection=True,
                            action=None,
                        )
                    )
            except IndexError:
                pass

        self.add_parent_datums(datums, module)

        return datums, assertions
Ejemplo n.º 3
0
    def entry_for_module(self, module):
        # avoid circular dependency
        from corehq.apps.app_manager.models import Module, AdvancedModule
        results = []
        for form in module.get_suite_forms():
            e = Entry()
            e.form = form.xmlns
            # Ideally all of this version check should happen in Command/Display class
            if self.app.enable_localized_menu_media:
                e.command = LocalizedCommand(
                    id=id_strings.form_command(form, module),
                    menu_locale_id=id_strings.form_locale(form),
                    media_image=bool(len(form.all_image_paths())),
                    media_audio=bool(len(form.all_audio_paths())),
                    image_locale_id=id_strings.form_icon_locale(form),
                    audio_locale_id=id_strings.form_audio_locale(form),
                )
            else:
                e.command = Command(
                    id=id_strings.form_command(form, module),
                    locale_id=id_strings.form_locale(form),
                    media_image=form.default_media_image,
                    media_audio=form.default_media_audio,
                )
            config_entry = {
                'module_form': self.configure_entry_module_form,
                'advanced_form': self.configure_entry_advanced_form,
                'careplan_form': self.configure_entry_careplan_form,
            }[form.form_type]
            config_entry(module, e, form)

            if (
                self.app.commtrack_enabled and
                session_var('supply_point_id') in getattr(form, 'source', "")
            ):
                from corehq.apps.app_manager.models import AUTO_SELECT_LOCATION
                datum, assertions = EntriesHelper.get_userdata_autoselect(
                    'commtrack-supply-point',
                    'supply_point_id',
                    AUTO_SELECT_LOCATION,
                )
                e.datums.append(datum)
                e.assertions.extend(assertions)

            results.append(e)

        if hasattr(module, 'case_list') and module.case_list.show:
            e = Entry()
            if self.app.enable_localized_menu_media:
                e.command = LocalizedCommand(
                    id=id_strings.case_list_command(module),
                    menu_locale_id=id_strings.case_list_locale(module),
                    media_image=bool(len(module.case_list.all_image_paths())),
                    media_audio=bool(len(module.case_list.all_audio_paths())),
                    image_locale_id=id_strings.case_list_icon_locale(module),
                    audio_locale_id=id_strings.case_list_audio_locale(module),
                )
            else:
                e.command = Command(
                    id=id_strings.case_list_command(module),
                    locale_id=id_strings.case_list_locale(module),
                    media_image=module.case_list.default_media_image,
                    media_audio=module.case_list.default_media_audio,
                )
            if isinstance(module, Module):
                for datum_meta in self.get_case_datums_basic_module(module):
                    e.datums.append(datum_meta.datum)
            elif isinstance(module, AdvancedModule):
                e.datums.append(SessionDatum(
                    id='case_id_case_%s' % module.case_type,
                    nodeset=(EntriesHelper.get_nodeset_xpath(module.case_type)),
                    value="./@case_id",
                    detail_select=self.details_helper.get_detail_id_safe(module, 'case_short'),
                    detail_confirm=self.details_helper.get_detail_id_safe(module, 'case_long'),
                    detail_persistent=self.get_detail_persistent_attr(module, module, "case_short"),
                    detail_inline=self.get_detail_inline_attr(module, module, "case_short"),
                    autoselect=module.auto_select_case,
                ))
                if self.app.commtrack_enabled:
                    e.datums.append(SessionDatum(
                        id='product_id',
                        nodeset=ProductInstanceXpath().instance(),
                        value="./@id",
                        detail_select=self.details_helper.get_detail_id_safe(module, 'product_short')
                    ))
            results.append(e)

        for entry in module.get_custom_entries():
            results.append(entry)

        return results
Ejemplo n.º 4
0
    def get_datum_meta_assertions_advanced(self, module, form):
        def get_target_module(case_type, module_id, with_product_details=False):
            if module_id:
                if module_id == module.unique_id:
                    return module

                from corehq.apps.app_manager.models import ModuleNotFoundException
                try:
                    target = module.get_app().get_module_by_unique_id(module_id,
                             error=_("Could not find target module used by form '{}'").format(form.default_name()))
                    if target.case_type != case_type:
                        raise ParentModuleReferenceError(
                            _(
                                "Form '%(form_name)s' in module '%(module_name)s' "
                                "references a module with an incorrect case type: "
                                "module '%(target_name)s' expected '%(expected_case_type)s', "
                                "found '%(target_case_type)s'"
                            ) % {
                                'form_name': form.default_name(),
                                'module_name': module.default_name(),
                                'target_name': target.default_name(),
                                'expected_case_type': case_type,
                                'target_case_type': target.case_type,
                            }
                        )
                    if with_product_details and not hasattr(target, 'product_details'):
                        raise ParentModuleReferenceError(
                            "Module with ID %s has no product details configuration" % module_id
                        )
                    return target
                except ModuleNotFoundException as ex:
                    raise ParentModuleReferenceError(ex.message)
            else:
                if case_type == module.case_type:
                    return module

                target_modules = [
                    mod for mod in module.get_app().modules
                    if mod.case_type == case_type and (not with_product_details or hasattr(mod, 'product_details'))
                ]
                try:
                    return target_modules[0]
                except IndexError:
                    raise ParentModuleReferenceError(
                        "Module with case type %s in app %s not found" % (case_type, self.app)
                    )

        def get_manual_datum(action_, parent_filter_=''):
            target_module_ = get_target_module(action_.case_type, action_.details_module)
            referenced_by = form.actions.actions_meta_by_parent_tag.get(action_.case_tag)
            filter_xpath = EntriesHelper.get_filter_xpath(target_module_)
            detail_inline = self.get_detail_inline_attr(target_module_, target_module_, "case_short")

            return SessionDatum(
                id=action_.case_session_var,
                nodeset=(EntriesHelper.get_nodeset_xpath(action_.case_type, filter_xpath=filter_xpath)
                         + parent_filter_),
                value="./@case_id",
                detail_select=self.details_helper.get_detail_id_safe(target_module_, 'case_short'),
                detail_confirm=(
                    self.details_helper.get_detail_id_safe(target_module_, 'case_long')
                    if (not referenced_by or referenced_by['type'] != 'load') and not detail_inline else None
                ),
                detail_persistent=self.get_detail_persistent_attr(target_module_, target_module_, "case_short"),
                detail_inline=detail_inline,
                autoselect=target_module_.auto_select_case,
            )

        datums = []
        assertions = []
        for action in form.actions.get_load_update_actions():
            auto_select = action.auto_select
            load_case_from_fixture = action.load_case_from_fixture
            if auto_select and auto_select.mode:
                datum, assertions = EntriesHelper.get_auto_select_datums_and_assertions(action, auto_select, form)
                datums.append(FormDatumMeta(
                    datum=datum,
                    case_type=None,
                    requires_selection=False,
                    action=action
                ))
            elif load_case_from_fixture:
                target_module = get_target_module(action.case_type, action.details_module)
                datums.extend(self.get_load_case_from_fixture_datums(action, target_module, form))
            else:
                if action.case_index.tag:
                    parent_action = form.actions.actions_meta_by_tag[action.case_index.tag]['action']
                    parent_filter = EntriesHelper.get_parent_filter(
                        action.case_index.reference_id,
                        parent_action.case_session_var
                    )
                else:
                    parent_filter = ''
                datums.append(FormDatumMeta(
                    datum=get_manual_datum(action, parent_filter),
                    case_type=action.case_type,
                    requires_selection=True,
                    action=action
                ))

        if module.get_app().commtrack_enabled:
            try:
                last_action = list(form.actions.get_load_update_actions())[-1]
                if last_action.show_product_stock:
                    nodeset = ProductInstanceXpath().instance()
                    if last_action.product_program:
                        nodeset = nodeset.select('program_id', last_action.product_program)

                    target_module = get_target_module(last_action.case_type, last_action.details_module, True)

                    datums.append(FormDatumMeta(
                        datum=SessionDatum(
                            id='product_id',
                            nodeset=nodeset,
                            value="./@id",
                            detail_select=self.details_helper.get_detail_id_safe(target_module, 'product_short'),
                            detail_persistent=self.get_detail_persistent_attr(
                                target_module, target_module, "product_short"
                            ),
                            detail_inline=self.get_detail_inline_attr(
                                target_module, target_module, "product_short"
                            ),
                        ),
                        case_type=None,
                        requires_selection=True,
                        action=None
                    ))
            except IndexError:
                pass

        self.add_parent_datums(datums, module)

        return datums, assertions