示例#1
0
 def get_if_clause(case_count_xpath, target_command):
     return_to = session_var(RETURN_TO)
     return XPath.and_(
         return_to.count().eq(1),
         return_to.eq(XPath.string(target_command)),
         case_count_xpath
     )
示例#2
0
 def get_if_clause(case_count_xpath, target_command):
     return_to = session_var(RETURN_TO)
     return XPath.and_(
         return_to.count().eq(1),
         return_to.eq(XPath.string(target_command)),
         case_count_xpath
     )
示例#3
0
    def _get_reg_form_action(self, module):
        """
        Returns registration form action
        """
        form = self.app.get_form(module.case_list_form.form_id)

        if self.app.enable_localized_menu_media:
            case_list_form = module.case_list_form
            action = LocalizedAction(
                menu_locale_id=id_strings.case_list_form_locale(module),
                media_image=case_list_form.uses_image(
                    build_profile_id=self.build_profile_id),
                media_audio=case_list_form.uses_audio(
                    build_profile_id=self.build_profile_id),
                image_locale_id=id_strings.case_list_form_icon_locale(module),
                audio_locale_id=id_strings.case_list_form_audio_locale(module),
                stack=Stack(),
                for_action_menu=True,
            )
        else:
            action = Action(display=Display(
                text=Text(locale_id=id_strings.case_list_form_locale(module)),
                media_image=module.case_list_form.default_media_image,
                media_audio=module.case_list_form.default_media_audio,
            ),
                            stack=Stack())

        frame = PushFrame()
        frame.add_command(XPath.string(id_strings.form_command(form)))

        target_form_dm = self.entries_helper.get_datums_meta_for_form_generic(
            form)
        source_form_dm = []
        if len(module.forms):
            source_form_dm = self.entries_helper.get_datums_meta_for_form_generic(
                module.get_form(0))
        for target_meta in target_form_dm:
            if target_meta.requires_selection:
                # This is true for registration forms where the case being created is a subcase
                try:
                    [source_dm] = [
                        source_meta for source_meta in source_form_dm
                        if source_meta.case_type == target_meta.case_type
                    ]
                except ValueError:
                    pass
                else:
                    frame.add_datum(
                        StackDatum(id=target_meta.datum.id,
                                   value=session_var(source_dm.datum.id)))
            else:
                s_datum = target_meta.datum
                frame.add_datum(
                    StackDatum(id=s_datum.id, value=s_datum.function))

        frame.add_datum(
            StackDatum(id=RETURN_TO,
                       value=XPath.string(id_strings.menu_id(module))))
        action.stack.add_frame(frame)
        return action
示例#4
0
    def _add_action_to_detail(self, detail, module):
        # add form action to detail
        form = self.app.get_form(module.case_list_form.form_id)

        if self.app.enable_localized_menu_media:
            case_list_form = module.case_list_form
            detail.action = LocalizedAction(
                menu_locale_id=id_strings.case_list_form_locale(module),
                media_image=bool(len(case_list_form.all_image_paths())),
                media_audio=bool(len(case_list_form.all_audio_paths())),
                image_locale_id=id_strings.case_list_form_icon_locale(module),
                audio_locale_id=id_strings.case_list_form_audio_locale(module),
                stack=Stack(),
                for_action_menu=True,
            )
        else:
            detail.action = Action(
                display=Display(
                    text=Text(locale_id=id_strings.case_list_form_locale(module)),
                    media_image=module.case_list_form.default_media_image,
                    media_audio=module.case_list_form.default_media_audio,
                ),
                stack=Stack()
            )

        frame = PushFrame()
        frame.add_command(XPath.string(id_strings.form_command(form)))

        target_form_dm = self.entries_helper.get_datums_meta_for_form_generic(form)
        source_form_dm = self.entries_helper.get_datums_meta_for_form_generic(module.get_form(0))
        for target_meta in target_form_dm:
            if target_meta.requires_selection:
                # This is true for registration forms where the case being created is a subcase
                try:
                    [source_dm] = [
                        source_meta for source_meta in source_form_dm
                        if source_meta.case_type == target_meta.case_type
                    ]
                except ValueError:
                    message = _(
                        "The '{form}' form selected as the case list registration form "
                        "for the '{module}' module requires a '{case_type}' case. "
                        "The '{module}' must load a case of this type.").format(
                        form=form.default_name(),
                        module=module.default_name(),
                        case_type=target_meta.case_type
                    )
                    raise SuiteValidationError(message)
                else:
                    frame.add_datum(StackDatum(
                        id=target_meta.datum.id,
                        value=session_var(source_dm.datum.id))
                    )
            else:
                s_datum = target_meta.datum
                frame.add_datum(StackDatum(id=s_datum.id, value=s_datum.function))

        frame.add_datum(StackDatum(id=RETURN_TO, value=XPath.string(id_strings.menu_id(module))))
        detail.action.stack.add_frame(frame)
