def __call__(self, context):
        # adapt first term value depending on global configuration value
        # get term from global value vocabulary
        vocabulary = queryUtility(
            IVocabularyFactory, 'collective.documentgenerator.ConfigColumnModifier')
        voc = vocabulary(context)
        global_value = _(voc.getTerm(get_column_modifier()).title)
        global_value_term = _('Global value (${global_value})',
                              mapping={'global_value': global_value})

        voc_terms = [
            SimpleTerm(-1, -1, global_value_term),
            SimpleTerm('disabled', 0, _('Force disabled')),
            SimpleTerm('nothing', 1, _('Force nothing')),
            SimpleTerm('optimize', 2, _('Force optimize')),
            SimpleTerm('distribute', 3, _('Force distribute'))]

        return SimpleVocabulary(voc_terms)
    def __call__(self, context):
        # adapt first term value depending on global configuration value
        # get term from global value vocabulary
        vocabulary = queryUtility(
            IVocabularyFactory, 'collective.documentgenerator.ConfigColumnModifier')
        voc = vocabulary(context)
        global_value = _(voc.getTerm(get_column_modifier()).title)
        global_value_term = _('Global value (${global_value})',
                              mapping={'global_value': global_value})

        voc_terms = [
            SimpleTerm(-1, -1, global_value_term),
            SimpleTerm('disabled', 0, _('Force disabled')),
            SimpleTerm('nothing', 1, _('Force nothing')),
            SimpleTerm('optimize', 2, _('Force optimize')),
            SimpleTerm('distribute', 3, _('Force distribute'))]

        return SimpleVocabulary(voc_terms)
    def test_table_column_modifier(self):
        """ """
        pod_template = self.portal.podtemplates.get('test_template_multiple')
        # do not limit portal_type the POD template is generatable on
        pod_template.pod_portal_types = []
        template_uid = pod_template.UID()
        doc = self.portal.get('front-page')
        # add a table to check if it is optimized or not
        text = u'<table><tr><td>Text1</td><td>Text2</td></tr><tr><td>Text3</td><td>Text4</td></tr></table>'
        textNone = u'<table style="table-layout:none"><tr><td>Text1</td><td>Text2</td></tr><tr><td>Text3</td><td>Text4</td></tr></table>'
        textAuto = u'<table style="table-layout:auto"><tr><td>Text1</td><td>Text2</td></tr><tr><td>Text3</td><td>Text4</td></tr></table>'
        textFixed = u'<table style="table-layout:fixed"><tr><td>Text1</td><td>Text2</td></tr><tr><td>Text3</td><td>Text4</td></tr></table>'

        generation_view = doc.restrictedTraverse('@@document-generation')

        def set_text(text):
            if base_hasattr(doc, 'setText'):
                # Archetypes
                doc.setText(text)
            else:
                # Dexterity
                doc.text = RichTextValue(text)

        def assert_result(ocw_in_xml, dc_in_xml):
            generated_doc = generation_view(template_uid, 'odt')
            content_xml = self.get_odt_content_xml(generated_doc)
            self.assertEquals(ocw_in_xml, 'OCW' in content_xml)
            self.assertEquals(dc_in_xml, 'DC' in content_xml)

        # By default : column_modifier disabled globally, CSS override enabled globally and pod_template using global parameter
        self.assertEquals(get_column_modifier(), 'nothing')
        self.assertEquals(pod_template.get_column_modifier(), -1)

        set_text(text)
        assert_result(False, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)

        # disable optimize locally, still disabled globally
        pod_template.column_modifier = 'disabled'
        set_text(text)
        assert_result(False, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(False, False)
        set_text(textFixed)
        assert_result(False, False)

        # enable optimize locally, still disabled globally
        pod_template.column_modifier = 'optimize'
        set_text(text)
        assert_result(True, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)

        # enable distribute locally, still disabled globally
        pod_template.column_modifier = 'distribute'
        set_text(text)
        assert_result(False, True)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)

        # reset local parameter
        pod_template.column_modifier = -1

        # CSS override enabled
        set_column_modifier('nothing')
        set_text(text)
        assert_result(False, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)

        # disable all column modifier globally
        set_column_modifier('disabled')
        set_text(text)
        assert_result(False, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(False, False)
        set_text(textFixed)
        assert_result(False, False)

        # optimize column_modifier enabled globally and pod_template using global parameter
        set_column_modifier('optimize')
        set_text(text)
        assert_result(True, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)

        # distribute column_modifier enabled globally and pod_template using global parameter
        set_column_modifier('distribute')
        set_text(text)
        assert_result(False, True)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)
