From 788e21dc8c755aed64f2c18167169eb1c04f9981 Mon Sep 17 00:00:00 2001 From: helmutm Date: Wed, 1 Feb 2006 15:08:53 +0000 Subject: [PATCH] Rendering of document data (on view/node) depending on contentType git-svn-id: svn://svn.cy55.de/Zope3/src/loops/trunk@1047 fd906abe-77d9-0310-91a1-e0d9ade77398 --- README.txt | 37 +++++++++++++++++++++++++- browser/configure.zcml | 8 ++++++ browser/node.py | 7 +++++ browser/node_macros.pt | 2 +- browser/resource.py | 59 ++++++++++++++++++++++++++++++++++++++++++ interfaces.py | 8 ++++++ 6 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 browser/resource.py diff --git a/README.txt b/README.txt index c869943..c17269c 100755 --- a/README.txt +++ b/README.txt @@ -347,6 +347,41 @@ A node object provides the targetSchema of its target: >>> IMediaAssetView.providedBy(m111) True +A node's target is rendered using the NodeView's renderTargetBody() +method. This makes use of a browser view registered for the target interface, +and of a lot of other stuff needed for the rendering machine. + + >>> from zope.app.publisher.interfaces.browser import IBrowserView + >>> from zope.publisher.interfaces.browser import IBrowserRequest + >>> from loops.browser.resource import DocumentView + >>> ztapi.provideAdapter(IDocument, Interface, DocumentView, + ... with=(IBrowserRequest,)) + + >>> from zope.component.interfaces import IFactory + >>> from zope.app.renderer import plaintext + >>> ztapi.provideUtility(IFactory, plaintext.PlainTextSourceFactory, + ... 'zope.source.plaintext') + >>> ztapi.provideAdapter(plaintext.IPlainTextSource, Interface, + ... plaintext.PlainTextToHTMLRenderer, + ... with=(IBrowserRequest,)) + >>> from zope.app.renderer import rest + >>> ztapi.provideUtility(IFactory, rest.ReStructuredTextSourceFactory, + ... 'zope.source.rest') + >>> ztapi.provideAdapter(rest.IReStructuredTextSource, Interface, + ... rest.ReStructuredTextToHTMLRenderer, + ... with=(IBrowserRequest,)) + + >>> m112.target = doc1 + >>> view = NodeView(m112, TestRequest()) + >>> view.renderTargetBody() + u'' + >>> doc1.data = u'Test data\n\nAnother paragraph' + >>> view.renderTargetBody() + u'Test data
\n
\nAnother paragraph' + >>> doc1.contentType = 'text/restructured' + >>> view.renderTargetBody() + u'

Test data

\n

Another paragraph

\n' + It is possible to edit a target's attributes directly in an edit form provided by the node: @@ -388,7 +423,7 @@ Let's add some more nodes and reorder them: ['m111', 'm112', 'm113', 'm114'] A special management view provides methods for moving objects down, up, -to the bottom, and to the top +to the bottom, and to the top. >>> from cybertools.container.ordered import OrderedContainerView >>> view = OrderedContainerView(m11, TestRequest()) diff --git a/browser/configure.zcml b/browser/configure.zcml index 7e4a2f6..fdac50a 100644 --- a/browser/configure.zcml +++ b/browser/configure.zcml @@ -390,4 +390,12 @@ name="node.html" /> + + diff --git a/browser/node.py b/browser/node.py index 1bfdc96..4d2dbee 100644 --- a/browser/node.py +++ b/browser/node.py @@ -72,6 +72,13 @@ class NodeView(object): return targetView() return u'' + def renderTargetBody(self): + target = self.target + if target is not None: + targetView = zapi.getMultiAdapter((target, self.request)) + return targetView.render() + return u'' + @Lazy def page(self): page = self.context.getPage() diff --git a/browser/node_macros.pt b/browser/node_macros.pt index d8de2c6..586e648 100644 --- a/browser/node_macros.pt +++ b/browser/node_macros.pt @@ -27,7 +27,7 @@ tal:condition="target" tal:attributes="class string:content-$level; ondblclick python: item.editable and onclick or ''" - tal:content="structure target/data"> + tal:content="structure view/renderTargetBody"> The body diff --git a/browser/resource.py b/browser/resource.py new file mode 100644 index 0000000..4d12700 --- /dev/null +++ b/browser/resource.py @@ -0,0 +1,59 @@ +# +# Copyright (c) 2006 Helmut Merz helmutm@cy55.de +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +""" +View class for resource objects. + +$Id$ +""" + +from zope.cachedescriptors.property import Lazy +from zope.app import zapi +from zope.app.dublincore.interfaces import ICMFDublinCore +from zope.proxy import removeAllProxies +from zope.security import canAccess, canWrite +from zope.security.proxy import removeSecurityProxy + +from loops.interfaces import IDocument, IMediaAsset + +renderingFactories = { + 'text/plain': 'zope.source.plaintext', + 'text/stx': 'zope.source.stx', + 'text/structured': 'zope.source.stx', + 'text/rest': 'zope.source.rest', + 'text/restructured': 'zope.source.rest', +} + + +class DocumentView(object): + + def __init__(self, context, request): + self.context = context + self.request = request + + def render(self): + """ Return the rendered content (data) of the context object. + """ + text = self.context.data + typeKey = renderingFactories.get(self.context.contentType, + renderingFactories['text/plain']) + source = zapi.createObject(typeKey, text) + view = zapi.getMultiAdapter((removeAllProxies(source), self.request)) + return view.render() + + diff --git a/interfaces.py b/interfaces.py index 5f99c7c..188900f 100644 --- a/interfaces.py +++ b/interfaces.py @@ -170,6 +170,14 @@ class IDocumentSchema(IResourceSchema): missing_value=u'', required=False) + contentType = schema.Choice( + title=_(u'Content Type'), + description=_(u'Content type (format) of the data field'), + values=('text/restructured', 'text/structured', 'text/html', + 'text/plain',), + default='text/restructured', + required=True) + class IDocumentView(IDocumentSchema): """ Used for accessing a document via a node's target attribute"""