示例#5
0
 def owner_name(owner_id):
     groups = XPath(u"instance('groups')/groups/group")
     group = groups.select("@id", owner_id)
     return XPath.if_(
         group.count().neq(0),
         group.slash("name"),
         XPath.if_(CommCareSession.userid.eq(owner_id), CommCareSession.username, XPath.string("")),
     )
示例#6
0
 def get_if_clause(case_count_xpath, target_command):
     return_to = session_var(RETURN_TO)
     args = [
         return_to.count().eq(1),
         return_to.eq(XPath.string(target_command)),
     ]
     if case_count_xpath:
         args.append(case_count_xpath)
     return XPath.and_(*args)
示例#7
0
 def test_complex(self):
     xp = XPath.and_(
         XPath('a').eq('1'),
         XPath('b').neq(XPath.string('')),
         XPath.or_(
             XPath('c').eq(XPath.string('')),
             XPath.date('d').neq('today()')
         ))
     self.assertEqual("a = 1 and b != '' and (c = '' or date(d) != today())", xp)
示例#8
0
 def get_if_clause(case_count_xpath, target_command):
     return_to = session_var(RETURN_TO)
     args = [
         return_to.count().eq(1),
         return_to.eq(XPath.string(target_command)),
     ]
     if case_count_xpath:
         args.append(case_count_xpath)
     return XPath.and_(*args)
示例#9
0
    def _get_reg_form_action(self, module):
        """
        Returns registration form action
        """
        form = self.app.get_form(module.case_list_form.form_id)

        if self.app.enable_localized_menu_media:
            case_list_form = module.case_list_form
            action = LocalizedAction(
                menu_locale_id=id_strings.case_list_form_locale(module),
                media_image=bool(len(case_list_form.all_image_paths())),
                media_audio=bool(len(case_list_form.all_audio_paths())),
                image_locale_id=id_strings.case_list_form_icon_locale(module),
                audio_locale_id=id_strings.case_list_form_audio_locale(module),
                stack=Stack(),
                for_action_menu=True,
            )
        else:
            action = Action(
                display=Display(
                    text=Text(locale_id=id_strings.case_list_form_locale(module)),
                    media_image=module.case_list_form.default_media_image,
                    media_audio=module.case_list_form.default_media_audio,
                ),
                stack=Stack()
            )

        frame = PushFrame()
        frame.add_command(XPath.string(id_strings.form_command(form)))

        target_form_dm = self.entries_helper.get_datums_meta_for_form_generic(form)
        source_form_dm = []
        if len(module.forms):
            source_form_dm = self.entries_helper.get_datums_meta_for_form_generic(module.get_form(0))
        for target_meta in target_form_dm:
            if target_meta.requires_selection:
                # This is true for registration forms where the case being created is a subcase
                try:
                    [source_dm] = [
                        source_meta for source_meta in source_form_dm
                        if source_meta.case_type == target_meta.case_type
                    ]
                except ValueError:
                    pass
                else:
                    frame.add_datum(StackDatum(
                        id=target_meta.datum.id,
                        value=session_var(source_dm.datum.id))
                    )
            else:
                s_datum = target_meta.datum
                frame.add_datum(StackDatum(id=s_datum.id, value=s_datum.function))

        frame.add_datum(StackDatum(id=RETURN_TO, value=XPath.string(id_strings.menu_id(module))))
        action.stack.add_frame(frame)
        return action
示例#10
0
 def owner_name(owner_id):
     groups = XPath(u"instance('groups')/groups/group")
     group = groups.select('@id', owner_id)
     return XPath.if_(
         group.count().not_equals(0),
         group.slash('name'),
         XPath.if_(
             CommCareSession.userid.equals(owner_id),
             CommCareSession.username,
             XPath.string('')
         )
     )
示例#11
0
 def _get_case_search_action(module):
     relevant_kwarg = {}
     if module.search_config.search_button_display_condition:
         relevant_kwarg = dict(relevant=XPath(
             module.search_config.search_button_display_condition), )
     action = Action(display=Display(text=Text(
         locale_id=id_strings.case_search_locale(module))),
                     stack=Stack(),
                     **relevant_kwarg)
     frame = PushFrame()
     frame.add_mark()
     frame.add_command(XPath.string(id_strings.search_command(module)))
     action.stack.add_frame(frame)
     return action
