Added key points paragraph
authorThierry Florac <thierry.florac@onf.fr>
Fri, 02 Mar 2018 11:22:56 +0100
changeset 416 c848b7ceefb7
parent 415 43f42d74cdb8
child 417 f09842eb4ada
Added key points paragraph
src/pyams_content/component/paragraph/interfaces/keypoint.py
src/pyams_content/component/paragraph/keypoint.py
src/pyams_content/component/paragraph/zmi/keypoint.py
src/pyams_content/component/paragraph/zmi/templates/keypoints-preview.pt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/interfaces/keypoint.py	Fri Mar 02 11:22:56 2018 +0100
@@ -0,0 +1,39 @@
+#
+# 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_content.component.paragraph.interfaces import IBaseParagraph
+
+# import packages
+from pyams_i18n.schema import I18nTextField
+
+from pyams_content import _
+
+
+#
+# Keypoints paragraph
+#
+
+KEYPOINTS_PARAGRAPH_TYPE = 'Keypoints'
+
+
+class IKeypointsParagraph(IBaseParagraph):
+    """Keypoints paragraph"""
+
+    body = I18nTextField(title=_("Key points"),
+                         description=_("Enter one key point by line, without hyphen or prefix"),
+                         required=False)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/keypoint.py	Fri Mar 02 11:22:56 2018 +0100
@@ -0,0 +1,82 @@
+#
+# 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_content.component.paragraph.interfaces import IParagraphFactory
+from pyams_content.component.paragraph.interfaces.keypoint import IKeypointsParagraph, KEYPOINTS_PARAGRAPH_TYPE
+from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
+from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
+
+# import packages
+from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker, BaseParagraphFactory
+from pyams_utils.adapter import adapter_config
+from pyams_utils.registry import utility_config, get_utility
+from pyams_utils.text import get_text_start
+from pyams_utils.traversing import get_parent
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
+from pyams_content import _
+
+
+@implementer(IKeypointsParagraph)
+class KeypointsParagraph(BaseParagraph):
+    """Key points paragraph"""
+
+    icon_class = 'fa-key'
+    icon_hint = _("Key points")
+
+    @property
+    def title(self):
+        body = II18n(self).query_attribute('body')
+        return get_text_start(body, 50, 10)
+
+    body = FieldProperty(IKeypointsParagraph['body'])
+
+
+@utility_config(name=KEYPOINTS_PARAGRAPH_TYPE, provides=IParagraphFactory)
+class KeypointsParagraphFactory(BaseParagraphFactory):
+    """Key points paragraph factory"""
+
+    name = _("Key points paragraph")
+    content_type = KeypointsParagraph
+    secondary_menu = True
+
+
+@adapter_config(context=IKeypointsParagraph, provides=IContentChecker)
+class KeypointsParagraphContentChecker(BaseParagraphContentChecker):
+    """Key points paragraph content checker"""
+
+    def inner_check(self, request):
+        output = []
+        translate = request.localizer.translate
+        manager = get_parent(self.context, II18nManager)
+        if manager is not None:
+            langs = manager.get_languages()
+        else:
+            negotiator = get_utility(INegotiator)
+            langs = (negotiator.server_language, )
+        i18n = II18n(self.context)
+        for lang in langs:
+            value = i18n.get_attribute('body', lang, request)
+            if not value:
+                field_title = translate(IKeypointsParagraph['body'].title)
+                if len(langs) == 1:
+                    output.append(translate(MISSING_VALUE).format(field=field_title))
+                else:
+                    output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
+        return output
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/zmi/keypoint.py	Fri Mar 02 11:22:56 2018 +0100
@@ -0,0 +1,164 @@
+#
+# 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_content.component.paragraph.interfaces import IParagraphContainerTarget, IParagraphContainer, \
+    IParagraphPreview
+from pyams_content.component.paragraph.interfaces.keypoint import IKeypointsParagraph, KEYPOINTS_PARAGRAPH_TYPE
+from pyams_content.component.paragraph.zmi.interfaces import IParagraphInnerEditor, IParagraphContainerView
+from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
+from pyams_form.interfaces.form import IInnerForm, IEditFormButtons
+from pyams_i18n.interfaces import II18n
+from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
+from pyams_skin.layer import IPyAMSLayer
+from z3c.form.interfaces import INPUT_MODE
+
+# import packages
+from pyams_content.component.paragraph.keypoint import KeypointsParagraph
+from pyams_content.component.paragraph.zmi import BaseParagraphAJAXAddForm, BaseParagraphAJAXEditForm, \
+    BaseParagraphAddMenu, BaseParagraphPropertiesEditForm
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_template.template import template_config
+from pyams_utils.adapter import adapter_config
+from pyams_viewlet.viewlet import viewlet_config, BaseContentProvider
+from pyams_zmi.form import AdminDialogAddForm
+from pyramid.view import view_config
+from z3c.form import field, button
+from zope.interface import implementer
+
+from pyams_content import _
+
+
+@viewlet_config(name='add-keypoints-paragraph.menu', context=IParagraphContainerTarget, view=IParagraphContainerView,
+                layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=570)
+class KeypointsParagraphAddMenu(BaseParagraphAddMenu):
+    """Key points paragraph add menu"""
+
+    label = _("Key points...")
+    label_css_class = 'fa fa-fw fa-key'
+    url = 'add-keypoints-paragraph.html'
+    paragraph_type = KEYPOINTS_PARAGRAPH_TYPE
+
+
+@pagelet_config(name='add-keypoints-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer,
+                permission=MANAGE_CONTENT_PERMISSION)
+class KeypointsParagraphAddForm(AdminDialogAddForm):
+    """Key points paragraph add form"""
+
+    legend = _("Add new key points paragraph")
+    icon_css_class = 'fa fa-fw fa-key'
+
+    fields = field.Fields(IKeypointsParagraph).select('body')
+    ajax_handler = 'add-keypoints-paragraph.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(KeypointsParagraphAddForm, self).updateWidgets(prefix)
+        if 'body' in self.widgets:
+            self.widgets['body'].widget_css_class = 'textarea height-100'
+
+    def create(self, data):
+        return KeypointsParagraph()
+
+    def add(self, object):
+        IParagraphContainer(self.context).append(object)
+
+
+@view_config(name='add-keypoints-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class KeypointsParagraphAJAXAddForm(BaseParagraphAJAXAddForm, KeypointsParagraphAddForm):
+    """Key points paragraph add form, JSON renderer"""
+
+
+@pagelet_config(name='properties.html', context=IKeypointsParagraph, layer=IPyAMSLayer,
+                permission=MANAGE_CONTENT_PERMISSION)
+class KeypointsParagraphPropertiesEditForm(BaseParagraphPropertiesEditForm):
+    """Key points paragraph properties edit form"""
+
+    legend = _("Edit key points paragraph properties")
+    icon_css_class = 'fa fa-fw fa-key'
+
+    fields = field.Fields(IKeypointsParagraph).select('body')
+    ajax_handler = 'properties.json'
+    edit_permission = MANAGE_CONTENT_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(KeypointsParagraphPropertiesEditForm, self).updateWidgets(prefix)
+        if 'body' in self.widgets:
+            self.widgets['body'].widget_css_class = 'textarea height-100'
+
+
+@view_config(name='properties.json', context=IKeypointsParagraph, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class KeypointsParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, KeypointsParagraphPropertiesEditForm):
+    """Key points paragraph properties edit form, JSON renderer"""
+
+    def get_ajax_output(self, changes):
+        output = super(KeypointsParagraphPropertiesAJAXEditForm, self).get_ajax_output(changes)
+        if 'body' in changes.get(IKeypointsParagraph, ()):
+            output.setdefault('events', []).append({
+                'event': 'myams.refresh',
+                'options': {
+                    'handler': 'PyAMS_content.paragraphs.refreshParagraph',
+                    'object_name': self.context.__name__,
+                    'title': II18n(self.context).query_attribute('title', request=self.request),
+                    'visible': self.context.visible
+                }
+            })
+        return output
+
+
+@adapter_config(context=(IKeypointsParagraph, IPyAMSLayer), provides=IParagraphInnerEditor)
+@implementer(IInnerForm)
+class KeypointsParagraphInnerEditForm(KeypointsParagraphPropertiesEditForm):
+    """Key points paragraph inner edit form"""
+
+    legend = None
+    label_css_class = 'control-label col-md-2'
+    input_css_class = 'col-md-10'
+
+    @property
+    def buttons(self):
+        if self.mode == INPUT_MODE:
+            return button.Buttons(IEditFormButtons)
+        else:
+            return button.Buttons()
+
+
+#
+# Key points paragraph preview
+#
+
+@adapter_config(context=(IKeypointsParagraph, IPyAMSLayer), provides=IParagraphPreview)
+@template_config(template='templates/keypoints-preview.pt', layer=IPyAMSLayer)
+class KeypointsParagraphPreview(BaseContentProvider):
+    """Key points paragraph preview"""
+
+    language = None
+
+    def update(self):
+        i18n = II18n(self.context)
+        if self.language:
+            for attr in ('body', ):
+                setattr(self, attr, i18n.get_attribute(attr, self.language, request=self.request))
+        else:
+            for attr in ('body', ):
+                setattr(self, attr, i18n.query_attribute(attr, request=self.request))
+
+    @property
+    def keypoints(self):
+        return (self.body or '').split('\n')
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/zmi/templates/keypoints-preview.pt	Fri Mar 02 11:22:56 2018 +0100
@@ -0,0 +1,8 @@
+<div class="margin-bottom-10"
+	 tal:define="keypoints view.keypoints"
+	 tal:condition="keypoints">
+	<ul class="inside">
+		<li tal:repeat="item keypoints"
+			tal:content="item">item</li>
+	</ul>
+</div>
\ No newline at end of file