Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new record to plone.registry without re-running GenericSetup / re-installing the product

in a Plone add-on product, I have a control panel page where some config options can be set. They are stored in plone.registry. The control panel adapter fetches the different fields in its __init__ method by querying the interface, like:

class MultiLanguageExtraOptionsAdapter(LanguageControlPanelAdapter):
    implementsOnly(IMultiLanguageExtraOptionsSchema)

    def __init__(self, context):
        super(MultiLanguageExtraOptionsAdapter, self).__init__(context)
        self.registry = getUtility(IRegistry)
        self.settings = self.registry.forInterface(
            IMultiLanguageExtraOptionsSchema)

Now I add an additional field to the interface IMultiLanguageExtraOptionsSchema and restart plone. On the control panel page I then an error:

KeyError: 'Interface `plone.app.multilingual.interfaces.IMultiLanguageExtraOptionsSchema` defines a field `blah`, for which there is no record.'

(This is expected for the forInterfacemethod , as described on the plone.registry README. The record is not there.)

Of course, if I add that field via GenericSetup (registry.xml), and I re-install the product / re-run the "Control Panel" step, all is well:

<registry>
 <records interface="plone.app.multilingual.interfaces.IMultiLanguageExtraOptionsSchema">
   <value key="blah"></value>
 <records>
<registry>

But I don't want to force users to re-install a product, just because there's a new option in the product-specific control panel. So my question: Is there a recommended way for getting a new record for a new field into plone.registry?

like image 413
pysailor Avatar asked Dec 09 '22 17:12

pysailor


1 Answers

You could try/catch the KeyError and then make sure all registry settings are registered:

try:
    self.settings = self.registry.forInterface(IMultiLanguageExtraOptionsSchema)
except KeyError:
    registry = getUtility(IRegistry)
    registry.registerInterface(IMultiLanguageExtraOptionsSchema)

I would recommend to write an upgrade step though (which would force your users to reinstall the product of course).

upgrades.py:

def update_registry(context):
    registry = getUtility(IRegistry)
    registry.registerInterface(IMultiLanguageExtraOptionsSchema)

upgrades.zcml::

  <genericsetup:upgradeStep
      source="*"
      destination="1100"
      title="Update plone.app.multilingual setting registry"
      description=""
      profile="plone.app.multilingual:default"
      handler=".upgrades.update_registry"
      />

See

https://github.com/collective/collective.mailchimp/blob/master/collective/mailchimp/upgrades.py

and

https://github.com/collective/collective.mailchimp/blob/master/collective/mailchimp/upgrades.zcml

for an example.

like image 61
tisto Avatar answered Jun 14 '23 00:06

tisto