示例#12
0
 def _get_auto_launch_expression(module, in_search):
     allow_auto_launch = toggles.USH_CASE_CLAIM_UPDATES.enabled(
         module.get_app().domain) and not in_search
     auto_launch_expression = "false()"
     if allow_auto_launch and module.search_config.auto_launch:
         auto_launch_expression = XPath(AUTO_LAUNCH_EXPRESSION)
     return auto_launch_expression
示例#13
0
 def test_complex(self):
     xp = XPath.and_(
         XPath('a').eq('1'),
         XPath('b').neq(XPath.string('')),
         XPath.or_(
             XPath('c').eq(XPath.string('')),
             XPath.date('d').neq('today()')))
     self.assertEqual(
         "a = 1 and b != '' and (c = '' or date(d) != today())", xp)
示例#14
0
 def get_userdata_autoselect(key, session_id, mode):
     base_xpath = session_var("data", path="user")
     xpath = session_var(key, path="user/data")
     protected_xpath = XPath.if_(
         XPath.and_(base_xpath.count().eq(1), xpath.count().eq(1)), xpath, XPath.empty_string()
     )
     datum = SessionDatum(id=session_id, function=protected_xpath)
     assertions = [
         EntriesHelper.get_assertion(
             XPath.and_(base_xpath.count().eq(1), xpath.count().eq(1)),
             "case_autoload.{0}.property_missing".format(mode),
             [key],
         ),
         EntriesHelper.get_assertion(
             CaseIDXPath(xpath).case().count().eq(1), "case_autoload.{0}.case_missing".format(mode)
         ),
     ]
     return datum, assertions
示例#15
0
 def _get_case_search_action(module):
     relevant_kwarg = {}
     if module.search_config.search_button_display_condition:
         relevant_kwarg = dict(relevant=XPath(
             module.search_config.search_button_display_condition), )
     allow_auto_launch = toggles.CASE_CLAIM_AUTOLAUNCH.enabled(
         module.get_app().domain)
     action = Action(display=Display(text=Text(
         locale_id=id_strings.case_search_locale(module))),
                     stack=Stack(),
                     auto_launch=allow_auto_launch
                     and module.search_config.auto_launch,
                     **relevant_kwarg)
     frame = PushFrame()
     frame.add_mark()
     frame.add_command(XPath.string(id_strings.search_command(module)))
     action.stack.add_frame(frame)
     return action
示例#16
0
 def _get_case_search_action(module):
     action = Action(display=Display(text=Text(
         locale_id=id_strings.case_search_locale(module))),
                     stack=Stack())
     frame = PushFrame()
     frame.add_mark()
     frame.add_command(XPath.string(id_strings.search_command(module)))
     action.stack.add_frame(frame)
     return action
示例#17
0
    def test_and_or(self):
        self.assertEqual('a and b and c', XPath.and_('a', 'b', 'c'))
        self.assertEqual('a and (b and c)',
                         XPath.and_('a', XPath.and_('b', 'c')))

        self.assertEqual('a or b or c', XPath.or_('a', 'b', 'c'))
        self.assertEqual('(a or b) or c',
                         XPath.or_(XPath.or_('a', 'b'), XPath('c')))
示例#18
0
 def _add_case_search_action_to_detail(detail, module):
     detail.action = Action(
         display=Display(
             text=Text(locale_id=id_strings.case_search_locale(module))
         ),
         stack=Stack()
     )
     frame = PushFrame()
     frame.add_command(XPath.string(id_strings.search_command(module)))
     detail.action.stack.add_frame(frame)
示例#19
0
 def _get_auto_launch_expression(module, in_search):
     allow_auto_launch = toggles.USH_CASE_CLAIM_UPDATES.enabled(
         module.get_app().domain) and not in_search
     auto_launch_expression = "false()"
     if allow_auto_launch and module.search_config.auto_launch:
         if module.is_multi_select():
             # AUTO_LAUNCH_EXPRESSION is incompatible with multi select case lists - See USH-1870
             auto_launch_expression = "true()"
         else:
             auto_launch_expression = XPath(AUTO_LAUNCH_EXPRESSION)
     return auto_launch_expression
