17. Write Your Own Add-Ons to Customize Plone#

In this part you will:

  • Create a custom Python package ploneconf.site to hold all the code

  • Modify buildout to install that package

Topics covered:

  • mr.bob and bobtemplates.plone

  • the structure of python packages

17.1. Creating the package#

Your own code has to be organized as a Python package. A python package is directory that follows certain conventions to hold python modules.

We are going to use bobtemplates.plone to create a skeleton package. You only need to fill in the blanks.

bobtemplates.plone offers several Plone-specific templates for mr.bob, a project template builder similar to cookiecutter.

Enter the src directory (src is short for sources) and call a script called mrbob from our buildout's bin directory:

$ cd src
$ ../bin/mrbob -O ploneconf.site bobtemplates.plone:addon

Warning

Before version 2.0.0 of bobtemplates.plone the command to create a add-on was different:

$ ../bin/mrbob -O ploneconf.site bobtemplates:plone_addon

You have to answer some questions about the add-on. Press Enter (i.e. choosing the default value) for most questions except where indicated (enter your GitHub username if you have one, do not initialize a GIT repository, Use Plone 5.2 and python 3.7):

--> Author's name [Philip Bauer]:

--> Author's email [bauer@starzel.de]:

--> Author's GitHub username: pbauer

--> Package description [An add-on for Plone]:

--> Do you want me to initialize a GIT repository in your new package? (y/n) [y]: n

--> Plone version [5.1]: 5.2

--> Python version for virtualenv [python2.7]: python3.7

git init is disabled!
Generated file structure at /Users/pbauer/workspace/training_buildout/src/ploneconf.site

If this is your first python package, this is a very special moment.

You generated a package with a lot files. It might look like too much boilerplate but all files in this package serve a clear purpose and it will take some time to learn about the meaning of each of them.

17.2. Eggs#

When a python package is production-ready you can choose to distribute it as an egg over the python package index, pypi. This allows everyone to install and use your package without having to download the code from github. The over 270 python packages that are used by your current Plone instance are also distributed as eggs.

17.3. Inspecting the package#

In src there is now a new folder ploneconf.site and in there is the new package. Let's have a look at some of the files:

buildout.cfg, .travis.yml, .coveragerc, requirements.txt, MANIFEST.in, .gitignores, .gitattributes,

You can ignore these files for now. They are here to create a buildout only for this package to make distributing and testing it easier.

README.rst, CHANGES.rst, CONTRIBUTORS.rst, DEVELOP.rst, docs/

The documentation of your package goes in here.

setup.py

This file configures the package, its name, dependencies and some metadata like the author's name and email address. The dependencies listed here are automatically downloaded when running buildout.

src/ploneconf/site/

The python code of your package itself lives inside a special folder structure. That seems confusing but is necessary for good testability. Our package contains a namespace package called ploneconf.site and because of this there is a folder ploneconf with a __init__.py and in there another folder site and in there finally is our code. From the buildout's perspective your code is in your buildout directory/src/ploneconf.site/src/ploneconf/site/real code

Note

Unless discussing the buildout we will from now on silently omit these folders when describing files and assume that your buildout directory/src/ploneconf.site/src/ploneconf/site/ is the root of our package!

configure.zcml (src/ploneconf/site/configure.zcml)

The phone book of the distribution. By reading it you can find out which functionality is registered using the component architecture. There are more registrations in other zcml-files in this add-ons (e.g. browser/configure.zcml, upgrades.zcml and permissions.zcml) that are included in your main configure.zcml

setuphandlers.py (src/ploneconf/site/setuphandlers.py)

This holds code that is automatically run when installing and uninstalling our add-on.

interfaces.py (src/ploneconf/site/interfaces.py)

Here a browserlayer is defined in a straightforward python class. We will need it later.

testing.py

This holds the setup for running tests.

tests/

This holds the tests.

browser/

This directory is a python package (because it has a __init__.py) and will by convention hold most things that are visible in the browser.

browser/configure.zcml

The phonebook of the browser package. Here views, resources and overrides are registered.

browser/overrides/

This folder is here to allow overriding existing default Plone templates.

browser/static/

A directory that holds static resources (images/css/js). The files in here will be accessible through URLs like ++resource++ploneconf.site/myawesome.css

locales/

This directory can hold translations of text used in the package to allow for multiple languages of your user-interface.

profiles/default/

This folder contains the GenericSetup profile. During the training we will put some XML files here that hold configuration for the site.

profiles/default/metadata.xml

Version number and dependencies that are auto-installed when installing our add-on.

17.4. Including the package in Plone#

Before we can use our new package we have to tell Plone about it. Look at buildout.cfg and see how ploneconf.site is included in auto-checkout, eggs and test:

auto-checkout +=
    ploneconf.site
#    starzel.votable_behavior

parts =
    checkversions
    instance
    mrbob
    packages
    robot
    test
    zopepy

eggs =
    Plone
    Pillow

# development tools
    plone.api
    plone.reload
    Products.PDBDebugMode
    plone.app.debugtoolbar
    Products.PrintingMailHost
    pdbpp

# TTW Forms
    collective.easyform

# The add-on we develop in the training
    ploneconf.site

# Voting on content
#    starzel.votable_behavior

zcml =

test-eggs +=
    ploneconf.site [test]

This tells Buildout to add the egg ploneconf.site. The sources for this eggs are defined in the section [sources] at the bottom of buildout.cfg.

[sources]
ploneconf.site = git https://github.com/collective/ploneconf.site.git pushurl=git@github.com:collective/ploneconf.site.git
starzel.votable_behavior = git https://github.com/collective/starzel.votable_behavior.git pushurl=git://github.com/collective/starzel.votable_behavior.git

This tells buildout to not download it from pypi but to do a checkout from GitHub put the code in src/ploneconf.site.

Note

The package ploneconf.site is now downloaded from GitHub and automatically in the branch master. ploneconf.site can be called an egg even though it has not been released on pypi. Plone can use it like it uses an egg.

Note

If you do not want to use the prepared package for ploneconf.site from GitHub but write it yourself (we suggest you try that) then add the following instead:

[sources]
ploneconf.site = fs ploneconf.site path=src
starzel.votable_behavior = git https://github.com/collective/starzel.votable_behavior.git pushurl=git://github.com/collective/starzel.votable_behavior.git

This tells buildout to expect ploneconf.site in src/ploneconf.site. The directive fs allows you to add eggs on the filesystem without a version control system.

Now run buildout to reconfigure Plone with the updated configuration:

$ ./bin/buildout

After restarting Plone with ./bin/instance fg the new add-on ploneconf.site is available for install like EasyForm or Plone True Gallery.

We will not install it now since we did not add any of our own code or configuration yet. Let's do that next.

17.5. Exercises#

  1. Create a new package called collective.behavior.myfeature. Inspect the directory structure of this package. Delete it after you are done. Many packages that are part of Plone and some add-ons use a nested namespace such as plone.app.contenttypes.

  2. Open plone/bobtemplates.plone and read about the templates and subtemplates it provides.

17.6. Summary#

  • You created the package ploneconf.site to hold your code.

  • You added the new package to buildout so that Plone can use it.