Programming Plone#

Todo

* Discuss backend development versus frontend development
* Discuss plone.restapi
* Discuss React developer tools

In this part you will:

  • Learn about the recommended ways to do something in backend code in Plone.

  • Learn to debug

Topics covered:

  • plone.api

  • Portal tools

  • Debugging

plone.api#

The most important tool nowadays for plone developers is the add-on plone.api that covers 20% of the tasks any Plone developer does 80% of the time. If you are not sure how to handle a certain task, be sure to first check if plone.api has a solution for you.

The API is divided in five sections. Here is one example from each:

plone.api is a great tool for integrators and developers that is included when you install Plone, though for technical reasons it is not used by the code of Plone itself.

In existing code you'll often encounter methods that don't mean anything to you. You'll have to use the source to find out what they do.

Some of these methods are replaced by plone.api:

  • Products.CMFCore.utils.getToolByName() -> api.portal.get_tool()

  • zope.component.getMultiAdapter() -> api.content.get_view()

portal tools#

Some parts of Plone are very complex modules in themselves (e.g. the versioning machinery of Products.CMFEditions). Most of them have an API of themselves that you will have to look up when you need to implement a feature that is not covered by plone.api.

Here are a few examples:

portal_catalog

unrestrictedSearchResults() returns search results without checking if the current user has the permission to access the objects.

uniqueValuesFor() returns all entries in an index

portal_setup

runAllExportSteps() generates a tarball containing artifacts from all export steps.

Products.CMFPlone.utils

is_product_installed() checks if a product is installed.

Usually the best way to learn about the API of a tool is to look in the interfaces.py in the respective package and read the docstrings. But sometimes the only way to figure out which features a tool offers is to read its code.

To use a tool, you usually first get the tool with plone.api and then invoke the method.

Here is an example where we get the tool portal_membership and use one of its methods to logout a user:

mt = api.portal.get_tool('portal_membership')
mt.logoutUser(request)

Note

The code for logoutUser() is in Products.PlonePAS.tools.membership.MembershipTool.logoutUser(). Many tools that are used in Plone are actually subclasses of tools from the package Products.CMFCore. For example portal_membership is subclassing and extending the same tool from Products.CMFCore.MembershipTool.MembershipTool. That can make it hard to know which options a tool has. There is an ongoing effort by the Plone Community to consolidate tools to make it easier to work with them as a developer.

Debugging#

Here are some tools and techniques we often use when developing and debugging. We use some of them in various situations during the training.

tracebacks and the log

The log (and the console when running in foreground) collects all log messages Plone prints. When an exception occurs, Plone throws a traceback. Most of the time the traceback is everything you need to find out what is going wrong. Also adding your own information to the log is very simple.

import logging

logger = logging.getLogger(__name__)

def do_vote(user, vote):
    logger.info(f"User {user} voted with {vote}")
pdb

The Python debugger pdb is the single most important tool for us when programming. Just add import pdb; pdb.set_trace() in your code and debug away! The code execution stops at the line you added import pdb; pdb.set_trace(). Switch to your terminal and step through your code.

pdbpp

A great drop-in replacement for pdb with tab completion, syntax highlighting, better tracebacks, introspection and more. And the best feature ever: The command ll prints the whole current method.

ipdb

Another enhanced pdb with the power of IPython, e.g. tab completion, syntax highlighting, better tracebacks and introspection. It also works nicely with Products.PDBDebugMode. Needs to be invoked with import ipdb; ipdb.set_trace().

Products.PDBDebugMode

An add-on that has two killer features.

Post-mortem debugging: throws you in a pdb whenever an exception occurs. This way you can find out what is going wrong.

pdb view: simply adding /pdb to a url drops you in a pdb session with the current context as self.context. From there you can do just about anything.

Interactive debugger

When starting Plone using venv/bin/zconsole debug instance/etc/zope.conf, you'll end up in an interactive debugger. app.Plone is your instance which you can inspect on the command line.

To list the ids of the objects in a folderish object:

>>> app.Plone.talks.keys()
['whats-new-in-python-3.10', 'plone-7', 'zope', 'betty-white', 'new-years-day', 'journey-band']

To list the items of a folderish object:

>>> from zope.component.hooks import setSite
>>> setSite(app.Plone)
>>> app.Plone.talks.contentItems()
[('whats-new-in-python-3.10', <Talk at /Plone/talks/whats-new-in-python-3.10>), ('plone-7', <Talk at /Plone/talks/plone-7>), ('zope', <Talk at /Plone/talks/zope>), ('betty-white', <Talk at /Plone/talks/betty-white>), ('new-years-day', <Talk at /Plone/talks/new-years-day>), ('journey-band', <Talk at /Plone/talks/journey-band>)]

Stop the interactive shell with ctrl-d.

plone.app.debugtoolbar

An add-on that allows you to inspect nearly everything. It even has an interactive console, a tester for TALES-expressions and includs a reload-feature like plone.reload.

plone.reload

An add-on that allows to reload code that you changed without restarting the site. Open http://localhost:8080/@@reload in your browser for plone.reloads UI. It is also used by plone.app.debugtoolbar.

Products.PrintingMailHost

An add-on that prevents Plone from sending mails. Instead, they are logged.

verbose-security = on

An option for the recipe plone.recipe.zope2instance that logs the detailed reasons why a user might not be authorized to see something.

Sentry

Sentry is an error logging application you can host yourself. It aggregates tracebacks from many sources and (here comes the killer feature) even the values of variables in the traceback. We use it in all our production sites.

Exercise#

  • Create a new BrowserView callable as /@@demo_content in a new file demo.py.

  • The view should create five talks each time it is called.

  • Use the docs at Content to find out how to create new talks.

  • Use plone.api.content.transition to publish all new talks. Find the docs for that method.

  • Only managers should be able to use the view (the permission is called cmf.ManagePortal).

  • Switch to the frontpage after calling the view.

  • Display a message about the results (see Show notification message).

  • For extra credits use the library requests and Wikipedia to populate the talks with content.

  • Use the utility methods cropText from Producs.CMFPlone to crop the title after 20 characters. Use the docs at Global utilities and helpers to find an overview of plone_view helpers.

Note

  • Do not try everything at once, work in small iterations, restart to check your results frequently.

  • Use pdb during development to experiment.

Solution

Add this to browser/configure.zcml:

1<browser:page
2  name="create_demo_talks"
3  for="*"
4  class=".demo.DemoContent"
5  permission="cmf.ManagePortal"
6  />

This is browser/demo.py:

 1from Products.Five import BrowserView
 2from plone import api
 3
 4from plone.protect.interfaces import IDisableCSRFProtection
 5from zope.interface import alsoProvides
 6
 7import json
 8import logging
 9import requests
10
11logger = logging.getLogger(__name__)
12
13
14class DemoContent(BrowserView):
15    def __call__(self):
16        portal = api.portal.get()
17        self.create_talks()
18        return self.request.response.redirect(portal.absolute_url())
19
20    def create_talks(self, amount=3):
21        """Create some talks"""
22
23        alsoProvides(self.request, IDisableCSRFProtection)
24        plone_view = api.content.get_view("plone", self.context, self.request)
25        wiki_content = self.get_wikipedia_content_of_the_day()
26        for data in wiki_content[:amount]:
27            talk = api.content.create(
28                container=self.context,
29                type="talk",
30                title=plone_view.cropText(data["titles"]["normalized"], length=20),
31                description=data["description"],
32                type_of_talk="talk",
33            )
34            api.content.transition(talk, to_state="published")
35            logger.info(f"Created talk {talk.absolute_url()}")
36        api.portal.show_message(f"Created {amount} talks!", self.request)
37
38    def get_wikipedia_content_of_the_day(self):
39        wiki = requests.get(
40            "https://en.wikipedia.org/api/rest_v1/feed/featured/2022/01/02"
41        )
42        return json.loads(wiki.text)["mostread"]["articles"]

Some notes:

  • Since calling view is a GET and not a POST we need alsoProvides(self.request, IDisableCSRFProtection)() to allow write-on-read without Plone complaining. Alternatively we could create a simple form and create the content on submit.

  • Transition has two modes of operation: The documented one is api.content.transition(obj=foo, transition='bar'). That mode tries to execute that specific transition. But sometimes it is better to use to_state which tries to to find a way to get from the current state to the target state. Follow the link to the method description in documentation to find more information on transition.

  • To use methods like cropText from another browser view, you can get the view for your context with api.content.get_view("view_name", self.context, self.request)

  • Here the description of the talk is set. To add text, you need to create an instance of RichTextValue and set it as an attribute:

    from plone.app.textfield.value import RichTextValue
    talk.details = RichTextValue(data["extract"], 'text/plain', 'text/html',)