示例#20
0
 def _get_case_search_action(module, in_search=False):
     relevant_kwarg = {}
     if not in_search and module.search_config.search_button_display_condition:
         relevant_kwarg = dict(relevant=XPath(
             module.search_config.search_button_display_condition), )
     allow_auto_launch = toggles.USH_CASE_CLAIM_UPDATES.enabled(
         module.get_app().domain) and not in_search
     action = Action(display=Display(text=Text(
         locale_id=(id_strings.case_search_again_locale(module) if in_search
                    else id_strings.case_search_locale(module)))),
                     stack=Stack(),
                     auto_launch=allow_auto_launch
                     and module.search_config.auto_launch,
                     redo_last=in_search,
                     **relevant_kwarg)
     frame = PushFrame()
     frame.add_mark()
     frame.add_command(XPath.string(id_strings.search_command(module)))
     action.stack.add_frame(frame)
     return action
示例#21
0
 def _get_case_search_action(module):
     action = Action(
         display=Display(
             text=Text(locale_id=id_strings.case_search_locale(module))
         ),
         stack=Stack()
     )
     frame = PushFrame()
     frame.add_mark()
     frame.add_command(XPath.string(id_strings.search_command(module)))
     action.stack.add_frame(frame)
     return action
示例#22
0
 def owner_name(owner_id):
     groups = XPath("instance('groups')/groups/group")
     group = groups.select('@id', owner_id)
     return XPath.if_(
         group.count().neq(0), group.slash('name'),
         XPath.if_(CommCareSession.userid.eq(owner_id),
                   CommCareSession.username, XPath.string('')))
示例#23
0
 def get_userdata_autoselect(key, session_id, mode):
     base_xpath = session_var('data', path='user')
     xpath = session_var(key, path='user/data')
     protected_xpath = XPath.if_(
         XPath.and_(base_xpath.count().eq(1), xpath.count().eq(1)),
         xpath,
         XPath.empty_string(),
     )
     datum = SessionDatum(id=session_id, function=protected_xpath)
     assertions = [
         EntriesHelper.get_assertion(
             XPath.and_(base_xpath.count().eq(1),
                        xpath.count().eq(1)),
             'case_autoload.{0}.property_missing'.format(mode),
             [key],
         ),
         EntriesHelper.get_assertion(
             CaseIDXPath(xpath).case().count().eq(1),
             'case_autoload.{0}.case_missing'.format(mode),
         )
     ]
     return datum, assertions
示例#24
0
    def to_frame(self):
        if not self.children and not self.allow_empty_frame:
            return

        frame = CreateFrame(if_clause=self.if_clause)

        for child in self.children:
            if isinstance(child, CommandId):
                frame.add_command(XPath.string(child.id))
            elif isinstance(child, StackDatum):
                frame.add_datum(child)
            else:
                raise Exception("Unexpected child type: {} ({})".format(type(child), child))

        return frame
示例#25
0
 def _get_case_search_action(module, in_search=False):
     action_kwargs = DetailContributor._get_action_kwargs(module, in_search)
     action = Action(
         display=Display(text=Text(locale_id=(
             id_strings.case_search_again_locale(module)
             if in_search else id_strings.case_search_locale(module)))),
         stack=Stack(),
         auto_launch=DetailContributor._get_auto_launch_expression(
             module, in_search),
         redo_last=in_search,
         **action_kwargs)
     frame = PushFrame()
     frame.add_mark()
     frame.add_command(XPath.string(id_strings.search_command(module)))
     action.stack.add_frame(frame)
     return action
示例#26
0
    def to_frame(self):
        if not self.children and not self.allow_empty_frame:
            return

        frame = CreateFrame(if_clause=self.if_clause)

        for child in self.children:
            if isinstance(child, CommandId):
                frame.add_command(XPath.string(child.id))
            elif isinstance(child, StackDatum):
                frame.add_datum(child)
            else:
                raise Exception("Unexpected child type: {} ({})".format(
                    type(child), child))

        return frame
示例#27
0
    def _make_session_endpoint(self, id_string, module, endpoint_id):
        stack = Stack()
        frame = PushFrame()
        stack.add_frame(frame)
        frame.add_command(XPath.string(id_string))
        arguments = []
        helper = WorkflowHelper(self.suite, self.app, self.modules)
        for child in helper.get_frame_children(id_string, module):
            if isinstance(child, WorkflowDatumMeta):
                arguments.append(Argument(id=child.id))
                frame.add_datum(StackDatum(id=child.id, value=f"${child.id}"))

        return SessionEndpoint(
            id=endpoint_id,
            arguments=arguments,
            stack=stack,
        )
示例#28
0
 def _get_case_search_action(module):
     relevant_kwarg = {}
     if module.search_config.search_button_display_condition:
         relevant_kwarg = dict(
             relevant=XPath(module.search_config.search_button_display_condition),
         )
     action = Action(
         display=Display(
             text=Text(locale_id=id_strings.case_search_locale(module))
         ),
         stack=Stack(),
         **relevant_kwarg
     )
     frame = PushFrame()
     frame.add_mark()
     frame.add_command(XPath.string(id_strings.search_command(module)))
     action.stack.add_frame(frame)
     return action
