Create content during add-on install

I'm developing a small back-end add-on (for Plone 6) and I'd like to programmatically create a certain Folder structure when my add-on is installed.

I'm not sure how I should configure the add-on / where I should write my code.
Can anyone give me some suggestion and/or point me to documentation?

[more details]
I've used cookieplone to setup the add-on
and then tried plonecli add both site_initialization and upgrade_step but they "don't feel like" the correct approach (or maybe they are and I'm just missing something :slightly_smiling_face: )

The operation that I need to perform is something like the following:

portal = plone.api.portal.get()  # ?
if "my_folder" not in portal:
    subfolder = create(
        container=portal,
        type="Folder",
        id="my_folder",
        title="...",
    )
    subfolder.setDescription("...")

If it is for a fresh install it should go into a setuphandler post_handler which is registered in profiles.zcml like so.

  <genericsetup:registerProfile
      name="default"
      title="myaddon: Install"
      description="Package to configure a new myaddon site"
      provides="Products.GenericSetup.interfaces.EXTENSION"
      post_handler=".setuphandlers.post.install"
      directory="profiles/default"
      />

Or in other words: Add the line with post_handler to the existing profile.

In folder /setuphandler create a new file post.py and in there you need a function install like so:


def install(context):
    """Post install script"""
    # your folder creation code goes here
2 Likes