Did not find a how-to for your problem?

Ask me to write the how-to post by writing to the mailing-list at cps-devel [@] lists [.] nuxeo [.] com or writing to me to joseluisdelarosa+cpshowto [@] gmail [.] com. Note: I'll keep all conversations on the official mailing list.

Thursday, June 7, 2007

How to add properties to a tool

This post will explain how to add properties to the tool portal_imessages.

  1. Modify the module of the tool to import the module PropertyManager. Remember that the path of the file is Products/CPSMessages/MessagesTool.py.



    # Python modules
    import logging

    # Third-party modules
    from OFS.SimpleItem import SimpleItem
    from OFS.PropertyManager import PropertyManager

    # This product's modules

    # Global variables

    logger = logging.getLogger('CPSMessages.MessagesTool')

    class MessagesTool(SimpleItem):
    ”””
    This class implements the most usual tasks to manage internal messages.
    ”””
    id = "portal_imessages"
    meta_type = "MessagesTool"





  2. Set the attribute manage_options to be the inheritance of each of the superclasses of the class MessagesTool.


    # Python modules
    import logging

    # Third-party modules
    from OFS.SimpleItem import SimpleItem
    from OFS.PropertyManager import PropertyManager

    # This product's modules

    # Global variables

    logger = logging.getLogger('CPSMessages.MessagesTool')

    class MessagesTool(SimpleItem):
    ”””
    This class implements the most usual tasks to manage internal messages.
    ”””
    id = "portal_imessages"
    meta_type = "MessagesTool"

    ## This class attribute defines the list of tabs available to manage the tool from the ZMI.
    manage_options = (SimpleItem.manage_options + PropertyManager.manage_options)



  3. In zope 2 each property is declared as a dictionary that has entries with keys: identifier, type, mode, label, select_variable and some more that I don't even know. Let's add a property that allows to disable or enable the tool activity whenever the administrator wants.


    # Python modules
    import logging

    # Third-party modules
    from OFS.SimpleItem import SimpleItem
    from OFS.PropertyManager import PropertyManager

    # This product's modules

    # Global variables

    logger = logging.getLogger('CPSMessages.MessagesTool')

    class MessagesTool(SimpleItem):
    ”””
    This class implements the most usual tasks to manage internal messages.
    ”””
    id = "portal_imessages"
    meta_type = "MessagesTool"

    ## This class attribute defines the list of tabs available to manage the tool from the ZMI.
    manage_options = (SimpleItem.manage_options + PropertyManager.manage_options)

    ## This class attribute defines a list of properties shown in the ZMI
    _properties = (
    {'id':'mbox_identifier_prefix', # Identifier
    'type':'string', # Type of property
    'mode':'w', # Mode: Writable (w), Read Only (r)
    'label':'Message box identifier' # Label to show in the ZMI form.
    },
    )

    For this type of property, select_variable is not needed but it is useful when a closed list of values exists for the property. An example of usage can be found in the class TypeInformation (product CMFCore module TypesTool) for the property labeled “Allowed Content Types” which will be familiar to you if you have already dealt with portal types.

  4. Set the value for the new property.



    # Python modules
    import logging

    # Third-party modules
    from OFS.SimpleItem import SimpleItem
    from OFS.PropertyManager import PropertyManager

    # This product's modules

    # Global variables

    logger = logging.getLogger('CPSMessages.MessagesTool')

    class MessagesTool(SimpleItem):
    ”””
    This class implements the most usual tasks to manage internal messages.
    ”””
    id = "portal_imessages"
    meta_type = "MessagesTool"

    ## This class attribute defines the list of tabs available to manage the tool from the ZMI.
    manage_options = (
    SimpleItem.manage_options
    + PropertyManager.manage_options
    )

    ## This class attribute defines a list of properties shown in the ZMI
    _properties = (
    {'id':'mbox_identifier_prefix', # Identifier
    'type':'string', # Type of property
    'mode':'w', # Mode: Writable (w), Read Only (r)
    'label':'Message box identifier' # Label to show in the ZMI form.
    },
    )

    ## This class attribute will be used to know if the tool is enabled or not in the site.
    mbox_identifier_prefix = 'imessagesbox-'


  5. Restart the Zope's instance and go to the tab Properties of portal_imessages and you should see the new property with its default value set.

Good luck! ;-)

1 comment:

Guillaume GB said...

just missing this :
class MessagesTool( PropertyManager, SimpleItem ):