Skip to content

Commit

Permalink
Make formlib support optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
thefunny42 committed May 2, 2012
1 parent 79bac10 commit 30bdbc2
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 70 deletions.
15 changes: 8 additions & 7 deletions buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ versions = versions

[versions]
plone.recipe.zope2instance =
grokcore.annotation = 1.2
grokcore.component = 2.4
grokcore.formlib = 1.8
grokcore.security = 1.5
grokcore.site = 1.5
grokcore.view = 2.6.1
grokcore.annotation = 1.3
grokcore.component = 2.5
grokcore.formlib = 1.9
grokcore.security = 1.6
grokcore.site = 1.6.1
grokcore.view = 2.7
grokcore.viewlet = 1.9
five.localsitemanager = 2.0.5
five.formlib = 1.0.4
Expand Down Expand Up @@ -46,5 +46,6 @@ products =

[test]
recipe = zc.recipe.testrunner
eggs = ${instance:eggs}
eggs =
five.grok [test]
defaults = ['-m', 'five.grok', '--tests-pattern', '^f?tests$', '-v']
8 changes: 6 additions & 2 deletions docs/HISTORY.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
Changelog
=========

1.4.0 (unreleased)
1.3.1 (unreleased)
------------------

- ...
- Make formlib support optional. This is not included by default. If
you whish to use formlib, you need to include the extra ``form`` for
``five.grok``. Example::

five.grok [form] >= 1.3.1

1.3.0 (2011-11-07)
------------------
Expand Down
18 changes: 13 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

version = '1.4.0dev'

form_requires = [
'grokcore.formlib >= 1.4',
'five.formlib',
'zope.formlib',
]
test_requires = form_requires + [
]

setup(name='five.grok',
version=version,
description="Grok-like layer for Zope 2",
Expand Down Expand Up @@ -30,24 +38,24 @@
'setuptools',
'martian',
'Zope2>=2.13',
'five.formlib',
'five.localsitemanager > 2.0dev',
'grokcore.annotation',
'grokcore.component',
'grokcore.formlib >= 1.4',
'grokcore.security',
'grokcore.component >= 2.5',
'grokcore.security [role] >= 1.6.1',
'grokcore.site',
'grokcore.view >= 1.12.1',
'grokcore.viewlet >= 1.3',
'zope.annotation',
'zope.component',
'zope.container',
'zope.contentprovider',
'zope.formlib',
'zope.interface',
'zope.location',
'zope.pagetemplate',
'zope.publisher',
'zope.traversing',
],
extras_require={
'form': form_requires,
'test': form_requires},
)
12 changes: 7 additions & 5 deletions src/five/grok/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@
from grokcore.site import *
from grokcore.view import *
from grokcore.viewlet import *
from grokcore.formlib import *

from five.grok.components import Model, Container, Site, LocalUtility
from five.grok.components import Form, AddForm
from five.grok.components import EditForm, DisplayForm
from five.grok.components import View, ViewletManager
from five.grok.formlib import AutoFields


# Override the one from grokcore.view so that we get Zope 2 semantics
Expand All @@ -35,5 +31,11 @@
from five.grok.components import ZopeTwoDirectoryResource as DirectoryResource

# Only export public API
from five.grok.interfaces import IFiveGrokAPI
from five.grok.interfaces import IFiveGrokAPI, HAVE_FORMLIB
if HAVE_FORMLIB:
from grokcore.formlib import *
from five.grok.components import Form, AddForm
from five.grok.components import EditForm, DisplayForm
from five.grok.formlib import AutoFields

__all__ = list(IFiveGrokAPI)
70 changes: 37 additions & 33 deletions src/five/grok/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@
from zope.location.interfaces import IPossibleSite
from zope import interface

from five.formlib import formbase

from grokcore.component.interfaces import IContext
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 ViewletManager as BaseViewletManager
from grokcore.site.components import BaseSite
import grokcore.view

from five.grok.interfaces import HAVE_FORMLIB

from Products.Five.browser.pagetemplatefile import ViewMapper
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.Five.browser.pagetemplatefile import getEngine
Expand Down Expand Up @@ -175,51 +172,58 @@ def __call__(self, request):
return resource


# forms from formlib

class GrokForm(BaseGrokForm):
# Viewlet / Viewlet Manager

def __init__(self, *args):
super(GrokForm, self).__init__(*args)
self.__name__ = self.__view_name__

class ViewletManager(BaseViewletManager, ZopeTwoBaseViewletManager):

class Form(GrokForm, formbase.PageForm, View):
martian.baseclass()
template = default_form_template

def filter(self, viewlets):
# XXX Need Zope 2 filter
return ZopeTwoBaseViewletManager.filter(self, viewlets)

class AddForm(GrokForm, formbase.AddForm, View):
martian.baseclass()
template = default_form_template
def __getitem__(self, key):
# XXX Need Zope 2 __getitem__
return ZopeTwoBaseViewletManager.__getitem__(self, key)

if HAVE_FORMLIB:
from five.formlib import formbase
from grokcore.formlib.components import GrokForm as BaseGrokForm
from grokcore.formlib.components import default_display_template, \
default_form_template

class EditForm(GrokForm, formbase.EditForm, View):
martian.baseclass()
template = default_form_template
# forms from formlib

