--- a/src/pyams_portal/interfaces/__init__.py Thu Jun 14 17:35:36 2018 +0200
+++ b/src/pyams_portal/interfaces/__init__.py Fri Jun 15 14:05:17 2018 +0200
@@ -56,7 +56,7 @@
toolbar_css_class = Attribute("Portlet toolbar CSS class")
- settings_class = Attribute("Portlet settings class")
+ settings_factory = Attribute("Portlet settings factory")
class IPortletAddingInfo(Interface):
--- a/src/pyams_portal/portlet.py Thu Jun 14 17:35:36 2018 +0200
+++ b/src/pyams_portal/portlet.py Fri Jun 15 14:05:17 2018 +0200
@@ -19,6 +19,8 @@
import venusian
+from inspect import isclass
+
# import interfaces
from pyams_form.interfaces.form import IFormContextPermissionChecker
from pyams_portal.interfaces import IPortlet, IPortletSettings, IPortletConfiguration, IPortletPreviewer, \
@@ -30,6 +32,7 @@
from persistent import Persistent
from persistent.mapping import PersistentMapping
from pyams_utils.adapter import adapter_config, ContextAdapter, get_adapter_weight, get_annotation_adapter
+from pyams_utils.factory import get_object_factory
from pyams_utils.request import check_request
from pyams_utils.vocabulary import vocabulary_config
from pyams_viewlet.viewlet import ViewContentProvider
@@ -38,7 +41,7 @@
from pyramid.threadlocal import get_current_registry
from zope.container.contained import Contained
from zope.copy import clone
-from zope.interface import implementer, alsoProvides, noLongerProvides
+from zope.interface import implementer, alsoProvides, noLongerProvides, Interface
from zope.lifecycleevent import ObjectCreatedEvent
from zope.location import locate
from zope.schema.fieldproperty import FieldProperty
@@ -58,7 +61,7 @@
toolbar_image = None
toolbar_css_class = 'fa fa-fw fa-2x fa-edit'
- settings_class = None
+ settings_factory = None
class portlet_config(object):
@@ -206,7 +209,10 @@
This class is supposed to be sub-classed by all custom portlet subclasses to
store their configuration settings.
- Each portlet sub-class must define it's settings class in it's "settings_class" attribute.
+
+ Each portlet sub-class must define it's settings factory in it's "settings_factory" attribute.
+ Given factory can be a function, a class or an interface; in this last case, implementation
+ is looking for default object factory registered for this interface.
"""
_renderer = FieldProperty(IPortletSettings['renderer'])
@@ -278,7 +284,11 @@
def __init__(self, portlet):
self.portlet_name = portlet.name
- self._settings = portlet.settings_class(self)
+ factory = portlet.settings_factory
+ if isclass(factory) and issubclass(factory, Interface):
+ factory = get_object_factory(factory)
+ assert factory is not None, "Missing portlet settings factory"
+ self._settings = factory(self)
@property
def can_inherit(self):
--- a/src/pyams_portal/portlets/html/__init__.py Thu Jun 14 17:35:36 2018 +0200
+++ b/src/pyams_portal/portlets/html/__init__.py Fri Jun 15 14:05:17 2018 +0200
@@ -21,6 +21,7 @@
# import packages
from pyams_portal.portlet import PortletSettings, portlet_config, Portlet
+from pyams_utils.factory import factory_config
from zope.interface import implementer
from zope.schema.fieldproperty import FieldProperty
@@ -35,6 +36,7 @@
@implementer(IRawPortletSettings)
+@factory_config(provided=IRawPortletSettings)
class RawPortletSettings(PortletSettings):
"""Raw HTML code portlet settings"""
@@ -50,7 +52,7 @@
toolbar_css_class = 'fa fa-fw fa-2x fa-code'
- settings_class = RawPortletSettings
+ settings_factory = IRawPortletSettings
#
@@ -61,6 +63,7 @@
@implementer(IHTMLPortletSettings)
+@factory_config(provided=IHTMLPortletSettings)
class HTMLPortletSettings(PortletSettings):
"""Rich text portlet settings"""
@@ -74,6 +77,6 @@
name = HTML_PORTLET_NAME
label = _("Rich text")
- toolbar_css_class = 'fa fa-fw fa-2x fa-html5'
+ toolbar_css_class = 'fa fa-fw fa-2x fa-font'
- settings_class = HTMLPortletSettings
+ settings_factory = IHTMLPortletSettings
--- a/src/pyams_portal/portlets/image/__init__.py Thu Jun 14 17:35:36 2018 +0200
+++ b/src/pyams_portal/portlets/image/__init__.py Fri Jun 15 14:05:17 2018 +0200
@@ -23,6 +23,7 @@
# import packages
from pyams_file.property import FileProperty
from pyams_portal.portlet import portlet_config, Portlet, PortletSettings
+from pyams_utils.factory import factory_config
from zope.interface import implementer, alsoProvides
from pyams_portal import _
@@ -32,6 +33,7 @@
@implementer(IImagePortletSettings)
+@factory_config(provided=IImagePortletSettings)
class ImagePortletSettings(PortletSettings):
"""Image portlet settings"""
@@ -58,4 +60,4 @@
toolbar_image = None
toolbar_css_class = 'fa fa-fw fa-2x fa-picture-o'
- settings_class = ImagePortletSettings
+ settings_factory = IImagePortletSettings