Skip to content

Commit

Permalink
Added all code from Five/formlib
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Dec 26, 2009
1 parent 7d97157 commit aba3d24
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/five/formlib/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,21 @@
allowed_interface="zope.interface.common.mapping.IItemMapping"
/>

</configure>
<configure package="zope.formlib">

<adapter
factory=".form.render_submit_button"
name="render"
/>

<!-- Error view for 'Invalid' -->
<adapter
for="zope.interface.Invalid
zope.publisher.interfaces.browser.IBrowserRequest"
factory=".errors.InvalidErrorView"
permission="zope.Public"
/>

</configure>

</configure>
117 changes: 117 additions & 0 deletions src/five/formlib/formbase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
##############################################################################
#
# Copyright (c) 2006 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.
#
##############################################################################
"""Five baseclasses for zope.formlib.form
$Id$
"""
import os.path

import zope.event
import zope.formlib
import zope.lifecycleevent
from zope import interface
from zope.formlib import interfaces, form
from zope.i18nmessageid import MessageFactory
_ = MessageFactory("zope")

from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.Five.browser.decode import processInputs, setPageEncoding

_FORMLIB_DIR = os.path.dirname(zope.formlib.__file__)
_PAGEFORM_PATH = os.path.join(_FORMLIB_DIR, 'pageform.pt')
_SUBPAGEFORM_PATH = os.path.join(_FORMLIB_DIR, 'subpageform.pt')


class FiveFormlibMixin(object):

# Overrides the formlib.form.FormBase.template attributes implemented
# using NamedTemplates. NamedTemplates using ViewPageTemplateFile (like
# formlib does by default) cannot work in Zope2.

# XXX Maybe we need to have Five-compatible NamedTemplates?

template = ViewPageTemplateFile(_PAGEFORM_PATH)

# Overrides formlib.form.FormBase.update. Make sure user input is
# decoded first and the page encoding is set before proceeding.

def update(self):
processInputs(self.request)
setPageEncoding(self.request)
super(FiveFormlibMixin, self).update()


class FormBase(FiveFormlibMixin, form.FormBase):
pass


class EditFormBase(FiveFormlibMixin, form.EditFormBase):
pass


class DisplayFormBase(FiveFormlibMixin, form.DisplayFormBase):
pass


class AddFormBase(FiveFormlibMixin, form.AddFormBase):
pass


class PageForm(FormBase):

interface.implements(interfaces.IPageForm)

Form = PageForm


class PageEditForm(EditFormBase):

interface.implements(interfaces.IPageForm)

EditForm = PageEditForm


class PageDisplayForm(DisplayFormBase):

interface.implements(interfaces.IPageForm)

DisplayForm = PageDisplayForm


class PageAddForm(AddFormBase):

interface.implements(interfaces.IPageForm)

AddForm = PageAddForm


class SubPageForm(FormBase):

template = ViewPageTemplateFile(_SUBPAGEFORM_PATH)

interface.implements(interfaces.ISubPageForm)


class SubPageEditForm(EditFormBase):

template = ViewPageTemplateFile(_SUBPAGEFORM_PATH)

interface.implements(interfaces.ISubPageForm)


class SubPageDisplayForm(DisplayFormBase):

template = ViewPageTemplateFile(_SUBPAGEFORM_PATH)

interface.implements(interfaces.ISubPageForm)
15 changes: 15 additions & 0 deletions src/five/formlib/tests/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,19 @@

<i18n:registerTranslations directory="locales"/>


<browser:page
name="add_content"
for="*"
class=".view.AddContentForm"
permission="zope2.Public"
/>

<browser:page
name="edit_content"
for=".content.IContent"
class=".view.EditContentForm"
permission="zope2.Public"
/>

</configure>
65 changes: 65 additions & 0 deletions src/five/formlib/tests/content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
##############################################################################
#
# Copyright (c) 2004, 2005 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.
#
##############################################################################
"""Test content.
$Id$
"""
from App.class_init import InitializeClass
from OFS.SimpleItem import SimpleItem

from zope.i18nmessageid import MessageFactory
from zope.interface import implements
from zope.interface import Interface
from zope.schema import ASCIILine
from zope.schema import List
from zope.schema import TextLine

_ = MessageFactory('formtest')

class IContent(Interface):

id = ASCIILine(
title=_(u"Id"),
description=_(u"The object id."),
default='',
required=True
)

title = TextLine(
title=_(u"Title"),
description=_(u"A short description of the event."),
default=u"",
required=True
)

somelist = List(
title=_(u"Some List"),
value_type=TextLine(title=_(u"Some item")),
default=[],
required=False
)

