Skip to main content

Enable behaviors per content type instance.

Project description

collective.instancebehavior

References

  • http://opkode.net/media/blog/plone-and-dexterity-enable-behaviors-per-content-type-instance

  • https://gist.github.com/thet/3990769

Problem and Motivation

Behaviors, provided by the plone.behavior package, provide a very useful way of extending the functionality of a Dexterity content type. Unfortunately, the fact that a content type’s behaviors are stored in its factory contents information (FTI) inside the portal_types tool, means that they are class (or type) specific, and not instance specific. This means that behaviors can only be enabled globally, in other words only for all the instances of a specific type.

To enable per instance behaviors, we need a way to store those behaviors on the instance. We use annotations for this purpose.

Additionally We provide our own IBehaviorAssignable adapter that overrides the default one to not only look for an object’s FTI registered annotations, but also look for the behaviors stored in the current instance’s annotations.

Also helper functions are implemented which can be used to dynamically assing behaviors and interfaces on dexterity content objects.

Installation

Depend your plone product on collective.instancebehavior.

Enable behaviors and interfaces

Note: the targeted object must implement collective.instancebehavior.IInstanceBehaviorAssignableContent.

>>> from plone.app.event.dx.interfaces import IDXEvent
>>> enable_behaviors(obj, ['plone.app.event.dx.behaviors.IEventBasic',],
...                       [IDXEvent,])

Disable behaviors and interfaces

>>> from plone.app.event.dx.interfaces import IDXEvent
>>> disable_behaviors(obj, ['plone.app.event.dx.behaviors.IEventBasic',],
...                        [IDXEvent,])

TODO

12:13 < rnix> datakurre: https://github.com/bluedynamics/bda.plone.productshop/blob/master/bda/plone/productshop/browser/forms.py
12:13 < rnix> datakurre: woring instance behavior enabled add form
12:21 < rnix> datakurre: may be generalized to collective.instancebehavior -> just need a generic way on telling DX to use the correct form. -> idea would be to have a
              behavior which can be bound to folderish objects and this defined default behaviors applied dynamically
12:21 < rnix> to it's direct children
12:22 < rnix> but i do not really like to register this add view for every type.. would be more appropriate if we found a way on
12:22 < rnix> deciding by applied container interface
12:22 < rnix> something like that...
12:26 < datakurre> rnix: What the differences for original add form? 1) show fields for default instance behaviors 2) enable default instance behaviors on save?
12:28 < datakurre> rnix: 2) Could probably be done by instancebehavior-adapter (add form would call it, when instancebehavior-behavior is enabled)
12:28 < datakurre> rnix: 1) migth be possible with plone.z3cform's FormExtender
12:30 < rnix> datakurre: i was thinking of an IChildDefaultInstanceBehaviors behavior, which reads the available IInstanceBehavior deriving definitions. and sets them to
              all added children by default
12:30 < rnix> the add view can then be bound to IChildDefaultInstanceBehaviors
12:31 < datakurre> rnix: Well, that works for sure.
12:32 < rnix> datakurre: instead of binding the add view by name to Products.CMFCore.interfaces.IFolderish, we bind it to IChildDefaultInstanceBehaviors without name...

Contributors

  • JC Brand

  • Johannes Raggam

  • Robert Niederreiter

  • Peter Holzer

Changelog

0.4

  • Be more explicit on dependencies and other egg improvements [tomgross]

0.3

  • Fix DexterityInstanceBehaviorAssignable.enumerateBehaviors in order to work with plone.dexterity >= 2.3.0 [agitator]

0.2

  • convert behaviors in enable_behaviors to tuple before concat. [rnix]

  • check whether instance behavior already in instance_behaviors before adding. [rnix]

0.1

  • created collective.instancebehavior based on jcbrand’s and thet’s work [rnix]

  • improved implementation with generic helper functions [thet]

  • problem description and initial implementation [jcbrand]

License

Copyright (c) 2013, JC Brand, Johannes Raggam, Robert Niederreiter All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the BlueDynamics Alliance nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY BlueDynamics Alliance AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BlueDynamics Alliance BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

collective.instancebehavior-0.4.tar.gz (15.3 kB view details)

Uploaded Source

File details

Details for the file collective.instancebehavior-0.4.tar.gz.

File metadata

File hashes

Hashes for collective.instancebehavior-0.4.tar.gz
Algorithm Hash digest
SHA256 f580ef18f7d87264012e2baca2cb71b90b4160210e5d89f19905008b989ebd39
MD5 759bc9029974558b3f692819af120df8
BLAKE2b-256 c2ccddb0c6566064710de2126293801fa8fe0eab46c3c8f7f4fac5e9c1fe20c9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page