示例#29
0
 def build_stack(self):
     stack = Stack()
     rewind_if = None
     if module_uses_smart_links(self.module):
         user_domain_xpath = session_var(COMMCARE_PROJECT, path="user/data")
         # For case in same domain, do a regular case claim rewind
         rewind_if = self._get_case_domain_xpath().eq(user_domain_xpath)
         # For case in another domain, jump to that other domain
         frame = PushFrame(if_clause=XPath.not_(rewind_if))
         frame.add_datum(
             StackJump(url=Text(xpath=TextXPath(
                 function=self.get_smart_link_function(),
                 variables=self.get_smart_link_variables(),
             ), ), ))
         stack.add_frame(frame)
     frame = PushFrame(if_clause=rewind_if)
     frame.add_rewind(QuerySessionXPath(self.case_session_var).instance())
     stack.add_frame(frame)
     return stack
示例#30
0
    def _get_case_search_action(self, module, in_search=False):
        action_kwargs = DetailContributor._get_action_kwargs(module, in_search)
        if in_search:
            search_label = module.search_config.search_again_label
        else:
            search_label = module.search_config.search_label

        if self.app.enable_localized_menu_media:
            action = LocalizedAction(
                menu_locale_id=(id_strings.case_search_again_locale(module)
                                if in_search else
                                id_strings.case_search_locale(module)),
                media_image=search_label.uses_image(
                    build_profile_id=self.build_profile_id),
                media_audio=search_label.uses_audio(
                    build_profile_id=self.build_profile_id),
                image_locale_id=(
                    id_strings.case_search_again_icon_locale(module) if
                    in_search else id_strings.case_search_icon_locale(module)),
                audio_locale_id=(
                    id_strings.case_search_again_audio_locale(module)
                    if in_search else
                    id_strings.case_search_audio_locale(module)),
                stack=Stack(),
                for_action_menu=True,
                **action_kwargs,
            )
        else:
            action = Action(display=Display(
                text=Text(locale_id=(
                    id_strings.case_search_again_locale(module)
                    if in_search else id_strings.case_search_locale(module))),
                media_image=search_label.default_media_image,
                media_audio=search_label.default_media_audio),
                            stack=Stack(),
                            **action_kwargs)
        frame = PushFrame()
        frame.add_mark()
        frame.add_command(XPath.string(id_strings.search_command(module)))
        action.stack.add_frame(frame)
        return action
示例#31
0
 def test_if(self):
     self.assertEqual('if(a, b, c)', XPath.if_('a', 'b', 'c'))
示例#32
0
    def test_and_or(self):
        self.assertEqual('a and b and c', XPath.and_('a', 'b', 'c'))
        self.assertEqual('a and (b and c)', XPath.and_('a', XPath.and_('b', 'c')))

        self.assertEqual('a or b or c', XPath.or_('a', 'b', 'c'))
        self.assertEqual('(a or b) or c', XPath.or_(XPath.or_('a', 'b'), XPath('c')))
