Developing a Plone Product Part 2

Next I want to add a Portlet which will display any items of the new content type which are in the current folder.

Creating a Plone Portlet

First step seems easy:

cd src/anorakgirl.mycontent
paster addcontent portlet

and then answer the easy questions. This creates a some files in the portlets folder of the product. However, when I restart Plone, I get this error:

ZopeXMLConfigurationError: File 
"c:\plone\src\anorakgirl.mycontent\anorakgirl\mycontent\portlets\configure.zcml", line 12.2-21.8]
ImportError: cannot import name QuotesPortletMessageFactory

I fixed this by commenting out the line below from portlets/mycontentportlet.py – who knows if this matters!

#from anorakgirl.mycontent.portlets import MyContentPortletMessageFactory as _

Plone now restarts, and I can add the MyContentPortlet via ‘Manage Portlets’. However, it doesn’t do anything yet.

To make it show the first ‘MyContent’ item from the current folder, I modified portlets/mycontentportlet.py as follows. Please note that this code is shamelessly copied from Professional Plone Development by Martin Aspeli.

from zope import schema
from zope.component import getMultiAdapter
from zope.formlib import form
from zope.interface import implements
from plone.app.portlets.portlets import base
from plone.memoize.instance import memoize
from plone.portlets.interfaces import IPortletDataProvider
from DateTime import DateTime
from Acquisition import aq_inner
from Products.CMFCore.interfaces import ICatalogTool
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.CMFCore.utils import getToolByName

from anorakgirl.mycontent.interfaces import IMyContent

#from anorakgirl.mycontent.portlets import MyContentPortletMessageFactory as _

class IMyContentPortlet(IPortletDataProvider):
    """A portlet

    It inherits from IPortletDataProvider because for this portlet, the
    data that is being rendered and the portlet assignment itself are the
    same.
    """

class Assignment(base.Assignment):
    """Portlet assignment.

    This is what is actually managed through the portlets UI and associated
    with columns.
    """

    implements(IMyContentPortlet)

    def __init__(self):
        pass

    @property
    def title(self):
        """This property is used to give the title of the portlet in the
        "manage portlets" screen.
        """
        return "MyContent Portlet"

class Renderer(base.Renderer):
	"""Portlet renderer.

	This is registered in configure.zcml. The referenced page template is
	rendered, and the implicit variable 'view' will refer to an instance
	of this class. Other methods can be added and referenced in the template.
	"""

	render = ViewPageTemplateFile('mycontentportlet.pt')

	@property
	def available(self):
		return len(self._data()) > 0

	def mycontent_list(self):
		for brain in self._data():
			mycontent = brain.getObject()

			yield dict(title=mycontent.title,
				description=mycontent.description,
				author=mycontent.author,
				details=mycontent.details)

	@memoize
	def _data(self):
		context = aq_inner(self.context)
		limit = 1
		query = dict(object_provides = IMyContent.__identifier__)
		if self.context.isPrincipiaFolderish:
			query['path'] = '/'.join(context.getPhysicalPath())

		if not self.context.isPrincipiaFolderish:
			parent = context.aq_parent
			query['path'] = '/'.join(parent.getPhysicalPath())
		catalog = getToolByName(context, 'portal_catalog')
		results = catalog(query)
		mycontents = []

		mycontents = results[:limit]
		return mycontents

# NOTE: If this portlet does not have any configurable parameters, you can
# inherit from NullAddForm and remove the form_fields variable.

class AddForm(base.AddForm):
    """Portlet add form.

    This is registered in configure.zcml. The form_fields variable tells
    zope.formlib which fields to display. The create() method actually
    constructs the assignment that is being added.
    """
    form_fields = form.Fields(IMyContentPortlet)

    def create(self, data):
        return Assignment(**data)

# NOTE: IF this portlet does not have any configurable parameters, you can
# remove this class definition and delete the editview attribute from the
# <plone:portlet /> registration in configure.zcml

