From: Jason M. <svn...@pl...> - 2010-08-31 00:35:46
|
Author: JMehring Date: Tue Aug 31 00:35:37 2010 New Revision: 39556 Modified: plone.z3cform/branches/jmehring-drafts/plone/z3cform/buttonoverrides.py plone.z3cform/branches/jmehring-drafts/plone/z3cform/interfaces.py Log: pep8ified draft code Modified: plone.z3cform/branches/jmehring-drafts/plone/z3cform/buttonoverrides.py ============================================================================== --- plone.z3cform/branches/jmehring-drafts/plone/z3cform/buttonoverrides.py (original) +++ plone.z3cform/branches/jmehring-drafts/plone/z3cform/buttonoverrides.py Tue Aug 31 00:35:37 2010 @@ -1,14 +1,51 @@ +"""buttonoverrides.py contains code required to override and/or add buttons +in existing z3c.form forms. + +ButtonAndHandlerSubscriber must be extended to includ the required buttons and handlers +to be modified + +ButtonActions overrides the delfault z3c.form ButtonActions in order to be +able to replace buttons on the fly. A zcml entry like the following must +per present for it to be called. Note that the original ButtonActions is +called within this class: + + <adapter + for="z3c.form.interfaces.IAddForm + plone.app.drafts.interfaces.IDraft + zope.interface.Interface" + factory="plone.z3cform.buttonoverrides.ButtonActions" + /> + <adapter + for="z3c.form.interfaces.IEditForm + plone.app.drafts.interfaces.IDraft + zope.interface.Interface" + factory="plone.z3cform.buttonoverrides.ButtonActions" + /> +""" + import zope.component import zope.interface -from zope.component.interfaces import IObjectEvent from zope.component.interfaces import ObjectEvent from zope.event import notify import z3c.form.button -class ButtonAndHandler(object): +from plone.z3cform.interfaces import IButtonOverrideEvent + + +class ButtonAndHandlerSubscriber(object): """Base convenience class. - buttonHandler and updateActions must be implemented + + Contains the custom button and handlers that will be overridden in the form. + This class must be extended and then a zcml configuration entry like follows: + + <subscriber + for="plone.dexterity.browser.add.DefaultAddForm + plone.z3cform.interfaces.IButtonOverrideEvent" + handler=".drafts.AddCancelDraftButtonAndHandlerSubscriber" + /> + + Note that buttonHandler and updateActions must be implemented. """ # Position of button. Best to use large numbers like @@ -18,8 +55,9 @@ originalButtons = None originalHandlers = None - def __init__( self, form, event ): + def __init__(self, form, event): self.form = form + self.event = event self.context = form.getContent() self.request = form.request @@ -29,44 +67,32 @@ def updateActions(self): """Must be implemented""" -class ButtonActions( z3c.form.button.ButtonActions ): + +class ButtonActions(z3c.form.button.ButtonActions): """Adapter for z3cform.form updateActions() to allow replacing of original form button and handlers with something custom""" - def appendAttr(self, obj, name, value): - if value is None: - return - - attr = getattr( obj, name, None ) - - if attr is None: - setattr( obj, name, value ) - else: - attr += value - setattr( obj, name, attr ) - - def appendButtonAndHandler( self, buttonAndHandler ): - if buttonAndHandler is None: - return - - self.appendAttr( self.form, 'buttons', buttonAndHandler.buttons ) - self.appendAttr( self.form, 'handlers', buttonAndHandler.handlers ) - def getButtonsAndHandlers(self, buttons, handlers): - b = {} - h = {} - - for name, button in buttons.items(): - b[name] = buttons.get(name) - h[name] = handlers.getHandler( buttons.get(name) ) + """Returns a tuple containing dictionary of buttons and handlers + """ + buttonsButtons = {} + handlersHandlers = {} + + #for name, button in buttons.items(): + for name in buttons.keys(): + buttonsButtons[name] = buttons.get(name) + handlersHandlers[name] = handlers.getHandler(buttons.get(name)) - return (b, h) + return (buttonsButtons, handlersHandlers) # Add our custom buttons here def update(self): + """Overrides z3c.form.button.ButtonActions.update() to allow override + or addition of custom buttons. Original ButtonAction is called + """ # Check to see if already 'cached' cached = False - buttonandhandlers = getattr( self.form, 'buttonsandhandlers', None ) + buttonandhandlers = getattr(self.form, 'buttonsandhandlers', None) if buttonandhandlers is None: # Get the custom buttons and handlers for the view, if available # Any event subscribers will automatically add their button and @@ -76,12 +102,12 @@ else: cached = True - if len( self.form.buttonsandhandlers ) is not 0 and not cached: + if len(self.form.buttonsandhandlers) is not 0 and not cached: buttons = z3c.form.button.Buttons() handlers = z3c.form.button.Handlers() # Dictionary of all original and new buttons and handlers - originalButtons, originalHandlers = self.getButtonsAndHandlers( self.form.buttons, self.form.handlers ) + originalHandlers = self.getButtonsAndHandlers(self.form.buttons, self.form.handlers)[1] newButtons = {} newHandlers = {} @@ -89,12 +115,12 @@ buttonList = [] for iface, buttonandhandler in self.form.buttonsandhandlers.items(): - b, h = self.getButtonsAndHandlers( buttonandhandler.buttons, buttonandhandler.handlers ) - name, button = b.items()[0] + buttonDictionary, handlerDictionary = self.getButtonsAndHandlers(buttonandhandler.buttons, buttonandhandler.handlers) + name, button = buttonDictionary.items()[0] newButtons[name] = button - newHandlers.update( h ) - buttonList.append( (self.form.buttonsandhandlers[iface].position, button) ) + newHandlers.update(handlerDictionary) + buttonList.append((self.form.buttonsandhandlers[iface].position, button)) # position original buttons that have no override first in the list defaultPosition = 10 @@ -102,7 +128,7 @@ if name not in newButtons: newButtons[name] = button newHandlers[button] = originalHandlers.get(name) - buttonList.append( (defaultPosition, button) ) + buttonList.append((defaultPosition, button)) defaultPosition += 10 # Need to sort buttonList now @@ -110,13 +136,12 @@ # Add remaining 'new' buttons and overrides for buttonTuple in buttonList: - button = buttonTuple[1] - name = button.getName() - if name in newButtons: # Should always be true - b = newButtons.get(name) - h = newHandlers.get(name) - buttons += z3c.form.button.Buttons( b ) - handlers.addHandler( b, h ) + name = buttonTuple[1].getName() + if name in newButtons: # Should always be true + button = newButtons.get(name) + handler = newHandlers.get(name) + buttons += z3c.form.button.Buttons(button) + handlers.addHandler(button, handler) # We want to use our own custom buttons and handlers; # although we will still call the old handlers if they exist @@ -126,24 +151,20 @@ self.form.handlers = handlers # Update again to provide new buttons - super(ButtonActions,self).update() + super(ButtonActions, self).update() # Update actions - if len( self.form.buttonsandhandlers ) != 0: + if len(self.form.buttonsandhandlers) != 0: for iface, buttonandhandler in self.form.buttonsandhandlers.items(): # Should only ever be one button in object if buttonandhandler.buttons.keys()[0] in self.form.actions: buttonandhandler.updateActions() -################################################################################ -# Event interfaces -class IButtonOverrideEvent(IObjectEvent): - """Marker interface for event signalling - """ + #Event definitions class ButtonOverrideEvent(ObjectEvent): """Looking for button override subscribers """ - zope.interface.implements( IButtonOverrideEvent ) + zope.interface.implements(IButtonOverrideEvent) Modified: plone.z3cform/branches/jmehring-drafts/plone/z3cform/interfaces.py ============================================================================== --- plone.z3cform/branches/jmehring-drafts/plone/z3cform/interfaces.py (original) +++ plone.z3cform/branches/jmehring-drafts/plone/z3cform/interfaces.py Tue Aug 31 00:35:37 2010 @@ -1,22 +1,24 @@ from zope.interface import Interface, Attribute from zope import schema +from zope.component.interfaces import IObjectEvent from zope.pagetemplate.interfaces import IPageTemplate -from z3c.form.interfaces import IForm +from z3c.form.interfaces import IForm, IButtons, IButtonHandlers + class IFormWrapper(Interface): """Form wrapper class. - + This class allows "two-step" rendering, with an outer view rendering part of the page and the form class rendering the form area. - + In Zope < 2.12, this is the only way to get z3c.form support, because the view class takes care of the acquisition requirement. - + In Zope 2.12 and later, this approach is optional: you may register the form class directly as a browser view. """ - + def update(): """We use the content provider update/render couple. """ @@ -24,16 +26,16 @@ def render(): """We use the content provider update/render couple. """ - + form = Attribute("The form class. Should be set at class level") - + form_instance = schema.Object( title = u"Instance of the form being rendered", description = u"Set by the wrapper code during __init__()", readonly = True, schema = IForm ) - + index = schema.Object( title = u"Page template instance", description = (u"If not set, a template will be found " @@ -42,35 +44,56 @@ schema = IPageTemplate ) + class IWrappedForm(Interface): """Marker interface applied to wrapped forms during rendering. - + This allows different handling of templates, for example. """ """Custom Button and Handler override related interfaces """ -class IButtonAndHandler( Interface ): + +class IButtonAndHandlerSubscriber(Interface): """Button and Handler object to contain custom buttons and handlers to be injected into the form when a updateActions() is called """ - - position = schema.Int( title=(u"Requested positon in button ordering;" - u"-1 appends"), - required=False, - default=-1) + + originalButtons = schema.Object( + title=(u'Buttons'), + description=(u'Original button manager describing the buttons to be ' + u'used for the form.'), + schema=IButtons) + + originalHandlers = schema.Object( + title=(u'Buttons'), + description=(u'Original handler manager describing the handlers to be ' + u'used for the form.'), + schema=IButtonHandlers) + + position = schema.Int( + title=(u"Requested positon in button ordering;" + u"-1 appends"), + required=False, + default=-1) def __init__(self, form, event): - """The button and handler is instantiated using the form it represents, + """The button and handler is instantiated using the form it represents, and the context interface. """ - + def buttonHandler(self, action): """Custom button handler for the button. Can access form using self.form """ - + def updateActions(self): """Custom actions applied after the super actions are updated in the main form. """ + + +# Event interfaces +class IButtonOverrideEvent(IObjectEvent): + """Marker interface for event signalling + """ |