示例#33
0
    def configure_entry_careplan_form(self, module, e, form=None, **kwargs):
        parent_module = self.app.get_module_by_unique_id(module.parent_select.module_id)
        e.datums.append(
            SessionDatum(
                id="case_id",
                nodeset=EntriesHelper.get_nodeset_xpath(parent_module.case_type),
                value="./@case_id",
                detail_select=self.details_helper.get_detail_id_safe(parent_module, "case_short"),
                detail_confirm=self.details_helper.get_detail_id_safe(parent_module, "case_long"),
            )
        )

        def session_datum(datum_id, case_type, parent_ref, parent_val):
            nodeset = (
                CaseTypeXpath(case_type)
                .case()
                .select("index/%s" % parent_ref, session_var(parent_val), quote=False)
                .select("@status", "open")
            )
            return SessionDatum(
                id=datum_id,
                nodeset=nodeset,
                value="./@case_id",
                detail_select=self.details_helper.get_detail_id_safe(module, "%s_short" % case_type),
                detail_confirm=self.details_helper.get_detail_id_safe(module, "%s_long" % case_type),
            )

        e.stack = Stack()
        frame = CreateFrame()
        e.stack.add_frame(frame)
        if form.case_type == CAREPLAN_GOAL:
            if form.mode == "create":
                new_goal_id_var = "case_id_goal_new"
                e.datums.append(SessionDatum(id=new_goal_id_var, function="uuid()"))
            elif form.mode == "update":
                new_goal_id_var = "case_id_goal"
                e.datums.append(session_datum(new_goal_id_var, CAREPLAN_GOAL, "parent", "case_id"))

            if not module.display_separately:
                open_goal = CaseIDXPath(session_var(new_goal_id_var)).case().select("@status", "open")
                frame.if_clause = "{count} = 1".format(count=open_goal.count())
                frame.add_command(XPath.string(id_strings.menu_id(parent_module)))
                frame.add_datum(StackDatum(id="case_id", value=session_var("case_id")))
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id="case_id_goal", value=session_var(new_goal_id_var)))
            else:
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id="case_id", value=session_var("case_id")))

        elif form.case_type == CAREPLAN_TASK:
            if not module.display_separately:
                frame.add_command(XPath.string(id_strings.menu_id(parent_module)))
                frame.add_datum(StackDatum(id="case_id", value=session_var("case_id")))
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id="case_id_goal", value=session_var("case_id_goal")))
                if form.mode == "update":
                    count = (
                        CaseTypeXpath(CAREPLAN_TASK)
                        .case()
                        .select("index/goal", session_var("case_id_goal"), quote=False)
                        .select("@status", "open")
                        .count()
                    )
                    frame.if_clause = "{count} >= 1".format(count=count)

                    frame.add_command(
                        XPath.string(id_strings.form_command(module.get_form_by_type(CAREPLAN_TASK, "update")))
                    )
            else:
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id="case_id", value=session_var("case_id")))

            if form.mode == "create":
                e.datums.append(session_datum("case_id_goal", CAREPLAN_GOAL, "parent", "case_id"))
            elif form.mode == "update":
                e.datums.append(session_datum("case_id_goal", CAREPLAN_GOAL, "parent", "case_id"))
                e.datums.append(session_datum("case_id_task", CAREPLAN_TASK, "goal", "case_id_goal"))
示例#34
0
 def test_paren(self):
     xp = XPath('/data/q1')
     self.assertEqual('/data/q1', xp.paren())
     self.assertEqual('(/data/q1)', xp.paren(force=True))
     self.assertEqual('(/data/q1)',
                      XPath('/data/q1', compound=True).paren())
示例#35
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
示例#36
0
 def test_int(self):
     self.assertEqual('int(a)', XPath.int('a'))
示例#37
0
    def configure_entry_careplan_form(self, module, e, form=None, **kwargs):
        parent_module = self.app.get_module_by_unique_id(module.parent_select.module_id)
        e.datums.append(SessionDatum(
            id='case_id',
            nodeset=EntriesHelper.get_nodeset_xpath(parent_module.case_type),
            value="./@case_id",
            detail_select=self.details_helper.get_detail_id_safe(parent_module, 'case_short'),
            detail_confirm=self.details_helper.get_detail_id_safe(parent_module, 'case_long')
        ))

        def session_datum(datum_id, case_type, parent_ref, parent_val):
            nodeset = CaseTypeXpath(case_type).case().select(
                'index/%s' % parent_ref, session_var(parent_val), quote=False
            ).select('@status', 'open')
            return SessionDatum(
                id=datum_id,
                nodeset=nodeset,
                value="./@case_id",
                detail_select=self.details_helper.get_detail_id_safe(module, '%s_short' % case_type),
                detail_confirm=self.details_helper.get_detail_id_safe(module, '%s_long' % case_type)
            )

        e.stack = Stack()
        frame = CreateFrame()
        e.stack.add_frame(frame)
        if form.case_type == CAREPLAN_GOAL:
            if form.mode == 'create':
                new_goal_id_var = 'case_id_goal_new'
                e.datums.append(SessionDatum(id=new_goal_id_var, function='uuid()'))
            elif form.mode == 'update':
                new_goal_id_var = 'case_id_goal'
                e.datums.append(session_datum(new_goal_id_var, CAREPLAN_GOAL, 'parent', 'case_id'))

            if not module.display_separately:
                open_goal = CaseIDXPath(session_var(new_goal_id_var)).case().select('@status', 'open')
                frame.if_clause = '{count} = 1'.format(count=open_goal.count())
                frame.add_command(XPath.string(id_strings.menu_id(parent_module)))
                frame.add_datum(StackDatum(id='case_id', value=session_var('case_id')))
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id='case_id_goal', value=session_var(new_goal_id_var)))
            else:
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id='case_id', value=session_var('case_id')))

        elif form.case_type == CAREPLAN_TASK:
            if not module.display_separately:
                frame.add_command(XPath.string(id_strings.menu_id(parent_module)))
                frame.add_datum(StackDatum(id='case_id', value=session_var('case_id')))
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id='case_id_goal', value=session_var('case_id_goal')))
                if form.mode == 'update':
                    count = CaseTypeXpath(CAREPLAN_TASK).case().select(
                        'index/goal', session_var('case_id_goal'), quote=False
                    ).select('@status', 'open').count()
                    frame.if_clause = '{count} >= 1'.format(count=count)

                    frame.add_command(XPath.string(
                        id_strings.form_command(module.get_form_by_type(CAREPLAN_TASK, 'update'))
                    ))
            else:
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id='case_id', value=session_var('case_id')))

            if form.mode == 'create':
                e.datums.append(session_datum('case_id_goal', CAREPLAN_GOAL, 'parent', 'case_id'))
            elif form.mode == 'update':
                e.datums.append(session_datum('case_id_goal', CAREPLAN_GOAL, 'parent', 'case_id'))
                e.datums.append(session_datum('case_id_task', CAREPLAN_TASK, 'goal', 'case_id_goal'))
