def test_raiseOnError_for_non_managers(self):
        # create a POD template that will fail in every case
        current_path = os.path.dirname(__file__)
        failing_template_data = open(os.path.join(current_path, 'failing_template.odt'), 'r').read()
        failing_template = api.content.create(
            type='ConfigurablePODTemplate',
            id='failing_template',
            title=_(u'Failing template'),
            odt_file=NamedBlobFile(
                data=failing_template_data,
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'modele_general.odt',
            ),
            pod_formats=['odt'],
            container=self.portal.podtemplates,
            exclude_from_nav=True
        )
        # create a user that is not Manager
        api.user.create(
            email='*****@*****.**',
            username='******',
            password='******',
            roles=['Member'],
            properties={})

        # disabled by default
        self.assertFalse(get_raiseOnError_for_non_managers())
        # when disabled, generating by anybody will always produce a document
        view = failing_template.restrictedTraverse('@@document-generation')
        template_UID = failing_template.UID()
        # generated for 'Manager'
        self.assertTrue('Manager' in api.user.get_current().getRoles())
        self.assertTrue(
            'mimetypeapplication/vnd.oasis.opendocument.text' in
            view(template_uid=template_UID, output_format='odt'))
        # generated for non 'Manager'
        login(self.portal, 'user')
        self.assertFalse('Manager' in api.user.get_current().getRoles())
        self.assertTrue(
            'mimetypeapplication/vnd.oasis.opendocument.text' in
            view(template_uid=template_UID, output_format='odt'))

        # enable raiseOnError_for_non_managers and test again
        login(self.portal, TEST_USER_NAME)
        api.portal.set_registry_record(
            'collective.documentgenerator.browser.controlpanel.'
            'IDocumentGeneratorControlPanelSchema.raiseOnError_for_non_managers',
            True)
        self.assertTrue(
            'mimetypeapplication/vnd.oasis.opendocument.text' in
            view(template_uid=template_UID, output_format='odt'))
        login(self.portal, 'user')
        # raises an error instead generating the document
        with self.assertRaises(Exception) as cm:
            view(template_uid=template_UID, output_format='odt')
        self.assertTrue(
            u'Error while evaluating expression "view.unknown_method()".' in
            cm.exception.message)
Exemplo n.º 2
0
    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 = False
        template_optimize_tables = pod_template.get_optimize_tables()
        if (template_optimize_tables == -1 and config.get_optimize_tables()) or \
           template_optimize_tables == 1:
            stylesMapping = {'table': TableProperties(optimalColumnWidths=True)}
            optimalColumnWidths = "OCW_.*"

        # 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,
            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
Exemplo n.º 3
0
    def test_raiseOnError_for_non_managers(self):
        # create a POD template that will fail in every case
        current_path = os.path.dirname(__file__)
        failing_template_data = open(
            os.path.join(current_path, 'failing_template.odt'), 'r').read()
        failing_template = api.content.create(
            type='ConfigurablePODTemplate',
            id='failing_template',
            title=_(u'Failing template'),
            odt_file=NamedBlobFile(
                data=failing_template_data,
                contentType='application/vnd.oasis.opendocument.text',
                filename=u'modele_general.odt',
            ),
            pod_formats=['odt'],
            container=self.portal.podtemplates,
            exclude_from_nav=True)
        # create a user that is not Manager
        api.user.create(email='*****@*****.**',
                        username='******',
                        password='******',
                        roles=['Member'],
                        properties={})

        # disabled by default
        self.assertFalse(get_raiseOnError_for_non_managers())
        # when disabled, generating by anybody will always produce a document
        view = failing_template.restrictedTraverse('@@document-generation')
        template_UID = failing_template.UID()
        # generated for 'Manager'
        self.assertTrue('Manager' in api.user.get_current().getRoles())
        self.assertTrue('mimetypeapplication/vnd.oasis.opendocument.text' in
                        view(template_uid=template_UID, output_format='odt'))
        # generated for non 'Manager'
        login(self.portal, 'user')
        self.assertFalse('Manager' in api.user.get_current().getRoles())
        self.assertTrue('mimetypeapplication/vnd.oasis.opendocument.text' in
                        view(template_uid=template_UID, output_format='odt'))

        # enable raiseOnError_for_non_managers and test again
        login(self.portal, TEST_USER_NAME)
        api.portal.set_registry_record(
            'collective.documentgenerator.browser.controlpanel.'
            'IDocumentGeneratorControlPanelSchema.raiseOnError_for_non_managers',
            True)
        self.assertTrue('mimetypeapplication/vnd.oasis.opendocument.text' in
                        view(template_uid=template_UID, output_format='odt'))
        login(self.portal, 'user')
        # raises an error instead generating the document
        with self.assertRaises(Exception) as cm:
            view(template_uid=template_UID, output_format='odt')
        self.assertTrue(
            'Error while evaluating expression "view.unknown_method()".' in
            str(cm.exception))
    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