示例#1
0
    def generate_suite(self):
        # Note: the order in which things happen in this function matters

        self._add_sections([
            FormResourceContributor(self.suite, self.app, self.modules,
                                    self.build_profile_id),
            LocaleResourceContributor(self.suite, self.app, self.modules,
                                      self.build_profile_id),
            DetailContributor(self.suite, self.app, self.modules,
                              self.build_profile_id),
        ])

        if self.app.supports_practice_users and self.app.get_practice_user(
                self.build_profile_id):
            self._add_sections([
                PracticeUserRestoreContributor(self.suite, self.app,
                                               self.modules,
                                               self.build_profile_id)
            ])

        # by module
        entries = EntriesContributor(self.suite, self.app, self.modules,
                                     self.build_profile_id)
        menus = MenuContributor(self.suite, self.app, self.modules,
                                self.build_profile_id)
        remote_requests = RemoteRequestContributor(self.suite, self.app,
                                                   self.modules)

        if any(module.is_training_module for module in self.modules):
            training_menu = LocalizedMenu(id='training-root')
            training_menu.text = Text(
                locale_id=id_strings.training_module_locale())
        else:
            training_menu = None

        for module in self.modules:
            self.suite.entries.extend(entries.get_module_contributions(module))

            self.suite.menus.extend(
                menus.get_module_contributions(module, training_menu))

            self.suite.remote_requests.extend(
                remote_requests.get_module_contributions(module))

        if training_menu:
            self.suite.menus.append(training_menu)

        self._add_sections([
            FixtureContributor(self.suite, self.app, self.modules),
            SchedulerFixtureContributor(self.suite, self.app, self.modules),
        ])

        # post process
        if self.app.enable_post_form_workflow:
            WorkflowHelper(self.suite, self.app, self.modules).update_suite()
        if self.app.use_grid_menus:
            GridMenuHelper(self.suite, self.app, self.modules).update_suite()

        EntryInstances(self.suite, self.app, self.modules).update_suite()
        return self.suite.serializeDocument(pretty=True)
示例#2
0
    def generate_suite(self):
        # Note: the order in which things happen in this function matters

        self._add_sections([
            FormResourceContributor(self.suite, self.app, self.modules, self.build_profile_id),
            LocaleResourceContributor(self.suite, self.app, self.modules, self.build_profile_id),
            DetailContributor(self.suite, self.app, self.modules, self.build_profile_id),
        ])

        if self.app.supports_practice_users and self.app.get_practice_user(self.build_profile_id):
            self._add_sections([
                PracticeUserRestoreContributor(self.suite, self.app, self.modules, self.build_profile_id)
            ])

        # by module
        entries = EntriesContributor(self.suite, self.app, self.modules, self.build_profile_id)
        menus = MenuContributor(self.suite, self.app, self.modules, self.build_profile_id)
        remote_requests = RemoteRequestContributor(self.suite, self.app, self.modules)

        if any(module.is_training_module for module in self.modules):
            training_menu = LocalizedMenu(id='training-root')
            training_menu.text = Text(locale_id=id_strings.training_module_locale())
        else:
            training_menu = None

        for module in self.modules:
            self.suite.entries.extend(entries.get_module_contributions(module))

            self.suite.menus.extend(
                menus.get_module_contributions(module, training_menu)
            )

            self.suite.remote_requests.extend(remote_requests.get_module_contributions(module))

        if training_menu:
            self.suite.menus.append(training_menu)

        self._add_sections([
            FixtureContributor(self.suite, self.app, self.modules),
            SchedulerFixtureContributor(self.suite, self.app, self.modules),
        ])

        # post process
        if self.app.enable_post_form_workflow:
            WorkflowHelper(self.suite, self.app, self.modules).update_suite()
        if self.app.use_grid_menus:
            GridMenuHelper(self.suite, self.app, self.modules).update_suite()

        EntryInstances(self.suite, self.app, self.modules).update_suite()
        return self.suite.serializeDocument(pretty=True)
