Enable behaviors per content type instance.
Project description
collective.instancebehavior
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. It does 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 addon 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,] ... )
Ideas
References
http://opkode.net/media/blog/plone-and-dexterity-enable-behaviors-per-content-type-instance
https://gist.github.com/thet/3990769
from IRC
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...
Support
We appreciate any contribution and if a release is needed to be done on pypi, please just contact one of the contributors. Some of us also offer commercial support if any training, coaching, integration or adaptions are needed.
If you are having issues, please let us know.
Source Code
The sources are in a GIT DVCS with its main branches at github. There you can report issue too.
We’d be happy to see many forks and pull-requests to make this addon even better.
This package uses the black code style.
Contributors
JC Brand
Johannes Raggam
Robert Niederreiter
Peter Holzer
Jens Klein
License
The project is licensed under the GPLv2.
Changelog
0.5
Invalidate global schema cache [jensens]
Black code style, isort imports, overall housekeeping [jensens]
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
Hashes for collective.instancebehavior-0.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6332c1d3be63c6ec315349e54c2c256a9aa637739bce9cbb59374494a9fa4e29 |
|
MD5 | a0c6b119ebb0996ac5438741a7641dec |
|
BLAKE2b-256 | a49b6c512c9ea74b1e7e53a80157f5addb9a5da085deacbdd355f5b84065b11f |