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.

Tuesday, June 5, 2007

How to get the type of an object

CPS is a content management system and as of that, it is very common when coding that you need to get the type of an object.

Let's throw a light on this subjet.

Firstly, in the Zope/CPS architecture there is not only one type associated to an object but, to say it someway, one for each level of abstraction. In fact there are three types for each object!! Don't hesitate, let's explain that...

  • Meta type: represents the type of the object in Zope's level and is the equivalent to the class from which the object is instance of. The name of this type is shown in ZMI as [Name of class] at [relative URL] in the upper left corner of the main frame.
    To work on an example, create a python script from the ZMI in the folder custom of portal_skins and write this code in it:

    proxy = context.workspaces
    print proxy
    return printed

    The result should be:

    <ProxyFolder at workspaces>

    And.. you got it! The meta type of the object workspaces is ProxyFolder.

  • As you know, each content of CPS is a proxy which points to the real document that is stored in portal_repository. As it couldn't be any other way, in CPS there are several types of proxies. The Proxy Type can be: folder, document, folderishdocument, btreefolder, btreefolderishdocument.

    Each of these types are instances of Factory-based Type Information and can be seen from ZMI in portal_types.
    Let's improve the example above:

    proxy = context.workspaces
    print "Meta type: ", proxy
    print "Proxy type: ", proxy.getTypeInfo().cps_proxy_type
    return printed

    And the results looks like this:

    Meta type: <ProxyFolder at workspaces>
    Proxy type: folder

    You can see the Proxy Type as the CMF level type.

  • Finally, the most ever wanted type: Portal Type. This is the type that the final user of the portal deals with. Some examples are News Item, Document, Blog, Forum, etc.

    Whenever you need to retrieve the types of an object, use this snippet:

    proxy = context.workspaces
    print "Meta type: ", proxy
    print "Proxy type: ", proxy.getTypeInfo().cps_proxy_type
    print "Portal type: ", proxy.getTypeInfo().getId()
    return printed

    And, the result, as you may guess:

    Meta type: <proxyfolder>
    Proxy type: folder
    Portal type: Workspace

    The portal type is the more useful when developing on CPS, but don't forget that not all objects have this type as not all objects in the ZODB are proxies (e.g. a Zope Page Template).

    Of course, the portal type is the CPS level type for an object.

I hope your tasks are a little bit easier now ;-)

No comments: