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.
Showing posts with label tools. Show all posts
Showing posts with label tools. Show all posts

Monday, June 11, 2007

How to build an action provider

Almost every link of CPS that let's you do a change on a document is called an action in CMF's jargon.

This post will deal with how to add a tab to the tool portal_imessages so that it allows to define its own actions. Let's start:

  1. Declare our tool as action provider:


    from Products.CMFCore.ActionProviderBase import ActionProviderBase
    ...
    ...
    class MessagesTool(SimpleItem, PropertyManager, ActionProviderBase):


  2. Declare the “Actions” tab by creating adding a new item to the attribute manage_options.


    manage_options = (
    PropertyManager.manage_options
    + SimpleItem.manage_options
    + ActionProviderBase.manage_options
    )


  3. Create the profile specification of the action provider with the list of actions. Add these lines to the actions.xml file.



    <action-provider name=”portal_imessages”>
    <action title="action_send_msg"
    action_id="action_send_msg" category="object"
    condition_expr="python:not portal.portal_membership.isAnonymousUser()"
    url_expr="string:${portal/portal_url}/send_message" visible="True">
    <permission>Modify portal content</permission>
    </action>
    </action-provider>


  4. Add the step actions to import_steps.xml.



    <import-step id="actions" version="20040630-01"
    handler="Products.CMFCore.exportimport.actions.importActionProviders"
    title="Action Providers">
    <dependency step="toolset"/>
    Import actions tool's action providers and their actions.
    </import-step>


    Don't relax!! It's almost done...

  5. Restart Zope's instance

  6. Go to portal_setup tool and import the profile CPS Messages

  7. Go to the tool portal_imessages and you should see the tab Actions.


I'll be waiting for your comments of success... ;-)

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! ;-)

Wednesday, June 6, 2007

How to create a tool

  1. Create a file MessagesTool.py in the folder CPSMessages like this:


    # Python modules
    import logging

    # Third-party modules
    from OFS.SimpleItem import SimpleItem

    # This product's modules

    # Global variables

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

    class MessagesTool(SimpleItem):
    ”””
    ”””
    id = "portal_imessages"
    meta_type = "MessagesTool"



  2. Register the tool in the file __init__.py of the product by adding the import of the class ToolInit and the ToolInit method to initialize procedure.



    from Products.CMFCore.utils import ToolInit

    import MessagesTool

    def initialize(context):
    """
    """

    ToolInit(
    'Messages Tool',
    tools=(MessagesTool.MessagesTool,),
    icon='tool.png',).initialize(context)



    The class ToolInit needs the name of the new tool, a list of tools to register and the filename of an icon.

  3. Copy the icon called tool.png from any other product or create your own, it can be a .gif too.


  4. Add a new step in the file import_steps.xml (path CPSMessages/profiles/default) to import tools. The new file looks like this:


    <?xml version="1.0"?>
    <import-steps>
    <import-step id="toolset" version="20040630-01"
    handler="Products.GenericSetup.tool.importToolset"
    title="Required tools">
    Create required tools, replacing any of the wrong class, and remove
    forbidden ones.
    </import-step>
    </import-steps>



  5. Create a new file toolset.xml (path CPSMessages/profiles/default)


    <?xml version="1.0"?>
    <tool-setup>
    <required tool_id="portal_imessages"
    class="Products.CPSMessages.MessagesTool.MessagesTool"/>
    </tool-setup>


  6. Restart Zope and import the profile CPS Messages from the ZMI in the tool portal_setup. The new tool portal_imessages should be available from the root of the CPS instance.

Note: The identifier of the tool is portal_imessages and not portal_messages due to a conflict of names. CPSSkins uses a tool called portal_messages for internationalization stuff.

Did you enjoy this post? Leave a comment please, even if you didn't enjoyed! Thanks ;-)