def test_archive(self): """Test you can move the source in a folder. The rendering in the document will be broken after, and you will have a message, only in preview. """ factory = self.root.manage_addProduct['OFSP'] factory.manage_addFolder('folder', 'Folder') self.assertFalse(IFolder.providedBy(self.root.folder)) self.assertNotIn('codesource', self.root.folder.objectIds()) self.assertIn('codesource', self.root.objectIds()) token = self.root.manage_cutObjects(['codesource']) self.root.folder.manage_pasteObjects(token) self.assertIn('codesource', self.root.folder.objectIds()) self.assertNotIn('codesource', self.root.objectIds()) controller = self.get_controller() self.assertTrue(verifyObject(IExternalSourceController, controller)) with self.assertRaises(SourceMissingError) as error: controller.render() message = silvaviews.render(error.exception, TestRequest()) self.assertEqual(message.strip(), '') message = silvaviews.render( error.exception, TestRequest(), preview=True) self.assertEqual( message.strip(), '<p>External Source unknow is not available.</p>') # The source is no longer available self.assertItemsEqual( filter(lambda (name, candidate): name == 'codesource', availableSources(self.root)), [])
def test_archive(self): """Test you can move the source in a folder. The rendering in the document will be broken after, and you will have a message, only in preview. """ factory = self.root.manage_addProduct['OFSP'] factory.manage_addFolder('folder', 'Folder') self.assertFalse(IFolder.providedBy(self.root.folder)) self.assertNotIn('codesource', self.root.folder.objectIds()) self.assertIn('codesource', self.root.objectIds()) token = self.root.manage_cutObjects(['codesource']) self.root.folder.manage_pasteObjects(token) self.assertIn('codesource', self.root.folder.objectIds()) self.assertNotIn('codesource', self.root.objectIds()) controller = self.get_controller() self.assertTrue(verifyObject(IExternalSourceController, controller)) with self.assertRaises(SourceMissingError) as error: controller.render() message = silvaviews.render(error.exception, TestRequest()) self.assertEqual(message.strip(), '') message = silvaviews.render(error.exception, TestRequest(), preview=True) self.assertEqual(message.strip(), '<p>External Source unknow is not available.</p>') # The source is no longer available self.assertItemsEqual([ name_candidate3 for name_candidate3 in availableSources(self.root) if name_candidate3[0] == 'codesource' ], [])
def __call__(self, tree): for node in tree.xpath(SOURCE_XPATH): instance = node.attrib.get('data-source-instance') if instance is not None: del node.attrib['data-source-instance'] try: if instance is None: raise ParametersMissingError() source = self.sources(self.request, instance=instance) html = source.render() keep = not set( node.attrib['class'].split()).issubset(DEFAULT_CLASSES) except SourceError as error: html = silvaviews.render(error, self.request).strip() if not html: continue # There is already a class, as it is used to match # in the XPath. node.attrib['class'] += ' broken-source' keep = True result = lxml.html.fragment_fromstring(html, create_parent="div") if keep: node.extend(result) else: parent = node.getparent() if parent is not None: index = parent.index(node) for child in result: parent.insert(index, child) index += 1 parent.remove(node)
def __call__(self, tree): for node in tree.xpath(SOURCE_XPATH): instance = node.attrib.get("data-source-instance") if instance is not None: del node.attrib["data-source-instance"] try: if instance is None: raise ParametersMissingError() source = self.sources(self.request, instance=instance) html = source.render() keep = not set(node.attrib["class"].split()).issubset(DEFAULT_CLASSES) except SourceError, error: html = silvaviews.render(error, self.request).strip() if not html: continue # There is already a class, as it is used to match # in the XPath. node.attrib["class"] += " broken-source" keep = True result = lxml.html.fragment_fromstring(html, create_parent="div") if keep: node.extend(result) else: parent = node.getparent() if parent is not None: index = parent.index(node) for child in result: parent.insert(index, child) index += 1 parent.remove(node)
def POST(self): try: source = self.get_source() html = source.render(preview=True).strip() if not html: # XXX Should support translation here (i.e. use a template) return """<div><h3>%s</h3> <p>The source didn't produce any content.</p></div>""" % source.label return html except SourceError as error: return silvaviews.render(error, self.request, preview=True)
def test_previewable(self): """Test that the preview prevent a source to be rendered if preview is asked. """ source = self.root.codesource controller = self.get_controller() self.assertTrue(verifyObject(IExternalSourceController, controller)) self.assertEqual(source.is_previewable(), True) self.assertEqual(controller.render(preview=True), "Render source") source.set_previewable(False) controller = self.get_controller() self.assertTrue(verifyObject(IExternalSourceController, controller)) self.assertEqual(source.is_previewable(), False) with self.assertRaises(SourcePreviewError) as error: self.assertEqual(controller.render(preview=True)) preview = silvaviews.render(error.exception, TestRequest()) self.assertIsInstance(preview, str) self.assertIn('Test Source', preview) self.assertNotEqual(preview, "Render source") # You can still render the not preview self.assertEqual(controller.render(), "Render source")
def test_previewable(self): """Test that the preview prevent a source to be rendered if preview is asked. """ source = self.root.codesource controller = self.get_controller() self.assertTrue(verifyObject(IExternalSourceController, controller)) self.assertEqual(source.is_previewable(), True) self.assertEqual(controller.render(preview=True), "Render source") source.set_previewable(False) controller = self.get_controller() self.assertTrue(verifyObject(IExternalSourceController, controller)) self.assertEqual(source.is_previewable(), False) with self.assertRaises(SourcePreviewError) as error: self.assertEqual(controller.render(preview=True)) preview = silvaviews.render(error.exception, TestRequest()) self.assertIsInstance(preview, basestring) self.assertIn('Test Source', preview) self.assertNotEqual(preview, "Render source") # You can still render the not preview self.assertEqual(controller.render(), "Render source")
def render(self): error = None try: # Try to fetch the controller. controller = self.content.get_controller(self.request) except SourceError as error: error = error else: try: # Try to render the source. return controller.render() except SourceError as error: error = error # Render the error if it returns something. message = silvaviews.render(error, self.request).strip() if message: return message # Return the default text used by the layout otherwise (this # happens with the source is published, you don't have any # details). text = _('Sorry, this ${meta_type} is not viewable.', mapping={'meta_type': self.context.meta_type}) return '<p>%s</p>' % translate(text, context=self.request)
def update(self): self.info = silvaviews.render(self.context, self.request)
return self.form.controller.fields return silvaforms.Fields() class SourceAssetView(silvaviews.View): grok.context(ISourceAsset) def render(self): error = None try: # Try to fetch the controller. controller = self.content.get_controller(self.request) except SourceError, error: error = error else: try: # Try to render the source. return controller.render() except SourceError, error: error = error # Render the error if it returns something. message = silvaviews.render(error, self.request).strip() if message: return message # Return the default text used by the layout otherwise (this # happens with the source is published, you don't have any # details). text = _('Sorry, this ${meta_type} is not viewable.', mapping={'meta_type': self.context.meta_type}) return u'<p>%s</p>' % translate(text, context=self.request)
def to_html(self, content, request, **parameters): return silvaviews.render(self, request)
def render(self): return silvaviews.render(self.context, self.request)
def get_activity(self): activities = self.context.objectValues('Silva Software Activity') if len(activities): return silvaviews.render(activities[0], self.request) return None