示例#38
0
 def test_int(self):
     self.assertEqual('int(a)', XPath.int('a'))
示例#39
0
 def test_paren(self):
     xp = XPath('/data/q1')
     self.assertEqual('/data/q1', xp.paren())
     self.assertEqual('(/data/q1)', xp.paren(force=True))
     self.assertEqual('(/data/q1)', XPath('/data/q1', compound=True).paren())
示例#40
0
    def get_module_contributions(self, module):
        if module_offers_search(module):
            domain = self.app.domain

            details_helper = DetailsHelper(self.app)

            sync_request = SyncRequest(
                post=SyncRequestPost(
                    url=absolute_reverse('claim_case', args=[domain]),
                    # Check whether the case to be claimed already exists in casedb:
                    # count(
                    #   instance('casedb')/casedb/case[@case_id=instance('querysession')/session/data/case_id]
                    # ) = 0
                    relevant=XPath.count(
                        CaseIDXPath(QuerySessionXPath('case_id').instance()).case()
                    ) + ' = 0',
                    data=[
                        QueryData(
                            key='case_id',
                            ref=QuerySessionXPath('case_id').instance(),
                            # e.g. instance('querysession')/session/data/case_id
                        ),
                    ]
                ),

                command=Command(
                    id=id_strings.search_command(module),
                    display=Display(
                        text=Text(locale_id=id_strings.case_search_locale(module)),
                    ),
                ),

                instances=[Instance(
                    id=SESSION_INSTANCE,
                    src='jr://instance/session'
                )],

                session=SyncRequestSession(
                    queries=[
                        SyncRequestQuery(
                            url=absolute_reverse('sync_search', args=[domain]),
                            storage_instance=RESULTS_INSTANCE,
                            data=[
                                QueryData(
                                    key='case_type',
                                    ref="'{}'".format(module.case_type)
                                ),
                            ],
                            prompts=[
                                QueryPrompt(
                                    key=p.name,
                                    display=Display(
                                        text=Text(locale_id=id_strings.search_property_locale(module, p.name)),
                                    ),
                                ) for p in module.search_config.properties
                            ]
                        )
                    ],
                    data=[SessionDatum(
                        id='case_id',
                        nodeset=(CaseTypeXpath(module.case_type)
                                 .case(instance_name=RESULTS_INSTANCE)
                                 .select(u'@status', u'open', quote=True)),
                        value='./@case_id',
                        detail_select=details_helper.get_detail_id_safe(module, 'case_short'),
                        detail_confirm=details_helper.get_detail_id_safe(module, 'case_long'),
                    )],
                ),

                stack=Stack(),
            )

            frame = PushFrame()
            # Open first form in module
            frame.add_command(XPath.string(id_strings.menu_id(module)))
            frame.add_datum(StackDatum(id=CALCULATED_DATA, value=XPath.string(MARK_AS_CLAIMED)))
            sync_request.stack.add_frame(frame)

            return [sync_request]
        return []
示例#41
0
 def test_count(self):
     self.assertEqual('count(/data/a)', XPath('/data/a').count())
示例#42
0
 def test_select(self):
     self.assertEqual("/data/1[anything]",
                      XPath('/data/1').select_raw('anything'))
     self.assertEqual("/data/1[a='b']", XPath('/data/1').select('a', 'b'))
     self.assertEqual("/data/1[a=/data/b]",
                      XPath('/data/1').select('a', XPath('/data/b')))
