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
This commit is contained in:
helmutm 2006-02-01 15:08:53 +00:00
parent 7103463483
commit 788e21dc8c
6 changed files with 119 additions and 2 deletions

View file

@ -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<br />\n<br />\nAnother paragraph'
>>> doc1.contentType = 'text/restructured'
>>> view.renderTargetBody()
u'<p>Test data</p>\n<p>Another paragraph</p>\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())

View file

@ -390,4 +390,12 @@
name="node.html"
/>
<zope:adapter
for="loops.interfaces.IDocument
zope.publisher.interfaces.browser.IBrowserRequest"
provides="zope.interface.Interface"
factory="loops.browser.resource.DocumentView"
permission="zope.View"
/>
</configure>

View file

@ -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()

View file

@ -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
</div>
</tal:body>

59
browser/resource.py Normal file
View file

@ -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()

View file

@ -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"""