At the Forge - CMF Types
The easiest way to create a new CMF type is to base it on an existing type with the Web-based CMF type creation tool. This method does not allow you to modify the fields or methods associated with a type, but it does let you change the permissions associated with the type's actions, whether the type can be discussed and even the way in which this data type is displayed.
For example, go to the portal_types tool and choose Factory-based type information from the Select type to add... menu in the top-right corner. You are prompted for two pieces of information, the ID or name of the new type and the existing type on which it should be based. We are creating the ATFDocument, which means we are basing ourselves on CMF Default: Document.
Once you create the new type, it is available and visible from all of the type listings, including the types tool and the contents view in which you create a new instance of a type. Indeed, anyone with administrative privileges on the portal can now see your new ATFDocument type in the menu of options from which they can choose a new type to create.
What's the point of doing this, if ATFDocument and Document are the same? Well, they're not exactly the same; rather, they share methods and an overall class definition. Other information about this type, such as properties, permissions and skins, default to be the same as Document, but they can be made to look quite different. This means that if you want instances of Document to be displayed in black-on-white text without discussions and ATFDocument to be displayed in yellow-on-maroon text with discussions, you can do that quickly and easily with this method. And, if and when you upgrade your copy of CMF, ATFDocument will be updated automatically, along with Document.
Of course, there will be times when you want to create a type that has fields or behavior significantly different from an existing type. Several options exist for doing this, but the most flexible (and challenging and poorly documented) method is to create a new Zope product that adheres to CMF rules. For example, all Python packages must contain an __init__.py file in the package's root directory. This file may be empty, or it may contain statements that are evaluated when the package is first loaded into memory. In the case of a product, __init__.py is where the class is first registered into Zope by use of the initialize() method, which takes a single argument commonly called context. A bare-bones Zope product thus has an __init__.py that looks something like the following mythical MyProduct:
import MyProduct def initialize(context): context.registerClass( MyProduct.MyProduct, constructors=(MyProduct.manage_addMyProductForm, MyProduct.manage_addMyProduct) )
When Zope starts up, it looks through the products and invokes the initialize() method with an appropriate context. Context is part of Zope's system of acquisition, in which an object's attributes are defined by its location in the hierarchy as well as by its class definition. In the above example, MyProduct registers itself with two constructors, the methods manage_addMyProductForm and manage_addMyProduct.
A CMF type must register itself not only with Zope but also with CMF, so it can appear in the various CMF tools. Our product's initialize() method thus needs to include CMF-specific registration, which means that __init__.py needs to import modules from CMF. Moreover, every type in CMF must register itself with one of the CMF-specific initialization routines in Products.CMFCore.utils. For example, __init__.py from CMFDefault, which comes with CMF, first defines the different classes it will register:
contentClasses = ( Document.Document , File.File , Image.Image , Link.Link , Favorite.Favorite , NewsItem.NewsItem , SkinnedFolder.SkinnedFolder )
It then defines the constructor for each of the classes:
contentConstructors = \ ( Document.addDocument , File.addFile , Image.addImage , Link.addLink , Favorite.addFavorite , NewsItem.addNewsItem , SkinnedFolder.addSkinnedFolder )
And, of course, every type can have its own specific tool:
tools = ( DiscussionTool.DiscussionTool , MembershipTool.MembershipTool , RegistrationTool.RegistrationTool , PropertiesTool.PropertiesTool , URLTool.URLTool , MetadataTool.MetadataTool , SyndicationTool.SyndicationTool )
Finally, the initialize() method, abbreviated slightly here, within the package registers these classes using CMF with utils.ToolInit(), for tools, or ContentInit, for content. It then invokes initialize(context) on what it receives back, thus registering the new object with Zope:
def initialize( context ): utils.ToolInit('CMFDefault Tool', tools=tools, product_name='CMFDefault', icon='tool.gif', ).initialize( context ) utils.ContentInit( 'CMFDefault Content' , content_types=contentClasses , permission=AddPortalContent , extra_constructors=contentConstructors , fti=Portal.factory_type_information ).initialize( context ) context.registerClass(Portal.CMFSite, constructors=(Portal.manage_addCMFSiteForm, Portal.manage_addCMFSite, ))
- Transitioning to Python 3
- What is your favorite Linux distribution for use on the desktop?
- Tech Tip: Really Simple HTTP Server with Python
- Red Hat OpenStack Platform
- OpenSSL Hacks
- FutureVault Inc.'s FutureVault
- Stepping into Science
- Linux Journal December 2016
- Radio Free Linux
- A Better Raspberry Pi Streaming Solution