class EditForm(base.EditForm):
    """Portlet edit form.

    This is registered with configure.zcml. The form_fields variable tells
    zope.formlib which fields to display.
    """
    form_fields = form.Fields(IMyContentPortlet)

Really all that I changed from the paster generated content was the imports and the Render method.

I also amended the template file mycontentportlet.pt to show the data returned by the Render method:

<dl class="portlet portletMyContentPortlet"
    i18n:domain="anorakgirl.mycontent.portlets">
    <dd class="portletItem odd">

        <div tal:define="items view/mycontent_list">
             <ul>
                 <li tal:repeat="item items">
                 <span tal:content="item/details"/>
                 <br/><b tal:content="item/author"></b></li>
             </ul>
            </div>
    </dd>
</dl>

Because paster amended the xml files in the profiles folder ( to register the new portlet) not only do you need to restart Plone, you need to reinstall the product to get the Portlet to show up. I’m doing this using the Setup > Add-on products page, but this may not be the best way?

You should now be able to add the Portlet using Manage Portlets. If you add it at the root, and the rest of the site is set to inherit parent portlets, then the portlet should show up in any folder in which you add a ‘MyContent’ item.

    Developing a Plone product part 1

    I have to say, I am used to being able to pick up a new tool or environment and start digging behind the scenes without becoming too bewildered, but Plone has not been like that at all. I am very impressed at the front end, but there’s always something extra required. Python is new to me, and I am struggling with the terminology and so on.

    So, I am going to record my progress in creating a Plone product. I am currently using Plone 3.2.2, developing on Windows but will be deploying on Linux.

    Useful things I learned so far

    #Run plone in the foreground, so that you can see the output and restart easily

    C:\Plone>bin\plonectl.exe fg

    #If you change a .py or .zcml file, you need to restart Plone to see your changes

    #Changes to .pt files should show up immediately in foreground mode

    #Changes to .xml files mean the product must be uninstalled and reinstalled  (I think via the front end Setup > Add-on products page is fine)

    My planned product

    I want the site users (Editors/Contributers) to be able to add content into the right hand column, without giving them Manager rights. My plan is this:

    1. Create a new content type for this content.  This will simply have a few custom fields, and should not appear in navigation by default (this tutorial).

    2. Create a portlet which displays any items of the new content type in the current folder. The portlet will be added at root level and carry through the site, so users simply add the new right hand content where they want it. If the users want specific right hand content for a single page, they would put the page in its own folder. (see next tutorial)

    Getting Started

    I am using paster to create the skeleton of my product. This creates a directory structure with all the required files. It is easiest if paster is in your path.  So, in the src directory:

    paster create -t archetype anorakgirl.mycontent

    And answer the questions that follow:

    Enter title (The title of the project) ['Plone Example']: Anorakgirl MyContent Project
    Enter namespace_package (Namespace package (like plone)) ['plone']: anorakgirl
    Enter package (The package contained namespace package (like example)) ['example']: mycontent
    ...

    Use paster to add the actual contentype

    cd anorakgirl.mycontent
    paster addcontent contenttype

    Then use paster again to add some fields to the content type.

    paster addcontent atschema

    Again answer the questions. The module name in this case will be mycontent. Repeat the above to add whatever fields you need. Note that you get Title and Description for free, so don’t add those.

    You’ve now done enough to see the product in action. So

    Adding the product to the build environment

    Add the following lines to buildout.cfg

    [buildout]
    ...
    develop =
       src/anorakgirl.mycontent
    [instance]
    ...
    eggs =  
       ...
       anorakgirl.mycontent
    zcml =
       ...
       anorakgirl.mycontent

    Run buildout.

    Start plone (in foreground mode).

    Login as an Administrator. Go to Site Setup > Add-on Products. The product should be listed under products to install. Install it.

    Your new content type should appear on the Add New menu. The edit form should have all the fields you added. The standard view won’t be very pretty but that’s a job for another tutorial…

    Next Time: creating a Plone portlet to display the new Content Type…