示例#3
0
class MenuContributor(SuiteContributorByModule):
    def get_module_contributions(self, module):
        def get_commands(excluded_form_ids):
            @memoized
            def module_uses_case():
                return module.all_forms_require_a_case()

            @memoized
            def domain_uses_usercase():
                return is_usercase_in_use(self.app.domain)

            for form in module.get_suite_forms():
                if form.unique_id in excluded_form_ids:
                    continue

                command = Command(id=id_strings.form_command(form, module))

                if form.requires_case():
                    form_datums = self.entries_helper.get_datums_meta_for_form_generic(
                        form, module)
                    var_name = next(meta.datum.id
                                    for meta in reversed(form_datums)
                                    if meta.action and meta.requires_selection)
                    case = CaseIDXPath(session_var(var_name)).case()
                else:
                    case = None

                if getattr(form, 'form_filter', None):
                    fixture_xpath = (session_var(
                        id_strings.fixture_session_var(module))
                                     if module.fixture_select.active else None)
                    interpolated_xpath = interpolate_xpath(form.form_filter,
                                                           case,
                                                           fixture_xpath,
                                                           module=module,
                                                           form=form)

                    if xpath_references_case(interpolated_xpath) and \
                            (not module_uses_case() or
                            module.put_in_root and not module.root_requires_same_case()):
                        raise CaseXPathValidationError(module=module,
                                                       form=form)

                    if xpath_references_user_case(
                            interpolated_xpath) and not domain_uses_usercase():
                        raise UserCaseXPathValidationError(module=module,
                                                           form=form)

                    command.relevant = interpolated_xpath

                if getattr(module, 'has_schedule',
                           False) and module.all_forms_require_a_case():
                    # If there is a schedule and another filter condition, disregard it...
                    # Other forms of filtering are disabled in the UI

                    schedule_filter_condition = MenuContributor._schedule_filter_conditions(
                        form, module, case)
                    if schedule_filter_condition is not None:
                        command.relevant = schedule_filter_condition

                yield command

            if hasattr(module, 'case_list') and module.case_list.show:
                yield Command(id=id_strings.case_list_command(module))

        supports_module_filter = self.app.enable_module_filtering and getattr(
            module, 'module_filter', None)

        menus = []
        if hasattr(module, 'get_menus'):
            for menu in module.get_menus(
                    supports_module_filter=supports_module_filter):
                menus.append(menu)
        else:
            from corehq.apps.app_manager.models import ShadowModule
            id_modules = [module]
            root_modules = []

            shadow_modules = [
                m for m in self.app.get_modules()
                if isinstance(m, ShadowModule) and m.source_module_id
            ]
            put_in_root = getattr(module, 'put_in_root', False)
            if not put_in_root and getattr(module, 'root_module', False):
                root_modules.append(module.root_module)
                for shadow in shadow_modules:
                    if module.root_module.unique_id == shadow.source_module_id:
                        root_modules.append(shadow)
            else:
                root_modules.append(None)
                if put_in_root and getattr(module, 'root_module', False):
                    for shadow in shadow_modules:
                        if module.root_module.unique_id == shadow.source_module_id:
                            id_modules.append(shadow)

            for id_module in id_modules:
                for root_module in root_modules:
                    menu_kwargs = {}
                    suffix = ""
                    if root_module:
                        menu_kwargs.update(
                            {'root': id_strings.menu_id(root_module)})
                        suffix = id_strings.menu_id(root_module) if isinstance(
                            root_module, ShadowModule) else ""
                    menu_kwargs.update(
                        {'id': id_strings.menu_id(id_module, suffix)})

                    if supports_module_filter:
                        menu_kwargs['relevant'] = interpolate_xpath(
                            module.module_filter)

                    if self.app.enable_localized_menu_media:
                        module_custom_icon = module.custom_icon
                        menu_kwargs.update({
                            'menu_locale_id':
                            id_strings.module_locale(module),
                            'media_image':
                            bool(len(module.all_image_paths())),
                            'media_audio':
                            bool(len(module.all_audio_paths())),
                            'image_locale_id':
                            id_strings.module_icon_locale(module),
                            'audio_locale_id':
                            id_strings.module_audio_locale(module),
                            'custom_icon_locale_id':
                            (id_strings.module_custom_icon_locale(
                                module, module_custom_icon.form)
                             if module_custom_icon
                             and not module_custom_icon.xpath else None),
                            'custom_icon_form': (module_custom_icon.form if
                                                 module_custom_icon else None),
                            'custom_icon_xpath':
                            (module_custom_icon.xpath if module_custom_icon
                             and module_custom_icon.xpath else None),
                        })
                        menu = LocalizedMenu(**menu_kwargs)
                    else:
                        menu_kwargs.update({
                            'locale_id':
                            id_strings.module_locale(module),
                            'media_image':
                            module.default_media_image,
                            'media_audio':
                            module.default_media_audio,
                        })
                        menu = Menu(**menu_kwargs)

                    excluded_form_ids = []
                    if root_module and isinstance(root_module, ShadowModule):
                        excluded_form_ids = root_module.excluded_form_ids
                    if id_module and isinstance(id_module, ShadowModule):
                        excluded_form_ids = id_module.excluded_form_ids
                    menu.commands.extend(get_commands(excluded_form_ids))

                    if len(menu.commands):
                        menus.append(menu)

        if self.app.grid_display_for_all_modules() or \
                self.app.grid_display_for_some_modules() and module.grid_display_style():
            self._give_non_root_menus_grid_style(menus)
        if self.app.use_grid_menus:
            self._give_root_menu_grid_style(menus)

        return menus
示例#4
0
    def _generate_menu(self, module, root_module, training_menu, id_module):
        # In general, `id_module` and `module` will be the same thing.
        # In the case of v1 shadow menus, `id_module` is either the current module or one of that module's shadows
        # For more information, see the note in `_generate_v1_shadow_menus`.
        from corehq.apps.app_manager.models import ShadowModule
        menu_kwargs = {}
        suffix = ""
        if id_module.is_training_module:
            menu_kwargs.update({'root': 'training-root'})
        elif root_module:
            menu_kwargs.update({'root': id_strings.menu_id(root_module)})
            suffix = id_strings.menu_id(root_module) if isinstance(
                root_module, ShadowModule) else ""
        menu_kwargs.update({'id': id_strings.menu_id(id_module, suffix)})

        # Determine relevancy
        if self.app.enable_module_filtering:
            relevancy = id_module.module_filter
            # If module has a parent, incorporate the parent's relevancy.
            # This is only necessary when the child uses display only forms.
            if id_module.put_in_root and id_module.root_module and id_module.root_module.module_filter:
                if relevancy:
                    relevancy = str(
                        XPath.and_(
                            XPath(relevancy).paren(force=True),
                            XPath(id_module.root_module.module_filter).paren(
                                force=True)))
                else:
                    relevancy = id_module.root_module.module_filter
            if relevancy:
                menu_kwargs['relevant'] = interpolate_xpath(relevancy)

        if self.app.enable_localized_menu_media:
            module_custom_icon = module.custom_icon
            menu_kwargs.update({
                'menu_locale_id':
                get_module_locale_id(module),
                'media_image':
                module.uses_image(build_profile_id=self.build_profile_id),
                'media_audio':
                module.uses_audio(build_profile_id=self.build_profile_id),
                'image_locale_id':
                id_strings.module_icon_locale(module),
                'audio_locale_id':
                id_strings.module_audio_locale(module),
                'custom_icon_locale_id':
                (id_strings.module_custom_icon_locale(
                    module, module_custom_icon.form) if module_custom_icon
                 and not module_custom_icon.xpath else None),
                'custom_icon_form':
                (module_custom_icon.form if module_custom_icon else None),
                'custom_icon_xpath':
                (module_custom_icon.xpath
                 if module_custom_icon and module_custom_icon.xpath else None),
            })
            menu = LocalizedMenu(**menu_kwargs)
        else:
            menu_kwargs.update({
                'media_image': module.default_media_image,
                'media_audio': module.default_media_audio,
                'locale_id': get_module_locale_id(module),
            })
            menu = Menu(**menu_kwargs)

        excluded_form_ids = []
        if root_module and isinstance(root_module, ShadowModule):
            excluded_form_ids = root_module.excluded_form_ids
        if id_module and isinstance(id_module, ShadowModule):
            excluded_form_ids = id_module.excluded_form_ids

        commands = self._get_commands(excluded_form_ids, module)
        if module.is_training_module and module.put_in_root and training_menu:
            training_menu.commands.extend(commands)
        else:
            menu.commands.extend(commands)
        return menu