Exemplo n.º 4
0
    def test_table_column_modifier(self):
        """ """
        pod_template = self.portal.podtemplates.get('test_template_multiple')
        # do not limit portal_type the POD template is generatable on
        pod_template.pod_portal_types = []
        template_uid = pod_template.UID()
        doc = self.portal.get('front-page')
        # add a table to check if it is optimized or not
        text = u'<table><tr><td>Text1</td><td>Text2</td></tr><tr><td>Text3</td><td>Text4</td></tr></table>'
        textNone = u'<table style="table-layout:none"><tr><td>Text1</td><td>Text2</td></tr><tr><td>Text3</td><td>Text4</td></tr></table>'
        textAuto = u'<table style="table-layout:auto"><tr><td>Text1</td><td>Text2</td></tr><tr><td>Text3</td><td>Text4</td></tr></table>'
        textFixed = u'<table style="table-layout:fixed"><tr><td>Text1</td><td>Text2</td></tr><tr><td>Text3</td><td>Text4</td></tr></table>'

        generation_view = doc.restrictedTraverse('@@document-generation')

        def set_text(text):
            if base_hasattr(doc, 'setText'):
                # Archetypes
                doc.setText(text)
            else:
                # Dexterity
                doc.text = RichTextValue(text)

        def assert_result(ocw_in_xml, dc_in_xml):
            generated_doc = generation_view(template_uid, 'odt')
            content_xml = self.get_odt_content_xml(generated_doc)
            self.assertEquals(ocw_in_xml, 'OCW' in content_xml)
            self.assertEquals(dc_in_xml, 'DC' in content_xml)

        # By default : column_modifier disabled globally, CSS override enabled globally and pod_template using global parameter
        self.assertEquals(get_column_modifier(), 'nothing')
        self.assertEquals(pod_template.get_column_modifier(), -1)

        set_text(text)
        assert_result(False, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)

        # disable optimize locally, still disabled globally
        pod_template.column_modifier = 'disabled'
        set_text(text)
        assert_result(False, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(False, False)
        set_text(textFixed)
        assert_result(False, False)

        # enable optimize locally, still disabled globally
        pod_template.column_modifier = 'optimize'
        set_text(text)
        assert_result(True, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)

        # enable distribute locally, still disabled globally
        pod_template.column_modifier = 'distribute'
        set_text(text)
        assert_result(False, True)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)

        # reset local parameter
        pod_template.column_modifier = -1

        # CSS override enabled
        set_column_modifier('nothing')
        set_text(text)
        assert_result(False, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)

        # disable all column modifier globally
        set_column_modifier('disabled')
        set_text(text)
        assert_result(False, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(False, False)
        set_text(textFixed)
        assert_result(False, False)

        # optimize column_modifier enabled globally and pod_template using global parameter
        set_column_modifier('optimize')
        set_text(text)
        assert_result(True, False)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)

        # distribute column_modifier enabled globally and pod_template using global parameter
        set_column_modifier('distribute')
        set_text(text)
        assert_result(False, True)
        set_text(textNone)
        assert_result(False, False)
        set_text(textAuto)
        assert_result(True, False)
        set_text(textFixed)
        assert_result(False, True)
    def _render_document(self,
                         pod_template,
                         output_format,
                         sub_documents,
                         raiseOnError=False,
                         **kwargs):
        """
        Render a single document of type 'output_format' using the odt file
        'document_template' as the generation template.
        Subdocuments is a dictionnary of previously generated subtemplate
        that will be merged into the current generated document.
        """
        document_template = pod_template.get_file()
        tmp_dir = os.getenv('CUSTOM_TMP', None)
        temp_filename = tempfile.mktemp(
            suffix='.{extension}'.format(extension=output_format), dir=tmp_dir)

        # Prepare rendering context
        helper_view = self.get_generation_context_helper()
        generation_context = self._get_generation_context(
            helper_view, pod_template=pod_template)

        # enrich the generation context with previously generated documents
        utils.update_dict_with_validation(
            generation_context, sub_documents,
            _("Error when merging merge_templates in generation context"))

        # enable optimalColumnWidths if enabled in the config and/or on ConfigurablePodTemplate
        stylesMapping = {}
        optimalColumnWidths = "OCW_.*"
        distributeColumns = "DC_.*"

        column_modifier = pod_template.get_column_modifier()
        if column_modifier == -1:
            column_modifier = config.get_column_modifier()

        if column_modifier == 'disabled':
            optimalColumnWidths = False
            distributeColumns = False
        else:
            stylesMapping = {
                'table':
                TableProperties(columnModifier=column_modifier != 'nothing'
                                and column_modifier or None)
            }

        # if raiseOnError is not enabled, enabled it in the config excepted if user is a Manager
        # and currently generated document use odt format
        if not raiseOnError:
            if config.get_raiseOnError_for_non_managers():
                raiseOnError = True
                if 'Manager' in api.user.get_roles(
                ) and output_format == 'odt':
                    raiseOnError = False

        # stylesMapping.update({'para[class=None, parent != cell]': 'texte_delibe',
        #                       'para[class=xSmallText, parent != cell]': 'bodyXSmall',
        #                       'para[class=smallText, parent != cell]': 'bodySmall',
        #                       'para[class=largeText, parent != cell]': 'bodyLarge',
        #                       'para[class=xLargeText, parent != cell]': 'bodyXLarge',
        #                       'para[class=indentation, parent != cell]': 'bodyIndentation',
        #                       'para[class=None, parent = cell]': 'cell_delibe',
        #                       'table': TableProperties(cellContentStyle='cell_delibe'),
        #                       'para[class=xSmallText, parent = cell]': 'cellXSmall',
        #                       'para[class=smallText, parent = cell]': 'cellSmall',
        #                       'para[class=largeText, parent = cell]': 'cellLarge',
        #                       'para[class=xLargeText, parent = cell]': 'cellXLarge',
        #                       'para[class=indentation, parent = cell]': 'cellIndentation',
        #                       })
        # stylesMapping.update({'para[class=None,parent!=cell]': 'texte_delibe',
        #                       'para[class=xSmallText,parent!=cell]': 'bodyXSmall',
        #                       'para[class=smallText,parent!=cell]': 'bodySmall',
        #                       'para[class=largeText,parent!=cell]': 'bodyLarge',
        #                       'para[class=xLargeText,parent!=cell]': 'bodyXLarge',
        #                       'para[class=indentation,parent!=cell]': 'bodyIndentation',
        #                       'para[class=xSmallText,parent=cell]': 'cellXSmall',
        #                       'para[class=smallText,parent=cell]': 'cellSmall',
        #                       'para[class=largeText,parent=cell]': 'cellLarge',
        #                       'para[class=xLargeText,parent=cell]': 'cellXLarge',
        #                       'para[class=indentation,parent=cell]': 'cellIndentation',
        #                       })

        renderer = Renderer(StringIO(document_template.data),
                            generation_context,
                            temp_filename,
                            pythonWithUnoPath=config.get_uno_path(),
                            ooServer=config.get_oo_server(),
                            ooPort=config.get_oo_port(),
                            raiseOnError=raiseOnError,
                            imageResolver=api.portal.get(),
                            forceOoCall=True,
                            optimalColumnWidths=optimalColumnWidths,
                            distributeColumns=distributeColumns,
                            stylesMapping=stylesMapping,
                            stream=config.get_use_stream(),
                            **kwargs)

        # it is only now that we can initialize helper view's appy pod renderer
        all_helper_views = self.get_views_for_appy_renderer(
            generation_context, helper_view)
        for view in all_helper_views:
            view._set_appy_renderer(renderer)

        renderer.run()

        # return also generation_context to test ist content in tests
        return temp_filename, generation_context
    def _render_document(self, pod_template, output_format, sub_documents, raiseOnError=False, **kwargs):
        """
        Render a single document of type 'output_format' using the odt file
        'document_template' as the generation template.
        Subdocuments is a dictionnary of previously generated subtemplate
        that will be merged into the current generated document.
        """
        document_template = pod_template.get_file()
        temp_filename = tempfile.mktemp('.{extension}'.format(extension=output_format))

        # Prepare rendering context
        helper_view = self.get_generation_context_helper()
        generation_context = self._get_generation_context(helper_view, pod_template=pod_template)

        # enrich the generation context with previously generated documents
        utils.update_dict_with_validation(generation_context, sub_documents,
                                          _("Error when merging merge_templates in generation context"))

        # enable optimalColumnWidths if enabled in the config and/or on ConfigurablePodTemplate
        stylesMapping = {}
        optimalColumnWidths = "OCW_.*"
        distributeColumns = "DC_.*"

        column_modifier = pod_template.get_column_modifier()
        if column_modifier == -1:
            column_modifier = config.get_column_modifier()

        if column_modifier == 'disabled':
            optimalColumnWidths = False
            distributeColumns = False
        else:
            stylesMapping = {
                'table': TableProperties(columnModifier=column_modifier != 'nothing' and column_modifier or None)}

        # if raiseOnError is not enabled, enabled it in the config excepted if user is a Manager
        # and currently generated document use odt format
        if not raiseOnError:
            if config.get_raiseOnError_for_non_managers():
                raiseOnError = True
                if 'Manager' in api.user.get_roles() and output_format == 'odt':
                    raiseOnError = False

        renderer = Renderer(
            StringIO(document_template.data),
            generation_context,
            temp_filename,
            pythonWithUnoPath=config.get_uno_path(),
            ooPort=config.get_oo_port(),
            raiseOnError=raiseOnError,
            imageResolver=api.portal.get(),
            forceOoCall=True,
            optimalColumnWidths=optimalColumnWidths,
            distributeColumns=distributeColumns,
            stylesMapping=stylesMapping,
            **kwargs
        )

        # it is only now that we can initialize helper view's appy pod renderer
        all_helper_views = self.get_views_for_appy_renderer(generation_context, helper_view)
        for view in all_helper_views:
            view._set_appy_renderer(renderer)

        renderer.run()

        # return also generation_context to test ist content in tests
        return temp_filename, generation_context