[Checkins] SVN: zope3org/trunk/src/zorg/multiform/ added gridform
and selection stuff
Bernd Dorn
bernd.dorn at fhv.at
Mon Apr 10 08:21:53 EDT 2006
Log message for revision 66775:
added gridform and selection stuff
Changed:
A zope3org/trunk/src/zorg/multiform/grid.pt
A zope3org/trunk/src/zorg/multiform/gridform.py
A zope3org/trunk/src/zorg/multiform/gridform.txt
A zope3org/trunk/src/zorg/multiform/griditem.pt
A zope3org/trunk/src/zorg/multiform/selections.txt
-=-
Added: zope3org/trunk/src/zorg/multiform/grid.pt
===================================================================
--- zope3org/trunk/src/zorg/multiform/grid.pt 2006-04-10 12:21:01 UTC (rev 66774)
+++ zope3org/trunk/src/zorg/multiform/grid.pt 2006-04-10 12:21:52 UTC (rev 66775)
@@ -0,0 +1,4 @@
+<html metal:extend-macro="context/@@standard_macros/view"
+ metal:define-macro="main">
+</html>
+
Added: zope3org/trunk/src/zorg/multiform/gridform.py
===================================================================
--- zope3org/trunk/src/zorg/multiform/gridform.py 2006-04-10 12:21:01 UTC (rev 66774)
+++ zope3org/trunk/src/zorg/multiform/gridform.py 2006-04-10 12:21:52 UTC (rev 66775)
@@ -0,0 +1,116 @@
+from interfaces import ISelection,IFormLocation,IGridForm,IGridItemForm
+from zope.schema.fieldproperty import FieldProperty
+from zope.interface import implements
+from zope.proxy import ProxyBase, getProxiedObject
+from zope.app.decorator import DecoratorSpecificationDescriptor
+from zope.app.decorator import DecoratedSecurityCheckerDescriptor
+from zope.app.location import location
+import multiform
+from zope.formlib import namedtemplate
+from zope.app.pagetemplate import ViewPageTemplateFile
+
+
+default_grid_template = namedtemplate.NamedTemplateImplementation(
+ ViewPageTemplateFile('grid.pt'), IGridForm)
+
+default_griditem_template = namedtemplate.NamedTemplateImplementation(
+ ViewPageTemplateFile('griditem.pt'), IGridItemForm)
+
+
+class FormLocationSelection(object):
+
+ implements(ISelection)
+
+ def __init__(self,context):
+ self.key = '_mf_selection.' + context.__form__.prefix + \
+ "." + context.__name__
+ self.request = context.__form__.request
+
+ def _setSelected(self,v):
+ self.request.form[self.key]=v
+
+ def _getSelected(self):
+ self.request.form.get(key,False)
+
+ selected = property(_getSelected,_setSelected)
+
+
+class FormLocationProxy(ProxyBase):
+
+ __doc__ = """Form Location-object proxy
+
+ This is a non-picklable proxy that can be put around objects that
+ implement `ILocation`.
+
+ >>> from zope import interface
+ >>> class IMarker(interface.Interface): pass
+ >>> class L(object):
+ ... x = 1
+ >>> l = L()
+ >>> interface.directlyProvides(l,IMarker)
+ >>> p = FormLocationProxy(l, "Form")
+ >>> p.x
+ 1
+ >>> p.x=2
+ >>> p.x
+ 2
+ >>> l.x
+ 2
+ >>> p.__form__
+ 'Form'
+
+ >>> IFormLocation.providedBy(p)
+ True
+
+ >>> IMarker.providedBy(p)
+ True
+
+ >>> import pickle
+ >>> p2 = pickle.dumps(p)
+ Traceback (most recent call last):
+ ...
+ TypeError: Not picklable
+
+ Proxies should get their doc strings from the object they proxy:
+
+ >>> p.__doc__ == l.__doc__
+ True
+
+ """
+
+ implements(IFormLocation)
+
+ __slots__ = '__form__'
+ __safe_for_unpickling__ = True
+
+ def __new__(self, ob, form):
+ return ProxyBase.__new__(self, ob)
+
+ def __init__(self, ob, form):
+ ProxyBase.__init__(self, ob)
+ self.__form__ = form
+
+ def __reduce__(self, proto=None):
+ raise TypeError("Not picklable")
+
+
+ __doc__ = location.ClassAndInstanceDescr(
+ lambda inst: getProxiedObject(inst).__doc__,
+ lambda cls, __doc__ = __doc__: __doc__,
+ )
+
+ __reduce_ex__ = __reduce__
+
+ __providedBy__ = DecoratorSpecificationDescriptor()
+
+ __Security_checker__ = DecoratedSecurityCheckerDescriptor()
+
+
+
+class GridItemFormBase(multiform.ItemFormBase):
+ implements(IGridItemForm)
+ template = namedtemplate.NamedTemplate('default')
+
+class GridFormBase(multiform.MultiFormBase):
+ implements(IGridForm)
+ template = namedtemplate.NamedTemplate('default')
Added: zope3org/trunk/src/zorg/multiform/gridform.txt
===================================================================
--- zope3org/trunk/src/zorg/multiform/gridform.txt 2006-04-10 12:21:01 UTC (rev 66774)
+++ zope3org/trunk/src/zorg/multiform/gridform.txt 2006-04-10 12:21:52 UTC (rev 66775)
@@ -0,0 +1,86 @@
+===========
+ Gridforms
+===========
+
+Gridforms are specialized multiforms which are supposed to be
+displayed as grids.
+
+Let's define a simple content object
+
+ >>> from multiform.interfaces import ISelection
+ >>> from zope.formlib import form
+ >>> from multiform import multiform, gridform
+ >>> from zope.publisher.browser import TestRequest
+ >>> from zope import interface, schema
+ >>> from zope.app.location.interfaces import ILocation
+ >>> class IOrder(interface.Interface):
+ ... identifier = schema.Int(title=u"Identifier", readonly=True)
+ ... name = schema.TextLine(title=u"Name")
+
+ >>> class Order:
+ ... interface.implements(IOrder,ILocation)
+ ...
+ ... def __init__(self, identifier, name=''):
+ ... self.identifier = identifier
+ ... self.name = name
+ ... self.__name__= name
+
+ >>> orderMapping = dict([(str(k),Order(k,name='n%s'%k)) for k in range(2)])
+
+Now we use the ``GridForm`` as a base class to display orders in
+tabular form. Additionally to the IOrder schema the ISelection schema
+is added to the ``form_fields`` of the ``OrderForm`` in order to get
+selectable items in our order grid.
+
+
+ >>> def isSelectedInput(form,action):
+ ... if not form.parentForm.inputMode:
+ ... return False
+ ... return ISelection(form.context).selected
+
+ >>> class OrderForm(gridform.GridItemFormBase):
+ ... inputMode=False
+ ... forceInput=['selected']
+ ...
+ ... def __init__(self,context,request,parentForm):
+ ... super(OrderForm,self).__init__(context,request,parentForm)
+ ... self.form_fields = form.Fields(ISelection,IOrder,
+ ... omit_readonly=False)
+ ...
+ ... def template(self):
+ ... return '\n<div>%s</div>\n' % '</div>\n<div>'.join([w() for w in
+ ... self.widgets])
+ ...
+ ... @multiform.parentAction(u"Save",
+ ... condition=isSelectedInput,inputMode=True)
+ ... def handle_save_action(self, action, data):
+ ... form.applyChanges(self.context, self.form_fields,
+ ... data, self.adapters)
+ ... self.parentForm.newInputMode=False
+ ...
+ ... @multiform.parentAction('Edit',
+ ... condition=multiform.isParentFormDisplayMode)
+ ... def handle_edit_action(self, action, data):
+ ... self.newInputMode=True
+
+
+
+ >>> class OrdersForm(gridform.GridFormBase):
+ ... itemFormFactory=OrderForm
+ ... def template(self):
+ ... res = u''
+ ... names = sorted(self.subForms.keys())
+ ... for name in names:
+ ... res += '<div>%s</div>\n' % self.subForms[name].render()
+ ... return res
+ ...
+ ... @form.action('Cancel',condition=multiform.isFormInputMode)
+ ... def handle_cancel_action(self, action, data):
+ ... self.newInputMode=False
+
+ >>> request = TestRequest()
+ >>> gf = OrdersForm(orderMapping,request)
+ >>> print gf()
+ <div>...
+ </div>
+
Added: zope3org/trunk/src/zorg/multiform/griditem.pt
===================================================================
--- zope3org/trunk/src/zorg/multiform/griditem.pt 2006-04-10 12:21:01 UTC (rev 66774)
+++ zope3org/trunk/src/zorg/multiform/griditem.pt 2006-04-10 12:21:52 UTC (rev 66775)
@@ -0,0 +1,3 @@
+<div>
+ griditem
+</div>
\ No newline at end of file
Added: zope3org/trunk/src/zorg/multiform/selections.txt
===================================================================
--- zope3org/trunk/src/zorg/multiform/selections.txt 2006-04-10 12:21:01 UTC (rev 66774)
+++ zope3org/trunk/src/zorg/multiform/selections.txt 2006-04-10 12:21:52 UTC (rev 66775)
@@ -0,0 +1,14 @@
+======================
+ Multiform Selections
+======================
+
+Selecting subforms of a multiform is a common task which is supported
+by the multiform package. Selections are implemented by the ISelection
+interface. This interface can be adapted to from objects which
+implement IFormLocation which in turn can be adapted to from ILocation
+and IForm. The IFormLocation adpater is a proxy which provides the
+``__form_name__`` attribute.
+
+The ISelection implementation in this package uses the request to
+store the selection state, by using the ILocationForm interface.
+
More information about the Checkins
mailing list