Added HTML portlets
authorThierry Florac <tflorac@ulthar.net>
Sun, 11 Mar 2018 11:04:34 +0100
changeset 67 d121b3645219
parent 66 5f2a36c93535
child 68 6945c7f17308
Added HTML portlets
src/pyams_portal/portlets/html/__init__.py
src/pyams_portal/portlets/html/html.pt
src/pyams_portal/portlets/html/interfaces.py
src/pyams_portal/portlets/html/zmi/__init__.py
src/pyams_portal/portlets/html/zmi/preview.pt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlets/html/__init__.py	Sun Mar 11 11:04:34 2018 +0100
@@ -0,0 +1,99 @@
+#
+# Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_portal.interfaces import IPortalContext, IPortletRenderer
+from pyams_portal.portlets.html.interfaces import IRawPortletSettings, IHTMLPortletSettings
+from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_PERMISSION
+
+# import packages
+from pyams_portal.portlet import PortletSettings, portlet_config, Portlet, PortletRenderer
+from pyams_template.template import template_config
+from pyams_utils.adapter import adapter_config
+from zope.interface import implementer, Interface
+from zope.schema.fieldproperty import FieldProperty
+
+from pyams_portal import _
+
+
+#
+# Raw HTML code portlet
+#
+
+RAW_PORTLET_NAME = 'pyams_portal.portlet.raw'
+
+
+@implementer(IRawPortletSettings)
+class RawPortletSettings(PortletSettings):
+    """Raw HTML code portlet settings"""
+
+    body = FieldProperty(IRawPortletSettings['body'])
+
+
+@portlet_config(permission=VIEW_PERMISSION)
+class RawPortlet(Portlet):
+    """Raw HTML code portlet"""
+
+    name = RAW_PORTLET_NAME
+    label = _("Raw HTML")
+
+    toolbar_css_class = 'fa fa-fw fa-2x fa-code'
+
+    settings_class = RawPortletSettings
+
+
+@adapter_config(context=(IPortalContext, IPyAMSLayer, Interface, IRawPortletSettings), provides=IPortletRenderer)
+@template_config(template='html.pt', layer=IPyAMSLayer)
+class RawPortletDefaultRenderer(PortletRenderer):
+    """Raw HTML portlet renderer"""
+
+    label = _("Default code renderer")
+
+
+#
+# Rich text portlet
+#
+
+HTML_PORTLET_NAME = 'pyams_portal.portlet.html'
+
+
+@implementer(IHTMLPortletSettings)
+class HTMLPortletSettings(PortletSettings):
+    """Rich text portlet settings"""
+
+    body = FieldProperty(IHTMLPortletSettings['body'])
+
+
+@portlet_config(permission=VIEW_PERMISSION)
+class HTMLPortlet(Portlet):
+    """Rich text portlet"""
+
+    name = HTML_PORTLET_NAME
+    label = _("Rich text")
+
+    toolbar_css_class = 'fa fa-fw fa-2x fa-html5'
+
+    settings_class = HTMLPortletSettings
+
+
+@adapter_config(context=(IPortalContext, IPyAMSLayer, Interface, IHTMLPortletSettings), provides=IPortletRenderer)
+@template_config(template='html.pt', layer=IPyAMSLayer)
+class HTMLPortletDefaultRenderer(PortletRenderer):
+    """Rich text portlet renderer"""
+
+    label = _("Default text renderer")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlets/html/html.pt	Sun Mar 11 11:04:34 2018 +0100
@@ -0,0 +1,1 @@
+<tal:var replace="structure i18n:view.settings.body">HTML code</tal:var>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlets/html/interfaces.py	Sun Mar 11 11:04:34 2018 +0100
@@ -0,0 +1,41 @@
+#
+# Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_portal.interfaces import IPortletSettings
+
+# import packages
+from pyams_i18n.schema import I18nTextField, I18nHTMLField
+
+
+from pyams_portal import _
+
+
+class IRawPortletSettings(IPortletSettings):
+    """Raw HTML code portlet settings interface"""
+
+    body = I18nTextField(title=_("Raw HTML code"),
+                         description=_("This HTML code will be used 'as is', without any transformation. Use with "
+                                       "care!!"),
+                         required=False)
+
+
+class IHTMLPortletSettings(IPortletSettings):
+    """Rich text portlet settings interface"""
+
+    body = I18nHTMLField(title=_("Body"),
+                         required=False)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlets/html/zmi/__init__.py	Sun Mar 11 11:04:34 2018 +0100
@@ -0,0 +1,89 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_form.interfaces.form import IInnerTabForm
+from pyams_pagelet.interfaces import IPagelet
+from pyams_portal.interfaces import IPortletPreviewer
+from pyams_portal.portlets.html.interfaces import IRawPortletSettings, IHTMLPortletSettings
+from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
+
+# import packages
+from pyams_form.form import AJAXEditForm
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_portal.portlet import PortletPreviewer
+from pyams_portal.zmi.portlet import PortletSettingsEditor, PortletSettingsPropertiesEditor
+from pyams_template.template import template_config
+from pyams_utils.adapter import adapter_config
+from zope.interface import Interface
+
+
+#
+# Raw code portlet properties
+#
+
+@pagelet_config(name='properties.html', context=IRawPortletSettings, request_type=IPyAMSLayer,
+                permission=VIEW_SYSTEM_PERMISSION)
+class RawPortletSettingsEditor(PortletSettingsEditor):
+    """Raw HTML portlet settings editor"""
+
+    settings = IRawPortletSettings
+
+
+@adapter_config(name='properties', context=(Interface, IPyAMSLayer, RawPortletSettingsEditor), provides=IInnerTabForm)
+class RawPortletSettingsPropertiesEditor(PortletSettingsPropertiesEditor):
+    """Raw portlet settings properties editor"""
+
+    def updateWidgets(self, prefix=None):
+        super(RawPortletSettingsPropertiesEditor, self).updateWidgets(prefix)
+        if 'body' in self.widgets:
+            self.widgets['body'].widget_css_class = 'textarea height-100'
+
+
+@adapter_config(name='properties.json', context=(IRawPortletSettings, IPyAMSLayer), provides=IPagelet)
+class RawPortletConfigurationAJAXEditor(AJAXEditForm, RawPortletSettingsEditor):
+    """Raw HTML portlet settings editor, AJAX renderer"""
+
+
+@adapter_config(context=(Interface, IPyAMSLayer, Interface, IRawPortletSettings), provides=IPortletPreviewer)
+@template_config(template='preview.pt', layer=IPyAMSLayer)
+class RawPortletPreviewer(PortletPreviewer):
+    """Raw HTML portlet previewer"""
+
+
+#
+# Rich text portlet properties
+#
+
+@pagelet_config(name='properties.html', context=IHTMLPortletSettings, request_type=IPyAMSLayer,
+                permission=VIEW_SYSTEM_PERMISSION)
+class HTMLPortletSettingsEditor(PortletSettingsEditor):
+    """Rich text portlet settings editor"""
+
+    settings = IHTMLPortletSettings
+
+
+@adapter_config(name='properties.json', context=(IHTMLPortletSettings, IPyAMSLayer), provides=IPagelet)
+class HTMLPortletConfigurationAJAXEditor(AJAXEditForm, HTMLPortletSettingsEditor):
+    """Rich text portlet settings editor, AJAX renderer"""
+
+
+@adapter_config(context=(Interface, IPyAMSLayer, Interface, IHTMLPortletSettings), provides=IPortletPreviewer)
+@template_config(template='preview.pt', layer=IPyAMSLayer)
+class HTMLPortletPreviewer(PortletPreviewer):
+    """Rich text portlet previewer"""
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/portlets/html/zmi/preview.pt	Sun Mar 11 11:04:34 2018 +0100
@@ -0,0 +1,12 @@
+<tal:var define="settings view.settings">
+	<tal:if condition="settings.visible"
+			replace="structure i18n:view.settings.body">HTML code</tal:if>
+	<tal:if condition="not settings.visible">
+		<div class="text-center padding-y-5">
+			<span class="fa-stack fa-lg">
+				<i class="fa fa-eye fa-stack-1x"></i>
+				<i class="fa fa-ban fa-stack-2x text-danger"></i>
+			</span>
+		</div>
+	</tal:if>
+</tal:var>
\ No newline at end of file