示例#5
0
class MenuContributor(SuiteContributorByModule):

    def get_module_contributions(self, module, training_menu):
        def get_commands(excluded_form_ids):
            @memoized
            def module_uses_case():
                return module.all_forms_require_a_case()

            @memoized
            def domain_uses_usercase():
                return is_usercase_in_use(self.app.domain)

            for form in module.get_suite_forms():
                if form.unique_id in excluded_form_ids:
                    continue

                command = Command(id=id_strings.form_command(form, module))

                if form.requires_case():
                    form_datums = self.entries_helper.get_datums_meta_for_form_generic(form)
                    var_name = next(
                        meta.datum.id for meta in reversed(form_datums)
                        if meta.action and meta.requires_selection
                    )
                    case = CaseIDXPath(session_var(var_name)).case()
                else:
                    case = None

                if getattr(form, 'form_filter', None):
                    fixture_xpath = (
                        session_var(id_strings.fixture_session_var(module)) if module.fixture_select.active
                        else None
                    )
                    interpolated_xpath = interpolate_xpath(form.form_filter, case, fixture_xpath,
                        module=module, form=form)

                    if xpath_references_case(interpolated_xpath) and \
                            (not module_uses_case() or
                            module.put_in_root and not module.root_requires_same_case()):
                        raise CaseXPathValidationError(module=module, form=form)

                    if xpath_references_user_case(interpolated_xpath) and not domain_uses_usercase():
                        raise UserCaseXPathValidationError(module=module, form=form)

                    command.relevant = interpolated_xpath

                if getattr(module, 'has_schedule', False) and module.all_forms_require_a_case():
                    # If there is a schedule and another filter condition, disregard it...
                    # Other forms of filtering are disabled in the UI

                    schedule_filter_condition = MenuContributor._schedule_filter_conditions(form, module, case)
                    if schedule_filter_condition is not None:
                        command.relevant = schedule_filter_condition

                yield command

            if hasattr(module, 'case_list') and module.case_list.show:
                yield Command(id=id_strings.case_list_command(module))

        menus = []
        if hasattr(module, 'get_menus'):
            for menu in module.get_menus(build_profile_id=self.build_profile_id):
                menus.append(menu)
        else:
            from corehq.apps.app_manager.models import ShadowModule
            id_modules = [module]       # the current module and all of its shadows
            root_modules = []           # the current module's parent and all of that parent's shadows

            shadow_modules = [m for m in self.app.get_modules()
                              if isinstance(m, ShadowModule) and m.source_module_id]
            if not module.put_in_root and module.root_module:
                root_modules.append(module.root_module)
                for shadow in shadow_modules:
                    if module.root_module.unique_id == shadow.source_module_id:
                        root_modules.append(shadow)
            else:
                root_modules.append(None)
                if module.put_in_root and module.root_module:
                    for shadow in shadow_modules:
                        if module.root_module.unique_id == shadow.source_module_id:
                            id_modules.append(shadow)

            for id_module in id_modules:
                for root_module in root_modules:
                    menu_kwargs = {}
                    suffix = ""
                    if id_module.is_training_module:
                        menu_kwargs.update({'root': 'training-root'})
                    elif root_module:
                        menu_kwargs.update({'root': id_strings.menu_id(root_module)})
                        suffix = id_strings.menu_id(root_module) if isinstance(root_module, ShadowModule) else ""
                    menu_kwargs.update({'id': id_strings.menu_id(id_module, suffix)})

                    # Determine relevancy
                    if self.app.enable_module_filtering:
                        relevancy = id_module.module_filter
                        # If module has a parent, incorporate the parent's relevancy.
                        # This is only necessary when the child uses display only forms.
                        if id_module.put_in_root and id_module.root_module and id_module.root_module.module_filter:
                            if relevancy:
                                relevancy = str(XPath.and_(XPath(relevancy).paren(force=True),
                                    XPath(id_module.root_module.module_filter).paren(force=True)))
                            else:
                                relevancy = id_module.root_module.module_filter
                        if relevancy:
                            menu_kwargs['relevant'] = interpolate_xpath(relevancy)

                    if self.app.enable_localized_menu_media:
                        module_custom_icon = module.custom_icon
                        menu_kwargs.update({
                            'menu_locale_id': get_module_locale_id(module),
                            'menu_enum_text': get_module_enum_text(module),
                            'media_image': module.uses_image(build_profile_id=self.build_profile_id),
                            'media_audio': module.uses_audio(build_profile_id=self.build_profile_id),
                            'image_locale_id': id_strings.module_icon_locale(module),
                            'audio_locale_id': id_strings.module_audio_locale(module),
                            'custom_icon_locale_id': (
                                id_strings.module_custom_icon_locale(module, module_custom_icon.form)
                                if module_custom_icon and not module_custom_icon.xpath else None),
                            'custom_icon_form': (module_custom_icon.form if module_custom_icon else None),
                            'custom_icon_xpath': (module_custom_icon.xpath
                                                  if module_custom_icon and module_custom_icon.xpath else None),
                        })
                        menu = LocalizedMenu(**menu_kwargs)
                    else:
                        menu_kwargs.update({
                            'media_image': module.default_media_image,
                            'media_audio': module.default_media_audio,
                            'locale_id': get_module_locale_id(module),
                            'enum_text': get_module_enum_text(module),
                        })
                        menu = Menu(**menu_kwargs)

                    excluded_form_ids = []
                    if root_module and isinstance(root_module, ShadowModule):
                        excluded_form_ids = root_module.excluded_form_ids
                    if id_module and isinstance(id_module, ShadowModule):
                        excluded_form_ids = id_module.excluded_form_ids

                    commands = get_commands(excluded_form_ids)
                    if module.is_training_module and module.put_in_root and training_menu:
                        training_menu.commands.extend(commands)
                    else:
                        menu.commands.extend(commands)

                    if len(menu.commands):
                        menus.append(menu)

        if self.app.grid_display_for_all_modules() or \
                self.app.grid_display_for_some_modules() and module.grid_display_style():
            self._give_non_root_menus_grid_style(menus)
        if self.app.use_grid_menus:
            self._give_root_menu_grid_style(menus)

        return menus