Package: mfabrik.behaviorutilities
ContentsIntroductionAbout Behavior patternFeaturesExample productsSample code walkthroughAuthorChangelog0.1 - 0.1.10.1Introductionmfabrik.behaviorutilities is plone.behavior support and helper package for Plone 3 and Archetypes.About Behavior patternBehavior pattern is a way to provide extensible behavior for content objects,
without touching the actual content object class source code and
so that behaviors can be turned on and off.First read plone.behavior tutorialhttp://plone.org/products/dexterity/documentation/manual/behaviorsNote that the tutorial does not describe how to use behavior objects
with Archetypes based content and this Python module tries to fill
in some gaps there.FeaturesCreate plone.behavior behaviors for Archetypes objects and make them assignableOn-demand behavior object creation - do not make saves or Zope transactions
if behavior defaults are not changedz3c.form support to make behavior objects context aware and thus selection widget vocabulary resolving workingTraversing toolsExample productsThe following Plone add-ons have been created based on this codehttps://svn.plone.org/svn/collective/mfabrik.trafficsigns/trunk/mfabrik/trafficsigns/behaviors.pyhttp://plonegomobile.googlecode.com/svn/trunk/gomobile/gomobile.mobile/gomobile/mobile/behaviors.pyhttp://plonegomobile.googlecode.com/svn/trunk/gomobile/gomobile.convergence/gomobile/convergence/behaviors.pyhttp://svn.plone.org/svn/collective/plone.app.headeranimation/trunk/README.txtSample code walkthroughNote: Code here is only for example purposes and probably does not work as is.
Proper usage documentation will be done after the framework has taken more shape.
Refer to existing code users for more help.Behavior is defined as an interface, which also defines form options
which user can edit for this behavior. Schema is defined usingplone.directives.formpackage:class IMultiChannelBehavior(form.Schema):
    """ How content and its children react to differt medias """

    contentMedias = schema.Choice(title=u"Content medias",
                                  description=u"Does this content appear on web, mobile or both",
                                  default=ContentMediaOption.USE_PARENT,
                                  required=True)

     # More form fields here

alsoProvides(IMultiChannelBehavior, form.IFormFieldProvider)The behavior implementation is persistent Zope object,
which knowns itscontexti.e. object for which behavior is assigned
by using mfabrik.beahviorutilities.volatilecontext.VolatileContext base class,
which is a subclass of Zope Persistent class.Implementation maps behavior interface fields itself as
class attributes using FieldProperties.We use AnnotationPersistentFactory to store behavior.
This means that when behavior is once saved on your content object,
you can access by directly by traversing:context.__annotations__["your_annotation_key_name"]Example:class MobileBehaviorStorage(VolatileContext):

    # Implement your behavior
    implements(IMobileBehavior)

    mobileFolderListing = FieldProperty(IMobileBehavior["mobileFolderListing"])

    appearInFolderListing = FieldProperty(IMobileBehavior["appearInFolderListing"])


# This defines a behavior factoty method
mobile_behavior_factory = AnnotationPersistentFactory(MobileBehaviorStorage, "mobile")Now you can create and query behaviors.First we check that the behavior is assignable. Currently it is hardcoded
that all behaviors are assignable to all Archetypes content objects:self.loginAsPortalOwner()
self.portal.invokeFactory("Document", "doc")
doc = self.portal.doc

# Check assignable works
from plone.behavior.interfaces import IBehaviorAssignable
assignable = IBehaviorAssignable(doc, None)

self.assertTrue(assignable.supports(IMobileBehavior))
self.assertNotEqual(assignable, None)When we query the behavior it is created on the fly if it does
not already exist on the content. If the behavior is created,
then its attributes are populated with the default values specific in the schema:behavior = IMobileBehavior(doc)Behavior knowns on which content it belongs.
This is implemented as volatile reference, so no circular
pointers are stored to ZODB.doc == behavior.contextYou can edit the behavior parameters by using properties defined
on the storage class:behavior.mobileFolderListing = TrueIf you do any changes to the behavior you need to call save()
method of the VolatileContext class. This makes sure that
if the behavior is not the default behavior, you need to
actually save persistent parameters in the annotations:behavior.save()

# Recreate behavior from the scratch
# and see it is persistent
behavior = IMobileBehavior(doc)
assert behavior.behavior.mobileFolderListing == TrueEach behavior also needs edit form - you can easily do this using z3c.form:class MobileForm(z3c.form.form.EditForm):
     """ Folder/page specific mobile publishing options """

     fields = field.Fields(IMobileBehavior)

     prefix = "mobile"
     label = u"Mobile navigation options"

     def update(self):
         return z3c.form.form.EditForm.update(self)

     def getContent(self):
         """
         Return the object which the form should edit.
         """
         behavior = IMobileBehavior(self.context)
         return behavior

     def applyChanges(self, data):
         # Call super
         content = self.getContent()
         val = z3c.form.form.EditForm.applyChanges(self, data)

         # Write behavior to database
         content = self.getContent()
         content.save()

         return val

MobileFormView = wrap_form(MobileForm)It is easiest to link this form to your object using document_actions
link. actions.xml snippet:<?xml version="1.0"?>
<object name="portal_actions" meta_type="Plone Actions Tool"
   xmlns:i18n="http://xml.zope.org/namespaces/i18n">

 <object name="document_actions" meta_type="CMF Action Category">

  <object name="mobile_options" meta_type="CMF Action" i18n:domain="plone">
   <property name="title" i18n:translate="">Mobile settings</property>
   <property name="description"
      i18n:translate="">Set mobile publishing options</property>
   <property
      name="url_expr">string:$object_url/@@mobile_options</property>
   <property name="icon_expr"></property>
   <property name="available_expr"></property>
   <property name="permissions">
    <element value="Modify portal content"/>
   </property>
   <property name="visible">True</property>
  </object>

 </object>

</object>AuthormFabrik Research Oy- Python and Plone professionals for hire.mFabrik web sitemFabrik mobile siteBlogMore about PloneChangelog0.1 - 0.1.1plone.org releaseUpdated README0.1Initial release