class Content(SimpleItem):
"""A Viewable piece of content with fields
"""
implements(IContent)

meta_type = 'Five Formlib Test Content'

def __init__(self, id, title, somelist=None):
self.id = id
self.title = title
self.somelist = somelist

InitializeClass(Content)
86 changes: 86 additions & 0 deletions src/five/formlib/tests/formlib.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Testing formlib integration
===========================

This doctest is will test the Five formlib support and to provide some
examples.

It will not test the actual formlib functionality. See
zope/formlib/form.txt for tests and more explanations of zope.formlib

Before we can begin, we need to set up a few things. We need a manager
account:

>>> uf = self.folder.acl_users
>>> uf._doAddUser('manager', 'r00t', ['Manager'], [])

We need to configure all of Five and the necessary formlib components for
this test:

>>> from Products.Five import zcml
>>> import Products.Five
>>> zcml.load_config('meta.zcml', Products.Five)
>>> import Products.Five.form.tests
>>> zcml.load_config('configure.zcml', package=Products.Five)
>>> zcml.load_config('configure.zcml', package=Products.Five.formlib.tests)

Finally, we need to setup a traversable folder. Otherwise, Five won't get
to to do its view lookup:

>>> from Products.Five.tests.testing import manage_addFiveTraversableFolder
>>> manage_addFiveTraversableFolder(self.folder, 'ftf')

Let's set up a testbrowser:

>>> from Products.Five.testbrowser import Browser
>>> browser = Browser()
>>> browser.addHeader('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')
>>> browser.addHeader('Accept-Language', 'en-US')
>>> browser.addHeader('Authorization', 'Basic manager:r00t')

Let's 'manually' create a Content instance and add it to the folder:

>>> from Products.Five.formlib.tests import content
>>> folder = self.folder.ftf
>>> obj = content.Content('content_1', 'Title', [])
>>> folder._setObject('content_1', obj)
'content_1'
>>> print folder.content_1.title
Title

Now we can edit this content object, e.g. changing the title. Formlib,
like the traditional AddView and EditView, supports unicode:

>>> browser.open("http://localhost/test_folder_1_/ftf/content_1/@@edit_content")
>>> ni_hao = '\xe4\xbd\xa0\xe5\xa5\xbd'
>>> ctl = browser.getControl(name="form.title")
>>> ctl.value = ni_hao
>>> browser.getControl(name="form.actions.apply").click()
>>> isinstance(folder.content_1.title, unicode)
True
>>> folder.content_1.title == ni_hao.decode('utf-8')
True

Adding a new content object, with two list items:

>>> browser.open("http://localhost/test_folder_1_/ftf/@@add_content")
>>> ctl = browser.getControl(name="form.id")
>>> ctl.value = 'test123'
>>> ctl = browser.getControl(name="form.title")
>>> ctl.value = ni_hao
>>> browser.getControl(name="form.somelist.add").click()
>>> ctl = browser.getControl(name="form.somelist.0.")
>>> ctl.value = 'a nice list item'
>>> browser.getControl(name="form.somelist.add").click()
>>> ctl = browser.getControl(name="form.somelist.1.")
>>> ctl.value = 'a nice list item'
>>> browser.getControl(name="form.actions.add").click()
>>> print folder.test123.somelist
[u'a nice list item', u'a nice list item']

Clean up
--------

Finally, we need to clean up:

>>> from zope.component.testing import tearDown
>>> tearDown()
25 changes: 25 additions & 0 deletions src/five/formlib/tests/test_formlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##############################################################################
#
# Copyright (c) 2006 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.
#
##############################################################################
"""Test forms
$Id$
"""

def test_suite():
import unittest
from Testing.ZopeTestCase import FunctionalDocFileSuite
return unittest.TestSuite((
FunctionalDocFileSuite(
'formlib.txt', package='Products.Five.formlib.tests'),
))
40 changes: 40 additions & 0 deletions src/five/formlib/tests/view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
##############################################################################
#
# Copyright (c) 2006 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.
#
##############################################################################
"""Five baseclasses for zope.formlib.form
$Id$
"""
from zope.formlib import form

from Products.Five.formlib.formbase import AddForm, EditForm
from Products.Five.formlib.tests.content import IContent, Content

class AddContentForm(AddForm):
"""AddForm for creating and adding IContent objects
"""

form_fields = form.Fields(IContent)

def createAndAdd(self, data):
id = data.get('id')
ctnt = Content(
id, data.get('title'), somelist=data.get('somelist'))
self.context._setObject(id, ctnt)

class EditContentForm(EditForm):
"""EditForm for editing IContent objects
"""

form_fields = form.Fields(IContent)

0 comments on commit aba3d24

Please sign in to comment.