Exemple #1
0
    def _get_stack_frames(self, target_module, form, source_form_datums):
        """
        Set up the stack blocks for a single case list form action.
        :param target_module: Module that the user is returning to
        :param form: Case list form
        :param source_form_datums: List of datum from the case list form
        :return: CaseListFormStackFrames object
        """
        from corehq.apps.app_manager.const import WORKFLOW_CASE_LIST
        source_session_var = self._get_source_session_var(form, target_module.case_type)
        source_case_id = session_var(source_session_var)
        case_count = CaseIDXPath(source_case_id).case().count()
        target_command = id_strings.menu_id(target_module)

        if target_module.case_list_form.post_form_workflow == WORKFLOW_CASE_LIST:
            frame_case_created = None
            frame_case_not_created = StackFrameMeta(
                self.get_if_clause(None, target_command), current_session=source_form_datums
            )
        else:
            frame_case_created = StackFrameMeta(
                self.get_if_clause(case_count.gt(0), target_command), current_session=source_form_datums
            )
            frame_case_not_created = StackFrameMeta(
                self.get_if_clause(case_count.eq(0), target_command), current_session=source_form_datums
            )

        stack_frames = CaseListFormStackFrames(
            case_created=frame_case_created, case_not_created=frame_case_not_created
        )
        stack_frames.source_session_var = source_session_var
        return stack_frames
Exemple #2
0
 def _get_stack_frames(self, target_module, form, source_form_datums):
     """
     Set up the stack blocks for a single case list form action.
     :param target_module: Module that the user is returning to
     :param form: Case list form
     :param source_form_datums: List of datum from the case list form
     :return: CaseListFormStackFrames object
     """
     source_session_var = self._get_source_session_var(form, target_module.case_type)
     source_case_id = session_var(source_session_var)
     case_count = CaseIDXPath(source_case_id).case().count()
     target_command = id_strings.menu_id(target_module)
     frame_case_created = StackFrameMeta(
         self.get_if_clause(case_count.gt(0), target_command), current_session=source_form_datums
     )
     frame_case_not_created = StackFrameMeta(
         self.get_if_clause(case_count.eq(0), target_command), current_session=source_form_datums
     )
     stack_frames = CaseListFormStackFrames(
         case_created=frame_case_created, case_not_created=frame_case_not_created
     )
     stack_frames.source_session_var = source_session_var
     return stack_frames
Exemple #3
0
    def get_stack_frames_for_case_list_form_target(self, target_module, form):
        stack_frames = []
        target_command = id_strings.menu_id(target_module)

        if form.form_type == 'module_form':
            [reg_action] = form.get_registration_actions(target_module.case_type)
            source_session_var = form.session_var_for_action(reg_action)
        if form.form_type == 'advanced_form':
            # match case session variable
            reg_action = form.get_registration_actions(target_module.case_type)[0]
            source_session_var = reg_action.case_session_var
        source_case_id = session_var(source_session_var)
        case_count = CaseIDXPath(source_case_id).case().count()
        frame_case_created = StackFrameMeta(self.get_if_clause(case_count.gt(0), target_command))
        stack_frames.append(frame_case_created)
        frame_case_not_created = StackFrameMeta(self.get_if_clause(case_count.eq(0), target_command))
        stack_frames.append(frame_case_not_created)

        def add_datums_for_target(module, source_form_dm, allow_missing=False):
            """
            Given a target module and a list of datums from the source module add children
            to the stack frames that are required by the target module and present in the source datums
            list.
            """
            target_form_dm = self.helper.get_frame_children(module.get_form(0), module_only=True)

            used = set()

            for source_meta in source_form_dm:
                if source_meta.case_type:
                    # This is true for registration forms where the case being created is a subcase
                    try:
                        target_dm = self.get_target_dm(target_form_dm, source_meta.case_type, module)
                    except SuiteError:
                        if source_meta.requires_selection:
                            raise
                    else:
                        used.add(source_meta)
                        meta = WorkflowDatumMeta.from_session_datum(source_meta)
                        frame_case_created.add_child(meta.to_stack_datum(datum_id=target_dm.id))
                        frame_case_not_created.add_child(meta.to_stack_datum(datum_id=target_dm.id))
                else:
                    source_case_type = self.get_case_type_created_by_form(form, target_module)
                    try:
                        target_dm = self.get_target_dm(target_form_dm, source_case_type, module)
                    except SuiteError:
                        if not allow_missing:
                            raise
                    else:
                        used.add(source_meta)
                        datum_meta = WorkflowDatumMeta.from_session_datum(target_dm)
                        frame_case_created.add_child(datum_meta.to_stack_datum(source_id=source_meta.id))

            # return any source datums that were not already added to the target
            return [dm for dm in source_form_dm if dm not in used]

        source_form_dm = self.helper.get_form_datums(form)
        if target_module.root_module_id:
            # add stack children for the root module before adding any for the child module.
            root_module = target_module.root_module
            root_module_command = CommandId(id_strings.menu_id(root_module))
            frame_case_created.add_child(root_module_command)
            frame_case_not_created.add_child(root_module_command)

            source_form_dm = add_datums_for_target(root_module, source_form_dm, allow_missing=True)

        frame_case_created.add_child(CommandId(target_command))
        frame_case_not_created.add_child(CommandId(target_command))
        add_datums_for_target(target_module, source_form_dm)

        return stack_frames