Maik Derstappen: Efficient Backend development for Plone
Talk by Maik Derstappen at the online Plone Conference 2021.
How can we efficiently develop a Plone backend? What do we expect from the tooling?
- Should be easy to adopt for new developers
- reduced development time
- following best practices
- provide test infrastructure and basic tests
These are our two friends:
- plonecli
- plone-snippets for VS Code
plonecli is a command line tool, installed with pip together with bobtemplates.plone, here version 6.0b10. With it, you iteratively enhance your add-on.
Let's create a restaurant add-on with a content type for the menu data, a vocabulary for the menu category, control panel, plus a bit more.
Demo:
plonecli create addon collective.restaurant cd collective.restaurant/ plonecli add controlpanel
Open Visual Studio Code (my preferred editor) and edit the created controlpanel.py. Add a field: a list of categories. Now add a vocabulary for this:
plonecli add vocabulary
Edit menu_category.py to let this get the contents of the controlpanel field from the registry.
Add two content types: Menu and Menu Item:
plonecli add content_type
Edit menu_item.py. When you have plone-vs-snippets installed in VS Code, it is easy to add fields. Add a photo (NamedBlobImage), menu_category (Choice with your menu vocabulary name).
Edit the types/*.xml files to enable behaviors like plone.basic and plone.namefromtitle.
Now get a local environment, mostly by running buildout, and start the instance, with:
plonecli build plonecli serve
On http://localhost:8080 create a Plone Site and activate collective.restaurant and plone.restapi. Go to the Restaurant control panel and add a few categories, like Starters, Main, Desserts. Now you can add a Menu folder and add menu items in various categories.
The basic data is available through the REST api. But we want more. Add a service RelatedDishes:
plonecli add restapi service
Edit api/services/related_dishes/get.py and use api.content.find to look for other Menu Items with the same category.
To make searching easier, edit profiles/default/catalog.xml. With the Plone VS code snippets, you can agains easily add a catalog index here. We will need an upgrade step to apply this:
plonecli add upgrade_step
Restart, reindex, and it works.
Please help by contributing templates to bobtemplates.plone or create an issue when you think a template can be improved. Or create an own similar package, like bobtemplates.migration.
Ideas for the future:
- Rest API support question in addon template
- Rest API sub-templates for serializer and deserializer
- support for Mosaic tiles
- graphical UI (ncurses) to make choices easier