示例#43
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
示例#44
0
 def test_not(self):
     self.assertEqual('not a', XPath.not_('a'))
     self.assertEqual('not (a or b)', XPath.not_(XPath.or_('a', 'b')))
示例#45
0
 def test_not(self):
     self.assertEqual('not a', XPath.not_('a'))
     self.assertEqual('not (a or b)', XPath.not_(XPath.or_('a', 'b')))
示例#46
0
 def test_date(self):
     self.assertEqual('date(a)', XPath.date('a'))
示例#47
0
 def test_date(self):
     self.assertEqual('date(a)', XPath.date('a'))
示例#48
0
 def test_eq_neq(self):
     self.assertEqual('a = b', XPath('a').eq('b'))
     self.assertEqual('a != b', XPath('a').neq('b'))
示例#49
0
    def configure_entry_careplan_form(self, module, e, form=None, **kwargs):
        parent_module = self.app.get_module_by_unique_id(module.parent_select.module_id,
                        error=_("Could not find module '{}' is attached to.").format(module.default_name()))
        e.datums.append(SessionDatum(
            id='case_id',
            nodeset=EntriesHelper.get_nodeset_xpath(parent_module.case_type),
            value="./@case_id",
            detail_select=self.details_helper.get_detail_id_safe(parent_module, 'case_short'),
            detail_confirm=self.details_helper.get_detail_id_safe(parent_module, 'case_long')
        ))

        def session_datum(datum_id, case_type, parent_ref, parent_val):
            nodeset = CaseTypeXpath(case_type).case().select(
                'index/%s' % parent_ref, session_var(parent_val), quote=False
            ).select('@status', 'open')
            return SessionDatum(
                id=datum_id,
                nodeset=nodeset,
                value="./@case_id",
                detail_select=self.details_helper.get_detail_id_safe(module, '%s_short' % case_type),
                detail_confirm=self.details_helper.get_detail_id_safe(module, '%s_long' % case_type)
            )

        e.stack = Stack()
        frame = CreateFrame()
        e.stack.add_frame(frame)
        if form.case_type == CAREPLAN_GOAL:
            if form.mode == 'create':
                new_goal_id_var = 'case_id_goal_new'
                e.datums.append(SessionDatum(id=new_goal_id_var, function='uuid()'))
            elif form.mode == 'update':
                new_goal_id_var = 'case_id_goal'
                e.datums.append(session_datum(new_goal_id_var, CAREPLAN_GOAL, 'parent', 'case_id'))

            if not module.display_separately:
                open_goal = CaseIDXPath(session_var(new_goal_id_var)).case().select('@status', 'open')
                frame.if_clause = '{count} = 1'.format(count=open_goal.count())
                frame.add_command(XPath.string(id_strings.menu_id(parent_module)))
                frame.add_datum(StackDatum(id='case_id', value=session_var('case_id')))
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id='case_id_goal', value=session_var(new_goal_id_var)))
            else:
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id='case_id', value=session_var('case_id')))

        elif form.case_type == CAREPLAN_TASK:
            if not module.display_separately:
                frame.add_command(XPath.string(id_strings.menu_id(parent_module)))
                frame.add_datum(StackDatum(id='case_id', value=session_var('case_id')))
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id='case_id_goal', value=session_var('case_id_goal')))
                if form.mode == 'update':
                    count = CaseTypeXpath(CAREPLAN_TASK).case().select(
                        'index/goal', session_var('case_id_goal'), quote=False
                    ).select('@status', 'open').count()
                    frame.if_clause = '{count} >= 1'.format(count=count)

                    frame.add_command(XPath.string(
                        id_strings.form_command(module.get_form_by_type(CAREPLAN_TASK, 'update'))
                    ))
            else:
                frame.add_command(XPath.string(id_strings.menu_id(module)))
                frame.add_datum(StackDatum(id='case_id', value=session_var('case_id')))

            if form.mode == 'create':
                e.datums.append(session_datum('case_id_goal', CAREPLAN_GOAL, 'parent', 'case_id'))
            elif form.mode == 'update':
                e.datums.append(session_datum('case_id_goal', CAREPLAN_GOAL, 'parent', 'case_id'))
                e.datums.append(session_datum('case_id_task', CAREPLAN_TASK, 'goal', 'case_id_goal'))
示例#50
0
 def test_slash(self):
     self.assertEqual('/data/1/2',
                      XPath().slash('/data').slash('1').slash('2'))
     self.assertEqual('/data/1/2', XPath('/data').slash('1').slash('2'))
示例#51
0
 def test_if(self):
     self.assertEqual('if(a, b, c)', XPath.if_('a', 'b', 'c'))