简体   繁体   中英

Extend Plone-Controlpanel Form

Is it possible to extend the Controlpanel-View with each addon?

For Example
ca.db.core -> Makes basic fieldset/tab for DB Connection Settings
ca.db.person -> If installed, Adds to the "core" settings a new fieldset/tab for Person specific fields/settings
ca.db.schema -> If installed, also adds an new fieldset/tab for schema.org Fields

Yes it's possible, I once discussed this problem with a guy, who wrote the bda.plone.shop addon.

They faced the same problem, and solved it by using a ContextProxy object, which puts the different schema definitions together in one proxy object.

Using a proxy is IMHO a hack, but I don't know a better solution.

The proxy, tries to get/set a attribute from a list of schemas. Be aware, you need to handle conflicting names, means if you have the same field name in more than one schema.

class ContextProxy(object):

    def __init__(self, interfaces):
        self.__interfaces = interfaces
        alsoProvides(self, *interfaces)

    def __setattr__(self, name, value):
        if name.startswith('__') or name.startswith('_ContextProxy__'):
            return object.__setattr__(self, name, value)

        registry = getUtility(IRegistry)
        for interface in self.__interfaces:
            proxy = registry.forInterface(interface)
            try:
                getattr(proxy, name)
            except AttributeError:
                pass
            else:
                return setattr(proxy, name, value)
        raise AttributeError(name)

    def __getattr__(self, name):
        if name.startswith('__') or name.startswith('_ContextProxy__'):
            return object.__getattr__(self, name)

        registry = getUtility(IRegistry)
        for interface in self.__interfaces:
            proxy = registry.forInterface(interface)
            try:
                return getattr(proxy, name)
            except AttributeError:
                pass

        raise AttributeError(name)

Now you need to use the proxy in your ControlPanel form. I assume you're using the RegistryEditForm from plone.registry :

class SettingsEditForm(controlpanel.RegistryEditForm):
    schema = ISettings
    label = _(u"Settings")
    description = _(u"")

    # IMPORTANT Note 1 - This is where you hook in your proxy    
    def getContent(self):
        interfaces = [self.schema]  # Base schema from ca.db.core
        interfaces.extend(self.additionalSchemata) # List of additional schemas
        return ContextProxy(interfaces)

    # IMPORTANT Note 2 - You may get the additional schemas dynamically to extend the Settings Form. For example by name (startswith...)
    # In this case they have a separate interface, which marks the relevant interfaces.
    @property
    def additionalSchemata(self):
        registry = getUtility(IRegistry)
        interface_names = set(record.interfaceName for record
                              in registry.records.values())

        for name in interface_names:
            if not name:
                continue

            interface = None
            try:
                interface = resolve(name)
            except ImportError:
                # In case of leftover registry entries of uninstalled Products
                continue

            if ISettingsProvider.providedBy(interface):
                yield interface

     ...

You can find the full code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM