Warning

This document hasn't been checked for compatibility with current versions of Plone. Use at your own risk.

Installation script snippets

by Mikko Ohtamaa last modified Dec 06, 2009 09:27 PM
Quick code snippets to use in quick installer installation scripts.

To make some running code for Plone you need to register it in various places. Here are some examples how you can make your product available in Plone programmatically, without manually hazzling with various pages of Zope Management Interface.

General guidelines

  • For each Zope class, remember to call Zope method Globals.InitializeClass
  • For each Archetypes class, remember to call Products.Archetypes.public.registerClass
  • Initialize Archetype types and tools in Product's __init__.py script
  • Register types, skins and tools in Extension/install.py quick installer script

Installing Archetypes types and tools

Here are code snippets how to register Archetypes types, registering tools, tuning Archetypes item creation permissions, register custom TAL scripts, allow TAL scripts to call functions in the product.

YourProductDir/Extensions/install.py / def install(self):

    from Products.Usability.config import * 

    # Register Archetypes types in the portal
    installTypes(self, out, listTypes(PROJECTNAME), PROJECTNAME)

    # Install UsabilityTool tool             
    if not hasattr(portal, UsabilityTool.id):
        portal.manage_addProduct[PROJECTNAME].manage_addTool(UsabilityTool.meta_type)
        #manage_addTool(m, "Usability Management Tool")
        print >> out, 'tool installed'

    # Register layout files in the portal
    install_subskin(self, out, GLOBALS)      

YourProductDir/__init__.py:

    from Products.Archetypes.public import process_types, listTypes
    from Products.Archetypes.ArchetypeTool import getType
    from Products.Archetypes.utils import capitalize
    from Products.CMFCore import utils
    from Products.CMFCore.DirectoryView import registerDirectory
    from AccessControl import allow_module
    from AccessControl import ModuleSecurityInfo

    def initialize(context):
        """ Register types and tools of your product

        Also register functions which can be called from TAL scripts.

        Parameters:

        @context Zope/App/ProductContext instance

        """

        # Import Types here to register them
        import Application
        import ApplicationFolder
        import Issue
        import UsabilityTool

        # List Archetypes types in this product automatically
        content_types, constructors, ftis = process_types(
            listTypes(PROJECTNAME),
            PROJECTNAME)

        # Initialize creation factories for Archetypes types
        utils.ContentInit(
            PROJECTNAME + ' Content',
            content_types      = content_types,
            permission         = VIEW_USABILITY_ITEM_PERMISSION,
            extra_constructors = constructors,
            fti                = ftis,
            ).initialize(context)

        # Initialize Tools 
        tools = (UsabilityTool.UsabilityTool,)
        utils.ToolInit(UsabilityTool.UsabilityTool.meta_type, tools=tools,
                           product_name=PROJECTNAME, icon='tool.gif',
                           ).initialize(context)

        # Allow embedded TAL scripts to call functions in this module
        ModuleSecurityInfo("Products.Usability").declarePublic("isRequestView")
        ModuleSecurityInfo("Products.Usability").declarePublic("issueFieldVisibility")

        # Fine tune creation permissions 
        # and now give it some extra permissions so that i
        # can control them on a per class limit        
        for i in range(0,len(content_types)):
            perm='Add '+ capitalize(ftis[i]['id'])+'s'
            methname='add'+capitalize(ftis[i]['id'])
            meta_type = ftis[i]['meta_type']

            context.registerClass(
                meta_type=meta_type,
                constructors = (
                                getattr(locals()[meta_type],'add'+capitalize(meta_type)),
                                   )
                , permission = perm
                )

        # Register tuned creation permissions                                         
        context.registerClass(
                meta_type="Issue",
                constructors = (getattr(locals()["Issue"],'addIssue'),),
                permission = EDIT_USABILITY_ITEM_PERMISSION
                )    

        context.registerClass(
                meta_type="Application",
                constructors = (getattr(locals()["Application"],'addApplication'),),
                permission = EDIT_USABILITY_ITEM_PERMISSION
                )    

Manage roles and permissions in quick installer script:

        def addRole(portal, user, role):
            """ Modify user roles by directly calling acl_users """
            roles = user.getRoles() + (role,)
            portal.acl_users._doChangeUser(user.getUserName(), user.getPassword(), roles, user.getDomains())

        def addPermissionsForRole(context, role, permissions):
            """ Add (inheritable) permissions for a role for the context

            Parameters:
                @param context Portal object (portal itself, or Archetypes item, inherited from RoleManager)
                @param role role name, as a string
                @param permissions tuple of permissions (string names) to add for the role    
            """        
            existingPermissions = context.permissionsOfRole(role)
            newPermissions = existingPermissions
            newPermissions += permissions
            context.manage_role(role_to_manage=role, permissions=tuple(newPermissions))    

Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.