# grokcore.formlib defines empty actions since 1.1. Restore save
# option here.
actions = formbase.EditForm.actions
class GrokForm(BaseGrokForm):

def __init__(self, *args):
super(GrokForm, self).__init__(*args)
self.__name__ = self.__view_name__

class DisplayForm(GrokForm, formbase.DisplayForm, View):
martian.baseclass()
template = default_display_template

class Form(GrokForm, formbase.PageForm, View):
martian.baseclass()
template = default_form_template

# Viewlet / Viewlet Manager

class AddForm(GrokForm, formbase.AddForm, View):
martian.baseclass()
template = default_form_template

class ViewletManager(BaseViewletManager, ZopeTwoBaseViewletManager):

martian.baseclass()
class EditForm(GrokForm, formbase.EditForm, View):
martian.baseclass()
template = default_form_template

def filter(self, viewlets):
# XXX Need Zope 2 filter
return ZopeTwoBaseViewletManager.filter(self, viewlets)
# grokcore.formlib defines empty actions since 1.1. Restore save
# option here.
actions = formbase.EditForm.actions


class DisplayForm(GrokForm, formbase.DisplayForm, View):
martian.baseclass()
template = default_display_template

def __getitem__(self, key):
# XXX Need Zope 2 __getitem__
return ZopeTwoBaseViewletManager.__getitem__(self, key)

3 changes: 2 additions & 1 deletion src/five/grok/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<include package="Products.Five" />
<include package="five.grok" file="meta.zcml" />

<include package="five.formlib" />
<include package="five.formlib"
zcml:condition="installed five.formlib" />
<include package="five.localsitemanager" />
<include package="grokcore.annotation" />
<include package="grokcore.view" />
Expand Down
15 changes: 13 additions & 2 deletions src/five/grok/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@
import grokcore.site.interfaces
import grokcore.view.interfaces
import grokcore.viewlet.interfaces
import grokcore.formlib.interfaces

try:
from grokcore.formlib.interfaces import IGrokcoreFormlibAPI

HAVE_FORMLIB = True
except ImportError:
from zope.interface import Interface

class IGrokcoreFormlibAPI(Interface):
"""Empty FormlibAPI
"""
HAVE_FORMLIB = False


class IFiveGrokView(grokcore.view.interfaces.IGrokView):
Expand All @@ -33,6 +44,6 @@ class IFiveGrokAPI(grokcore.annotation.interfaces.IGrokcoreAnnotationAPI,
grokcore.site.interfaces.IGrokcoreSiteAPI,
grokcore.view.interfaces.IGrokcoreViewAPI,
grokcore.viewlet.interfaces.IGrokcoreViewletAPI,
grokcore.formlib.interfaces.IGrokcoreFormlibAPI):
IGrokcoreFormlibAPI):
"""Official five.grok API.
"""
30 changes: 17 additions & 13 deletions src/five/grok/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
#
##############################################################################

from five.grok import components, formlib
from five.grok import components
from grokcore.view.meta.directoryresource import _get_resource_path
from zope import interface, component
from zope import interface
from zope.publisher.interfaces.browser import IDefaultBrowserLayer

import five.grok
import grokcore.component
import grokcore.security
Expand All @@ -26,17 +27,20 @@
from App.class_init import InitializeClass as initializeClass


class FormGrokker(martian.ClassGrokker):
martian.component(components.GrokForm)
martian.directive(grokcore.component.context)
martian.priority(800) # Must be run before real formlib grokker.
if components.HAVE_FORMLIB:
from five.grok import formlib

def execute(self, factory, config, context, **kw):
# Set up form_fields from context class if they haven't been
# configured manually already using our version of get_auto_fields
if getattr(factory, 'form_fields', None) is None:
factory.form_fields = formlib.get_auto_fields(context)
return True
class FormGrokker(martian.ClassGrokker):
martian.component(components.GrokForm)
martian.directive(grokcore.component.context)
martian.priority(800) # Must be run before real formlib grokker.

def execute(self, factory, config, context, **kw):
# Set up form_fields from context class if they haven't been
# configured manually already using our version of get_auto_fields
if getattr(factory, 'form_fields', None) is None:
factory.form_fields = formlib.get_auto_fields(context)
return True


class ViewSecurityGrokker(martian.ClassGrokker):
Expand Down Expand Up @@ -71,7 +75,7 @@ def _register_resource(config, resource_path, name, layer):

config.action(
discriminator=('adapter', adapts, provides, name),
callable=component.provideAdapter,
callable=grokcore.component.util.provideAdapter,
args=(resource_factory, adapts, provides, name),
)
return True
Expand Down
6 changes: 4 additions & 2 deletions src/five/grok/meta.zcml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:meta="http://namespaces.zope.org/meta"
xmlns:zcml="http://namespaces.zope.org/zcml"
xmlns:grok="http://namespaces.zope.org/grok">

<include package="grokcore.component" file="meta.zcml" />
Expand All @@ -11,7 +11,9 @@
<!-- 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 package="grokcore.formlib" file="meta.zcml"
zcml:condition="installed grokcore.formlib" />

<!-- Include skin support as well -->
<grok:grok package="grokcore.view.meta.skin" />
Expand Down

0 comments on commit 30bdbc2

Please sign in to comment.