def display_code(self, field_name): code = [] if self.is_default_field(field_name): code.append('%s : context.%s' % (_(u'input_field'), field_name)) elif self.is_rich_text_field(field_name): code.append("%s : do text from view.render_xhtml('%s')" % (_(u'comment'), field_name)) elif self.is_line_field(field_name): code.append('%s : line' % _(u'input_field')) code.append("%s : do text for line in view.list('%s')" % (_(u'comment'), field_name)) return code
def display_code(self, field_name): code = [] if self.is_default_field(field_name): code.append('%s : context.%s' % (_(u'input_field'), field_name)) elif self.is_rich_text_field(field_name): code.append("%s : do text from view.render_xhtml('%s')" % (_(u'comment'), field_name)) elif self.is_line_field(field_name): code.append('%s : line' % _(u'input_field')) code.append("%s : do text for line in view.list('%s')" % (_(u'comment'), field_name)) return code
def manage_messages(self): messages = OrderedDict() for left_over in self.left_to_verify: self._add_by_path(left_over, self.error, None, (_("Error"), _("Could not check"))) messages["check_pod_template_error"] = self.error messages["check_pod_template_no_obj_found"] = self.no_obj_found messages[ "check_pod_template_no_pod_portal_types"] = self.no_pod_portal_types messages["check_pod_template_not_enabled"] = self.not_enabled messages["check_pod_template_not_managed"] = self.not_managed messages["check_pod_template_clean"] = self.clean return messages
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)
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 check_pod_template(self, pod_template, obj, output_format): try: view = obj.restrictedTraverse("@@document-generation") view() view._generate_doc(pod_template, output_format=output_format, raiseOnError=True) self._add_by_path(pod_template, self.clean, obj) except Exception as exc: self._add_by_path(pod_template, self.error, obj, (_("Error"), str(exc))) self.left_to_verify.remove(pod_template)
def check_mailing_loop(self, mailed_template, obj, output_format): folder = api.content.create(type="Folder", title=u"Folder", id="temp_folder", container=self.context) def do_nothing(ignored_param): pass mailing_loop_template_uid = mailed_template.mailing_loop_template try: if mailing_loop_template_uid not in self.mailing_loop_templates.keys( ): mailing_loop_template = uuidToObject(mailing_loop_template_uid) self.mailing_loop_templates[ mailing_loop_template_uid] = mailing_loop_template generation_view = folder.restrictedTraverse( "@@persistent-document-generation") generation_view.redirects = do_nothing generation_view(mailed_template.UID(), "odt") persistant_doc = folder.listFolderContents()[0] view = folder.restrictedTraverse( "@@mailing-loop-persistent-document-generation") view.redirects = do_nothing view(document_uid=persistant_doc.UID()) # double check anyway just in case there is an error inside the result file view._generate_doc( self.mailing_loop_templates[mailing_loop_template_uid], output_format=output_format, raiseOnError=True, ) self._add_by_path( self.mailing_loop_templates[mailing_loop_template_uid], self.clean, obj) except Exception as exc: self._add_by_path( self.mailing_loop_templates[mailing_loop_template_uid], self.error, obj, (_("Error"), str(exc)), ) finally: api.content.delete(obj=folder) self.left_to_verify.remove( self.mailing_loop_templates[mailing_loop_template_uid])
def add_failing_template(self): current_path = os.path.dirname(__file__) failing_template_data = open( os.path.join(current_path, "failing_template.odt"), "r").read() self.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, ) self.failing_template.reindexObject()
def _assert_fmt(lst, length, var_name): if length > 0: true_lst = lst["/plone/podtemplates"] self.assertEqual( len(true_lst), length, "{} should have {} element, not {}".format( var_name, length, len(true_lst)), ) for element in true_lst: self.assertGreaterEqual(len(element), 2) self.assertIsInstance(element[0], PODTemplate) self.assertNotIsInstance(element[1], PODTemplate) self.assertEqual(len(element), 3) if element[-1]: self.assertEqual(len(element[-1]), 2) self.assertEqual(element[-1][0], _("Error")) self.assertIsInstance(element[-1][1], str) else: self.assertEqual(len(lst), 0, "{} should be empty".format(var_name))
def summary(self, obj): summary = '%s - %s - %s %s' % \ (obj.Title(), obj.creators[0], _(u'last_change'), obj.modification_date.strftime('%d/%m/%Y %H:%M')) return summary
def install_demo(context): """ """ if context.readDataFile('collectivedocumentgenerator_demo_marker.txt') is None: return portal = api.portal.get() if not hasattr(portal, 'podtemplates'): templates_folder = api.content.create( type='Folder', title=_(u'POD Templates'), id='podtemplates', container=portal, exclude_from_nav=True ) templates_folder.setTitle('POD Templates') templates_folder.reindexObject() pod_folder = getattr(portal, 'podtemplates') constrain_types = IConstrainTypes(pod_folder) constrain_types.setConstrainTypesMode(1) constrain_types.setLocallyAllowedTypes(POD_TEMPLATE_TYPES.keys()) constrain_types.setImmediatelyAddableTypes(POD_TEMPLATE_TYPES.keys()) # Create some test content style_template_id = 'test_style_template' if not hasattr(pod_folder, style_template_id): api.content.create( type='StyleTemplate', id=style_template_id, title='Styles', odt_file=NamedBlobFile( data=context.readDataFile('templates/styles.odt'), contentType='application/vnd.oasis.opendocument.text', filename=u'styles.odt', ), container=pod_folder, exclude_from_nav=True ) style_template = getattr(pod_folder, style_template_id) style_template_id = 'test_style_template_2' if not hasattr(pod_folder, style_template_id): api.content.create( type='StyleTemplate', id=style_template_id, title=_(u'Styles n°2'), odt_file=NamedBlobFile( data=context.readDataFile('templates/styles_2.odt'), contentType='application/vnd.oasis.opendocument.text', filename=u'styles_2.odt', ), container=pod_folder, exclude_from_nav=True ) sub_template_id = 'sub_template' if not hasattr(pod_folder, sub_template_id): api.content.create( type='SubTemplate', id=sub_template_id, title=_(u'Header'), odt_file=NamedBlobFile( data=context.readDataFile('templates/sub_template.odt'), contentType='application/vnd.oasis.opendocument.text', filename=u'sub_template.odt', ), container=pod_folder, exclude_from_nav=True, style_template=[style_template.UID()], ) sub_template = getattr(pod_folder, sub_template_id) if not hasattr(pod_folder, 'loop_template'): api.content.create( type='MailingLoopTemplate', id='loop_template', title=_(u'Mailing loop template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/mailing.odt'), contentType='application/vnd.oasis.opendocument.text', filename=u'mailing.odt', ), container=pod_folder, exclude_from_nav=True, style_template=[style_template.UID()], ) loop_template = getattr(pod_folder, 'loop_template') if not hasattr(pod_folder, 'test_template'): api.content.create( type='PODTemplate', id='test_template', title=_(u'General template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_general.odt'), contentType='application/vnd.oasis.opendocument.text', filename=u'modele_general.odt', ), container=pod_folder, exclude_from_nav=True, ) if not hasattr(pod_folder, 'test_template_multiple'): api.content.create( type='ConfigurablePODTemplate', id='test_template_multiple', title=_(u'Multiple format template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_general.odt'), contentType='application/vnd.oasis.opendocument.text', filename=u'modele_general.odt', ), container=pod_folder, exclude_from_nav=True, pod_formats=['odt', 'pdf', 'doc', 'docx'], pod_portal_types=['Document'], style_template=[style_template.UID()], merge_templates=[ { 'template': sub_template.UID(), 'pod_context_name': 'header', 'do_rendering': True, } ], ) if not hasattr(pod_folder, 'test_template_bis'): api.content.create( type='ConfigurablePODTemplate', id='test_template_bis', title=_(u'Collection template'), odt_file=NamedBlobFile( data=context.readDataFile(safe_unicode('templates/modèle_collection.odt')), contentType='application/vnd.oasis.opendocument.text', filename=u'modèle_collection.odt', ), container=pod_folder, exclude_from_nav=True, pod_formats=['odt', 'pdf', ], pod_portal_types=['Collection', 'Folder'], style_template=[style_template.UID()], merge_templates=[ { 'template': sub_template.UID(), 'pod_context_name': 'header', 'do_rendering': False, } ], context_variables=[ { 'name': 'details', 'value': '1', } ], ) if not hasattr(pod_folder, 'test_ods_template'): api.content.create( type='ConfigurablePODTemplate', id='test_ods_template', title=_(u'Spreadsheet template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_general.ods'), contentType='application/vnd.oasis.opendocument.spreadsheet', filename=u'modele_general.ods', ), container=pod_folder, exclude_from_nav=True, pod_formats=['ods', 'xls', ], pod_portal_types=['Document'], style_template=[style_template.UID()], ) if not hasattr(pod_folder, 'test_template_possibly_mailed'): api.content.create( type='ConfigurablePODTemplate', id='test_template_possibly_mailed', title=_(u'Possibly mailed template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/possibly_mailed_model.odt'), contentType='application/vnd.oasis.opendocument.text', filename=u'possibly_mailed_model.odt', ), container=pod_folder, exclude_from_nav=True, pod_formats=['odt'], pod_portal_types=['Folder'], mailing_loop_template=loop_template.UID(), context_variables=[ { 'name': 'details', 'value': '1', } ], ) if not hasattr(pod_folder, 'test_template_reusable'): api.content.create( type='ConfigurablePODTemplate', id='test_template_reusable', title=_(u'Reusable template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_general.odt'), contentType='application/vnd.oasis.opendocument.text', filename=u'modele_general.odt', ), is_reusable=True, container=pod_folder, pod_formats=['odt'], pod_portal_types=['Folder'], exclude_from_nav=True ) reusable_template = getattr(pod_folder, 'test_template_reusable') if not hasattr(pod_folder, 'test_template_reuse'): api.content.create( type='ConfigurablePODTemplate', id='test_template_reuse', title=_(u'Reuse Test Template'), container=pod_folder, exclude_from_nav=True, pod_formats=['odt', 'pdf', 'doc', 'docx'], pod_portal_types=['Document'], pod_template_to_use=reusable_template.UID() )
def install_demo(context): """ """ portal = api.portal.get() if not hasattr(portal, 'podtemplates'): templates_folder = api.content.create( type='Folder', title=_(u'POD Templates'), id='podtemplates', container=portal, excludeFromNav=True ) templates_folder.setTitle('POD Templates') templates_folder.reindexObject() pod_folder = getattr(portal, 'podtemplates') pod_folder.setConstrainTypesMode(1) pod_folder.setLocallyAllowedTypes(POD_TEMPLATE_TYPES.keys()) pod_folder.setImmediatelyAddableTypes(POD_TEMPLATE_TYPES.keys()) # Create some test content style_template_id = 'test_style_template' if not hasattr(pod_folder, style_template_id): api.content.create( type='StyleTemplate', id=style_template_id, title='Styles', odt_file=NamedBlobFile( data=context.readDataFile('templates/styles.odt'), contentType='applications/odt', filename=u'styles.odt', ), container=pod_folder, excludeFromNav=True ) style_template = getattr(pod_folder, style_template_id) style_template_id = 'test_style_template_2' if not hasattr(pod_folder, style_template_id): api.content.create( type='StyleTemplate', id=style_template_id, title=_(u'Styles n°2'), odt_file=NamedBlobFile( data=context.readDataFile('templates/styles_2.odt'), contentType='applications/odt', filename=u'styles_2.odt', ), container=pod_folder, excludeFromNav=True ) sub_template_id = 'sub_template' if not hasattr(pod_folder, sub_template_id): api.content.create( type='SubTemplate', id=sub_template_id, title=_(u'Header'), odt_file=NamedBlobFile( data=context.readDataFile('templates/sub_template.odt'), contentType='applications/odt', filename=u'sub_template.odt', ), container=pod_folder, excludeFromNav=True ) sub_template = getattr(pod_folder, sub_template_id) if not hasattr(pod_folder, 'test_template'): api.content.create( type='PODTemplate', id='test_template', title=_(u'General template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_general.odt'), contentType='applications/odt', filename=u'modele_general.odt', ), container=pod_folder, excludeFromNav=True ) if not hasattr(pod_folder, 'test_template_multiple'): api.content.create( type='ConfigurablePODTemplate', id='test_template_multiple', title=_(u'Multiple format template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_general.odt'), contentType='applications/odt', filename=u'modele_general.odt', ), container=pod_folder, excludeFromNav=True, pod_formats=['odt', 'pdf', ], pod_portal_types=['Document'], style_template=[style_template.UID()], merge_templates=[ { 'template': sub_template.UID(), 'pod_context_name': 'header', } ], ) if not hasattr(pod_folder, 'test_template_bis'): api.content.create( type='ConfigurablePODTemplate', id='test_template_bis', title=_(u'Collection template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_collection.odt'), contentType='applications/odt', filename=u'modele_collection.odt', ), container=pod_folder, excludeFromNav=True, pod_formats=['odt', 'pdf', ], pod_portal_types=['Collection'], style_template=[style_template.UID()], merge_templates=[ { 'template': sub_template.UID(), 'pod_context_name': 'header', } ], ) if not hasattr(pod_folder, 'test_ods_template'): api.content.create( type='ConfigurablePODTemplate', id='test_ods_template', title=_(u'Spreadsheet template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_general.ods'), contentType='applications/odt', filename=u'modele_collection.odt', ), container=pod_folder, excludeFromNav=True, pod_formats=['ods', 'xls', ], pod_portal_types=['Document'], style_template=[style_template.UID()], )
def summary(self, obj): summary = '%s - %s - %s %s' % \ (obj.Title(), obj.creators[0], _(u'last_change'), obj.modification_date.strftime('%d/%m/%Y %H:%M')) return summary
def install_demo(context): """ """ portal = api.portal.get() if not hasattr(portal, 'podtemplates'): templates_folder = api.content.create( type='Folder', title=_(u'POD Templates'), id='podtemplates', container=portal, excludeFromNav=True ) templates_folder.setTitle('POD Templates') templates_folder.reindexObject() pod_folder = getattr(portal, 'podtemplates') pod_folder.setConstrainTypesMode(1) pod_folder.setLocallyAllowedTypes(POD_TEMPLATE_TYPES.keys()) pod_folder.setImmediatelyAddableTypes(POD_TEMPLATE_TYPES.keys()) # Create some test content style_template_id = 'test_style_template' if not hasattr(pod_folder, style_template_id): api.content.create( type='StyleTemplate', id=style_template_id, title='Styles', odt_file=NamedBlobFile( data=context.readDataFile('templates/styles.odt'), contentType='applications/odt', filename=u'styles.odt', ), container=pod_folder, excludeFromNav=True ) style_template = getattr(pod_folder, style_template_id) style_template_id = 'test_style_template_2' if not hasattr(pod_folder, style_template_id): api.content.create( type='StyleTemplate', id=style_template_id, title=_(u'Styles n°2'), odt_file=NamedBlobFile( data=context.readDataFile('templates/styles_2.odt'), contentType='applications/odt', filename=u'styles_2.odt', ), container=pod_folder, excludeFromNav=True ) sub_template_id = 'sub_template' if not hasattr(pod_folder, sub_template_id): api.content.create( type='SubTemplate', id=sub_template_id, title=_(u'Header'), odt_file=NamedBlobFile( data=context.readDataFile('templates/sub_template.odt'), contentType='applications/odt', filename=u'sub_template.odt', ), container=pod_folder, excludeFromNav=True ) sub_template = getattr(pod_folder, sub_template_id) if not hasattr(pod_folder, 'test_template'): api.content.create( type='PODTemplate', id='test_template', title=_(u'General template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_general.odt'), contentType='applications/odt', filename=u'modele_general.odt', ), container=pod_folder, excludeFromNav=True ) if not hasattr(pod_folder, 'test_template_multiple'): api.content.create( type='ConfigurablePODTemplate', id='test_template_multiple', title=_(u'Multiple format template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_general.odt'), contentType='applications/odt', filename=u'modele_general.odt', ), container=pod_folder, excludeFromNav=True, pod_formats=['odt', 'pdf', ], pod_portal_types=['Document'], style_template=[style_template.UID()], merge_templates=[ { 'template': sub_template.UID(), 'pod_context_name': 'header', } ], ) if not hasattr(pod_folder, 'test_template_bis'): api.content.create( type='ConfigurablePODTemplate', id='test_template_bis', title=_(u'Collection template'), odt_file=NamedBlobFile( data=context.readDataFile('templates/modele_collection.odt'), contentType='applications/odt', filename=u'modele_collection.odt', ), container=pod_folder, excludeFromNav=True, pod_formats=['odt', 'pdf', ], pod_portal_types=['Collection'], style_template=[style_template.UID()], merge_templates=[ { 'template': sub_template.UID(), 'pod_context_name': 'header', } ], )