[Checkins] SVN: five.grok/branches/sylvain-viewlets/src/five/grok/ Implements viewlets.
Sylvain Viollon
sylvain at infrae.com
Fri Oct 24 21:29:52 EDT 2008
Log message for revision 92538:
Implements viewlets.
Changed:
U five.grok/branches/sylvain-viewlets/src/five/grok/__init__.py
U five.grok/branches/sylvain-viewlets/src/five/grok/components.py
U five.grok/branches/sylvain-viewlets/src/five/grok/configure.zcml
D five.grok/branches/sylvain-viewlets/src/five/grok/directives.py
U five.grok/branches/sylvain-viewlets/src/five/grok/ftests/test_grok_functional.py
A five.grok/branches/sylvain-viewlets/src/five/grok/ftests/viewlet/
A five.grok/branches/sylvain-viewlets/src/five/grok/ftests/viewlet/__init__.py
A five.grok/branches/sylvain-viewlets/src/five/grok/ftests/viewlet/manager_render.py
U five.grok/branches/sylvain-viewlets/src/five/grok/meta.py
U five.grok/branches/sylvain-viewlets/src/five/grok/meta.zcml
U five.grok/branches/sylvain-viewlets/src/five/grok/templatereg.py
-=-
Modified: five.grok/branches/sylvain-viewlets/src/five/grok/__init__.py
===================================================================
--- five.grok/branches/sylvain-viewlets/src/five/grok/__init__.py 2008-10-25 01:26:56 UTC (rev 92537)
+++ five.grok/branches/sylvain-viewlets/src/five/grok/__init__.py 2008-10-25 01:29:52 UTC (rev 92538)
@@ -1,14 +1,27 @@
+#############################################################################
+#
+# Copyright (c) 2008 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
from grokcore.component import *
from grokcore.security import *
from grokcore.view import *
+from grokcore.viewlet import *
from grokcore.formlib import *
from five.grok.components import View, Model, Form, AddForm
from five.grok.components import EditForm, DisplayForm
from five.grok.components import ViewletManager, Viewlet
-from five.grok.directives import view, viewletmanager
-
# Override the one from grokcore.view so that we get Zope 2 semantics
from five.grok.components import ZopeTwoPageTemplate as PageTemplate
Modified: five.grok/branches/sylvain-viewlets/src/five/grok/components.py
===================================================================
--- five.grok/branches/sylvain-viewlets/src/five/grok/components.py 2008-10-25 01:26:56 UTC (rev 92537)
+++ five.grok/branches/sylvain-viewlets/src/five/grok/components.py 2008-10-25 01:29:52 UTC (rev 92538)
@@ -1,3 +1,17 @@
+#############################################################################
+#
+# Copyright (c) 2008 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
import martian
from zope.annotation.interfaces import IAttributeAnnotatable
@@ -10,6 +24,8 @@
from grokcore.formlib.components import GrokForm as BaseGrokForm
from grokcore.formlib.components import default_display_template, default_form_template
from grokcore.view.components import PageTemplate
+from grokcore.viewlet.components import Viewlet as BaseViewlet
+from grokcore.viewlet.components import ViewletManager as BaseViewletManager
import grokcore.view
import grokcore.security
@@ -18,8 +34,8 @@
from Products.Five.browser.pagetemplatefile import getEngine
from Products.Five.browser import resource
from Products.Five.formlib import formbase
-from Products.Five.viewlet.manager import ViewletManagerBase
-from Products.Five.viewlet.viewlet import ViewletBase
+from Products.Five.viewlet.manager import ViewletManagerBase as \
+ ZopeTwoBaseViewletManager
from Products.PageTemplates.Expressions import SecureModuleImporter
from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
from OFS.SimpleItem import SimpleItem
@@ -176,115 +192,40 @@
template = default_display_template
-# Content provider/viewlets
+# Viewlet / Viewlet Manager
-class ContentProviderBase(Acquisition.Explicit):
+class ViewletManager(BaseViewletManager, ZopeTwoBaseViewletManager):
+
martian.baseclass()
def __init__(self, context, request, view):
- self.context = context
- self.request = request
- self.view = view
- static = component.queryAdapter(
- self.request, interface.Interface,
- name = self.module_info.package_dotted_name)
- if not (static is None):
- self.static = static.__of__(self)
- else:
- self.static = static
- self.__parent__ = view
- self.__name__ = self.__view_name__
+ super(ViewletManager, self).__init__(context, request, view)
+ if not (self.static is None):
+ # XXX See View
+ self.static = self.static.__of__(self)
+ # XXX See View
getPhysicalPath = Acquisition.Acquired
- def namespace(self):
- return {}
-
- def default_namespace(self):
- namespace = {}
- namespace['view'] = self.view
- namespace['static'] = self.static
- return namespace
-
-
-class ViewletManager(ContentProviderBase, ViewletManagerBase):
-
- interface.implements(IViewletManager)
-
- martian.baseclass()
-
- def __init__(self, context, request, view):
- ContentProviderBase.__init__(self, context, request, view)
- ViewletManagerBase.__init__(self, context, request, view)
-
- def default_namespace(self):
- namespace = super(ViewletManager, self).default_namespace()
- namespace['viewletmanager'] = self
- return namespace
-
- def sort(self, viewlets):
- s_viewlets = []
- for name, viewlet in viewlets:
- viewlet.__viewlet_name__ = name
- s_viewlets.append(viewlet)
-
- def sort_key(viewlet):
- # If components have a grok.order directive, sort by that.
- #explicit_order, implicit_order = silvaconf.order.bind().get(viewlet)
- return (viewlet.__module__,
- viewlet.__class__.__name__)
- s_viewlets = sorted(s_viewlets, key=sort_key)
- return [(viewlet.__viewlet_name__, viewlet) for viewlet in s_viewlets]
-
def filter(self, viewlets):
- # Wrap viewlet in aquisition, and only return viewlets
- # accessible to the user.
- parent = self.context.aq_parent
- security_manager = getSecurityManager()
+ # XXX Need Zope 2 filter
+ return ZopeTwoBaseViewletManager.filter(self, viewlets)
- def checkPermission(viewlet):
- _, viewlet = viewlet
- # Unfortuanetly, we don't have easy way to check the permission.
- permission = grokcore.security.require.bind().get(viewlet)
- if (permission is None) or (permission == 'zope.Public'):
- return True
- if isinstance(permission, str):
- permission = component.getUtility(IPermission, permission)
- return security_manager.checkPermission(permission.title, viewlet)
+ def __getitem__(self, key):
+ # XXX Need Zope 2 __getitem__
+ return ZopeTwoBaseViewletManager.__getitem__(self, key)
- return filter(checkPermission,
- [(name, viewlet.__of__(parent)) for name, viewlet in viewlets])
- def render(self):
- """See zope.contentprovider.interfaces.IContentProvider
- """
- # Now render the view
- if getattr(self, 'template', None):
- return self.template.render(self)
- return u'\n'.join([viewlet.render() for viewlet in self.viewlets])
+class Viewlet(BaseViewlet, Acquisition.Explicit):
-
-
-class Viewlet(ContentProviderBase, ViewletBase):
-
- interface.implements(IViewlet)
-
martian.baseclass()
- def __init__(self, context, request, view, viewletmanager):
- ContentProviderBase.__init__(self, context, request, view)
- self.viewletmanager = viewletmanager
+ def __init__(self, context, request, view, manager):
+ super(ViewletManager, self).__init__(context, request, view, manager)
+ if not (self.static is None):
+ # XXX See View
+ self.static = self.static.__of__(self)
- def default_namespace(self):
- namespace = super(ContentProvider, self).default_namespace()
- namespace['viewlet'] = self
- namespace['viewletmanager'] = self.viewletmanager
- return namespace
-
- def update(self):
- pass
-
- def render(self):
- return self.template.render(self)
-
+ # XXX See View
+ getPhysicalPath = Acquisition.Acquired
Modified: five.grok/branches/sylvain-viewlets/src/five/grok/configure.zcml
===================================================================
--- five.grok/branches/sylvain-viewlets/src/five/grok/configure.zcml 2008-10-25 01:26:56 UTC (rev 92537)
+++ five.grok/branches/sylvain-viewlets/src/five/grok/configure.zcml 2008-10-25 01:29:52 UTC (rev 92538)
@@ -5,6 +5,8 @@
i18n_domain="five.grok">
<include package="grokcore.view" />
+ <include package="grokcore.viewlet" />
+ <include package="grokcore.formlib" />
<browser:defaultView
for="grokcore.component.interfaces.IContext"
@@ -16,7 +18,7 @@
title="View"
zcml:condition="not-installed kss.core"
/>
-
+
<includeOverrides file="overrides.zcml" />
</configure>
Deleted: five.grok/branches/sylvain-viewlets/src/five/grok/directives.py
===================================================================
--- five.grok/branches/sylvain-viewlets/src/five/grok/directives.py 2008-10-25 01:26:56 UTC (rev 92537)
+++ five.grok/branches/sylvain-viewlets/src/five/grok/directives.py 2008-10-25 01:29:52 UTC (rev 92538)
@@ -1,15 +0,0 @@
-
-from zope import interface
-import martian
-
-class view(martian.Directive):
- scope = martian.CLASS_OR_MODULE
- store = martian.ONCE
- default = interface.Interface
- validate = martian.validateInterfaceOrClass
-
-class viewletmanager(martian.Directive):
- scope = martian.CLASS_OR_MODULE
- store = martian.ONCE
- default = interface.Interface
- validate = martian.validateInterfaceOrClass
Modified: five.grok/branches/sylvain-viewlets/src/five/grok/ftests/test_grok_functional.py
===================================================================
--- five.grok/branches/sylvain-viewlets/src/five/grok/ftests/test_grok_functional.py 2008-10-25 01:26:56 UTC (rev 92537)
+++ five.grok/branches/sylvain-viewlets/src/five/grok/ftests/test_grok_functional.py 2008-10-25 01:29:52 UTC (rev 92538)
@@ -58,7 +58,7 @@
def test_suite():
suite = unittest.TestSuite()
- for name in ['view', 'form']:
+ for name in ['view', 'viewlet','form']:
suite.addTest(suiteFromPackage(name))
return suite
Added: five.grok/branches/sylvain-viewlets/src/five/grok/ftests/viewlet/__init__.py
===================================================================
--- five.grok/branches/sylvain-viewlets/src/five/grok/ftests/viewlet/__init__.py (rev 0)
+++ five.grok/branches/sylvain-viewlets/src/five/grok/ftests/viewlet/__init__.py 2008-10-25 01:29:52 UTC (rev 92538)
@@ -0,0 +1 @@
+# this is a package.
Added: five.grok/branches/sylvain-viewlets/src/five/grok/ftests/viewlet/manager_render.py
===================================================================
--- five.grok/branches/sylvain-viewlets/src/five/grok/ftests/viewlet/manager_render.py (rev 0)
+++ five.grok/branches/sylvain-viewlets/src/five/grok/ftests/viewlet/manager_render.py 2008-10-25 01:29:52 UTC (rev 92538)
@@ -0,0 +1,38 @@
+"""
+ >>> from five.grok.ftests.viewlet.manager_render import *
+ >>> id = getRootFolder()._setObject("manfred", Mammoth(id='manfred'))
+
+ >>> from Products.Five.testbrowser import Browser
+ >>> browser = Browser()
+ >>> browser.handleErrors = False
+ >>> browser.open("http://localhost/manfred/@@painting")
+ >>> print browser.contents
+ <html>
+ <body>
+ <p>Art is beautiful</p>
+ </body>
+ </html>
+
+"""
+from five import grok
+
+class Mammoth(grok.Model):
+ pass
+
+class Painting(grok.View):
+ pass
+
+class Art(grok.ViewletManager):
+
+ grok.view(Painting)
+
+ def render(self):
+ return '<p>Art is beautiful</p>'
+
+painting = grok.PageTemplate("""\
+<html>
+<body>
+<tal:replace tal:replace="structure provider:art" />
+</body>
+</html>
+""")
Modified: five.grok/branches/sylvain-viewlets/src/five/grok/meta.py
===================================================================
--- five.grok/branches/sylvain-viewlets/src/five/grok/meta.py 2008-10-25 01:26:56 UTC (rev 92537)
+++ five.grok/branches/sylvain-viewlets/src/five/grok/meta.py 2008-10-25 01:29:52 UTC (rev 92538)
@@ -1,3 +1,16 @@
+#############################################################################
+#
+# Copyright (c) 2008 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
import martian
import five.grok
@@ -13,7 +26,7 @@
from grokcore.view.meta.views import default_view_name
-from Products.Five.security import protectClass
+from Products.Five.security import protectClass, protectName
from Globals import InitializeClass as initializeClass
import os.path
@@ -82,81 +95,31 @@
return False
-class ViewletManagerGrokker(martian.ClassGrokker):
+class ViewletSecurityGrokker(martian.ClassGrokker):
+ martian.component(five.grok.Viewlet)
+ martian.directive(grokcore.security.require, name='permission')
- martian.component(components.ViewletManager)
- martian.directive(grokcore.component.name, get_default=default_view_name)
- martian.directive(grokcore.component.context)
- martian.directive(grokcore.view.layer)
- martian.directive(five.grok.view)
+ def execute(self, factory, config, permission, **kw):
+ if permission is None:
+ permission = 'zope.Public'
- def grok(self, name, provider, module_info, **kw):
- # Store module_info on the object.
- provider.__view_name__ = name
- provider.module_info = module_info
- return super(ViewletManagerGrokker, self).grok(
- name, provider, module_info, **kw)
-
- def execute(self, provider, name, context, view, layer, config, **kw):
- """Register a content provider.
- """
- templates = provider.module_info.getAnnotation('grok.templates', None)
- if templates is not None:
- config.action(
- discriminator=None,
- callable=self.checkTemplates,
- args=(templates, provider.module_info, provider)
- )
-
- for_ = (context, layer, view,)
+ attributes = ['update', 'render',]
config.action(
- discriminator=('adapter', for_, IContentProvider, name),
- callable=component.provideAdapter,
- args=(provider, for_, IContentProvider, name),
+ discriminator = ('five:protectClass', factory),
+ callable = protectClass,
+ args = (factory, permission)
)
+ config.action(
+ discriminator = ('five:protectName', factory, attributes),
+ callable = protectname,
+ args = (factory, attributes, permission)
+ )
- return True
-
- def checkTemplates(self, templates, module_info, provider):
- def has_render(provider):
- return provider.render != components.ViewletManager.render
- def has_no_render(provider):
- # always has a render method
- return False
- templates.checkTemplates(module_info, provider, 'viewlet manager',
- has_render, has_no_render)
-
-
-class ViewletGrokker(ViewletManagerGrokker):
-
- martian.component(components.Viewlet)
- martian.directive(five.grok.viewletmanager)
-
- def execute(self, provider, name, context, view,
- layer, viewletmanager, config, **kw):
- """Register a viewlet.
- """
- templates = provider.module_info.getAnnotation('grok.templates', None)
- if templates is not None:
- config.action(
- discriminator=None,
- callable=self.checkTemplates,
- args=(templates, provider.module_info, provider)
- )
-
- for_ = (context, layer, view, viewletmanager)
+ # Protect the class
config.action(
- discriminator=('adapter', for_, IViewlet, name),
- callable=provideAdapter,
- args=(provider, for_, IViewlet, name),
+ discriminator = ('five:initialize:class', factory),
+ callable = initializeClass,
+ args = (factory,)
)
return True
-
- def checkTemplates(self, templates, module_info, provider):
- def has_render(provider):
- return provider.render != components.Viewlet.render
- def has_no_render(provider):
- return not has_render(provider)
- templates.checkTemplates(module_info, provider, 'viewlet',
- has_render, has_no_render)
Modified: five.grok/branches/sylvain-viewlets/src/five/grok/meta.zcml
===================================================================
--- five.grok/branches/sylvain-viewlets/src/five/grok/meta.zcml 2008-10-25 01:26:56 UTC (rev 92537)
+++ five.grok/branches/sylvain-viewlets/src/five/grok/meta.zcml 2008-10-25 01:29:52 UTC (rev 92538)
@@ -8,6 +8,7 @@
<!-- Load minimal support of grokcore.view, views + templates -->
<include package="grokcore.view" file="meta-minimal.zcml" />
+ <include package="grokcore.viewlet" file="meta.zcml" />
<include package="grokcore.formlib" file="meta.zcml" />
<!-- Include skin support as well -->
Modified: five.grok/branches/sylvain-viewlets/src/five/grok/templatereg.py
===================================================================
--- five.grok/branches/sylvain-viewlets/src/five/grok/templatereg.py 2008-10-25 01:26:56 UTC (rev 92537)
+++ five.grok/branches/sylvain-viewlets/src/five/grok/templatereg.py 2008-10-25 01:29:52 UTC (rev 92538)
@@ -1,3 +1,17 @@
+#############################################################################
+#
+# Copyright (c) 2008 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
import grokcore.component
from grokcore.view.interfaces import ITemplateFileFactory
@@ -8,4 +22,4 @@
grokcore.component.name('pt')
def __call__(self, filename, _prefix=None):
- return ZopeTwoPageTemplate(filename=filename, _prefix=_prefix)
\ No newline at end of file
+ return ZopeTwoPageTemplate(filename=filename, _prefix=_prefix)
More information about the Checkins
mailing list