Added base paragraph edit form
authorThierry Florac <thierry.florac@onf.fr>
Fri, 10 Nov 2017 11:49:07 +0100
changeset 242 2bbead234b84
parent 241 50452584f7ae
child 243 73b31fa70b8b
Added base paragraph edit form
src/pyams_content/component/paragraph/zmi/__init__.py
--- a/src/pyams_content/component/paragraph/zmi/__init__.py	Fri Nov 10 11:47:59 2017 +0100
+++ b/src/pyams_content/component/paragraph/zmi/__init__.py	Fri Nov 10 11:49:07 2017 +0100
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyramid.location import lineage
 
 __docformat__ = 'restructuredtext'
 
@@ -18,13 +19,16 @@
 # import interfaces
 from pyams_content.component.paragraph.interfaces import IBaseParagraph, IParagraphFactorySettings
 from pyams_content.interfaces import MANAGE_TOOL_PERMISSION
+from pyams_content.shared.common.interfaces import IWfSharedContent
 from pyams_form.interfaces.form import IFormHelp
 from pyams_i18n.interfaces import II18n
+from pyams_skin.interfaces.container import ITableElementName
 from pyams_skin.layer import IPyAMSLayer
 from pyams_zmi.interfaces.menu import IPropertiesMenu
 from pyams_zmi.layer import IAdminLayer
 
 # import packages
+from pyams_content.component.paragraph.zmi.container import ParagraphContainerTable, ParagraphContainerBaseTable
 from pyams_form.form import AJAXEditForm, AJAXAddForm
 from pyams_form.help import FormHelp
 from pyams_form.security import ProtectedFormObjectMixin
@@ -96,7 +100,7 @@
 
     def __new__(cls, context, request, view, manager):
         settings = get_parent(context, IParagraphFactorySettings)
-        if (settings is not None) and (cls.paragraph_type not in settings.allowed_paragraphs):
+        if (settings is not None) and (cls.paragraph_type not in (settings.allowed_paragraphs or ())):
             return None
         return ToolbarMenuItem.__new__(cls)
 
@@ -105,8 +109,38 @@
     """Base paragraph AJAX add form"""
 
     def get_ajax_output(self, changes):
-        return {'status': 'reload',
-                'location': '#paragraphs.html'}
+        if IWfSharedContent.providedBy(self.context):
+            table_factory = ParagraphContainerTable
+        else:
+            table_factory = ParagraphContainerBaseTable
+        paragraphs_table = table_factory(self.context, self.request)
+        paragraphs_table.update()
+        return {'status': 'success',
+                'message': self.request.localizer.translate(_("Paragraph was correctly added.")),
+                'events': [{
+                    'event': 'PyAMS_content.changed_item',
+                    'options': {
+                        'handler': 'PyAMS_content.paragraphs.refreshParagraphs',
+                        'object_name': paragraphs_table.id,
+                        'table': paragraphs_table.render()
+                    }
+                }]}
+
+
+class BaseParagraphPropertiesEditForm(AdminDialogEditForm):
+    """Base paragraph edit form"""
+
+    @property
+    def title(self):
+        registry = self.request.registry
+        for parent in lineage(self.context):
+            adapter = registry.queryMultiAdapter((parent, self.request), ITableElementName)
+            if adapter is not None:
+                return adapter.name
+        content = get_parent(self.context, IWfSharedContent)
+        if content is not None:
+            return II18n(content).query_attribute('title', request=self.request)
+        return super(BaseParagraphPropertiesEditForm, self).title
 
 
 class BaseParagraphAJAXEditForm(AJAXEditForm):