Use new PyAMS helpers and classes
authorThierry Florac <thierry.florac@onf.fr>
Fri, 06 Apr 2018 12:27:58 +0200
changeset 524 6de7d0a3a77d
parent 523 193ad9281a1c
child 525 72e35444c6b2
Use new PyAMS helpers and classes
src/pyams_content/component/paragraph/zmi/__init__.py
src/pyams_content/component/paragraph/zmi/container.py
--- a/src/pyams_content/component/paragraph/zmi/__init__.py	Fri Apr 06 12:07:45 2018 +0200
+++ b/src/pyams_content/component/paragraph/zmi/__init__.py	Fri Apr 06 12:27:58 2018 +0200
@@ -133,16 +133,16 @@
             table_factory = ParagraphContainerTable
         else:
             table_factory = ParagraphContainerBaseTable
-        paragraphs_table = table_factory(self.context, self.request)
-        paragraphs_table.update()
+        table = table_factory(self.context, self.request)
+        table.update()
         return {'status': 'success',
                 'message': self.request.localizer.translate(_("Paragraph was correctly added.")),
                 'events': [{
                     'event': 'myams.refresh',
                     'options': {
-                        'handler': 'PyAMS_content.paragraphs.refreshParagraphs',
-                        'object_id': paragraphs_table.id,
-                        'table': paragraphs_table.render()
+                        'handler': 'MyAMS.skin.refreshSwitchedTable',
+                        'object_id': table.id,
+                        'table': table.render()
                     }
                 }]}
 
--- a/src/pyams_content/component/paragraph/zmi/container.py	Fri Apr 06 12:07:45 2018 +0200
+++ b/src/pyams_content/component/paragraph/zmi/container.py	Fri Apr 06 12:27:58 2018 +0200
@@ -40,9 +40,11 @@
 from pyams_content.shared.common.zmi import WfModifiedContentColumnMixin
 from pyams_content.skin import pyams_content
 from pyams_form.security import ProtectedFormObjectMixin
-from pyams_pagelet.pagelet import pagelet_config, Pagelet
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.container import switch_element_visibility
 from pyams_skin.page import DefaultPageHeaderAdapter
-from pyams_skin.table import BaseTable, I18nColumn, TrashColumn, JsActionColumn, SorterColumn, ImageColumn
+from pyams_skin.table import BaseTable, I18nColumn, TrashColumn, SorterColumn, ImageColumn, \
+    VisibilitySwitcherColumn
 from pyams_skin.viewlet.menu import MenuItem
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter, NullAdapter
@@ -50,12 +52,13 @@
 from pyams_utils.traversing import get_parent
 from pyams_utils.url import absolute_url
 from pyams_viewlet.manager import viewletmanager_config, WeightOrderedViewletManager, TemplateBasedViewletManager
-from pyramid.exceptions import NotFound
-from pyramid.view import view_config
 from pyams_viewlet.viewlet import viewlet_config, Viewlet
 from pyams_zmi.form import AdminDialogDisplayForm
 from pyams_zmi.view import AdminView, ContainerAdminView
+from pyams_zmi.zmi.table import InnerTableView
 from pyramid.decorator import reify
+from pyramid.exceptions import NotFound
+from pyramid.view import view_config
 from z3c.form import field
 from z3c.table.column import GetAttrColumn
 from zope.interface import implementer, Interface
@@ -104,7 +107,8 @@
             'data-ams-pre-reload': 'PyAMS_content.paragraphs.preReload',
             'data-ams-post-reload': 'PyAMS_content.paragraphs.postReload',
             'data-ams-tablednd-drag-handle': 'td.sorter',
-            'data-ams-tablednd-drop-target': 'set-paragraphs-order.json'
+            'data-ams-tablednd-drop-target': 'set-paragraphs-order.json',
+            'data-ams-visibility-target': 'switch-paragraph-visibility.json'
         })
         return attributes
 
@@ -151,32 +155,26 @@
     """Paragraphs container sorter column"""
 
 
+@view_config(name='set-paragraphs-order.json', context=IParagraphContainer, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+def set_paragraphs_order(request):
+    """Update paragraphs order"""
+    order = list(map(str, json.loads(request.params.get('names'))))
+    request.context.updateOrder(order)
+    return {'status': 'success'}
+
+
 @adapter_config(name='show-hide', context=(IParagraphContainerTarget, IPyAMSLayer, ParagraphContainerBaseTable),
                 provides=IColumn)
-class ParagraphContainerShowHideColumn(ProtectedFormObjectMixin, JsActionColumn):
+class ParagraphContainerShowHideColumn(ProtectedFormObjectMixin, VisibilitySwitcherColumn):
     """Paragraphs container visibility switcher column"""
 
-    cssClasses = {'th': 'action',
-                  'td': 'action switcher'}
 
-    icon_class = 'fa fa-fw fa-eye'
-    icon_hint = _("Switch paragraph visibility")
-
-    url = 'PyAMS_content.paragraphs.switchVisibility'
-
-    weight = 5
-
-    def get_icon_class(self, item):
-        if item.visible:
-            return self.icon_class
-        else:
-            return 'fa fa-fw fa-eye-slash text-danger'
-
-    def renderCell(self, item):
-        if self.permission and not self.request.has_permission(self.permission, context=item):
-            return self.get_icon(item)
-        else:
-            return super(ParagraphContainerShowHideColumn, self).renderCell(item)
+@view_config(name='switch-paragraph-visibility.json', context=IParagraphContainer, request_type=IPyAMSLayer,
+             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+def switch_paragraph_visibility(request):
+    """Switch paragraph visibility"""
+    return switch_element_visibility(request, IParagraphContainer)
 
 
 @adapter_config(context=ParagraphContainerShowHideColumn, provides=IFormSecurityContext)
@@ -302,21 +300,14 @@
 
 @adapter_config(name='paragraphs', context=(IParagraphContainerTarget, IPyAMSLayer, ParagraphContainerDialogView),
                 provides=IInnerSubForm)
-@template_config(template='templates/paragraphs.pt', layer=IPyAMSLayer)
 @implementer(IParagraphContainerView)
-class ParagraphsView(Pagelet):
+class ParagraphsView(InnerTableView):
     """Paragraphs view"""
 
-    weight = 90
+    title = _("Paragraphs")
 
-    def __init__(self, context, request, view):
-        super(ParagraphsView, self).__init__(context, request)
-        self.table = ParagraphContainerBaseTable(context, request)
-        self.table.view = self
-
-    def update(self):
-        super(ParagraphsView, self).update()
-        self.table.update()
+    table_class = ParagraphContainerBaseTable
+    weight = 90
 
 
 @adapter_config(context=(IParagraphContainerTarget, IPyAMSLayer, ParagraphContainerView), provides=IPageHeader)
@@ -327,32 +318,10 @@
     icon_class = 'fa fa-fw fa-paragraph'
 
 
-@view_config(name='set-paragraphs-order.json', context=IParagraphContainer, request_type=IPyAMSLayer,
-             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
-def set_paragraphs_order(request):
-    """Update paragraphs order"""
-    order = list(map(str, json.loads(request.params.get('names'))))
-    request.context.updateOrder(order)
-    return {'status': 'success'}
-
-
-@view_config(name='set-paragraph-visibility.json', context=IParagraphContainer, request_type=IPyAMSLayer,
-             permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
-def set_paragraph_visibility(request):
-    """Set paragraph visibility"""
-    container = IParagraphContainer(request.context)
-    paragraph = container.get(str(request.params.get('object_name')))
-    if paragraph is None:
-        raise NotFound()
-    paragraph = IBaseParagraph(paragraph)
-    paragraph.visible = not paragraph.visible
-    return {'visible': paragraph.visible}
-
-
 @view_config(name='get-paragraph-editor.json', context=IParagraphContainer, request_type=IPyAMSLayer,
              permission=VIEW_SYSTEM_PERMISSION, renderer='json', xhr=True)
 def get_paragraph_editor(request):
-    """Get paragraph editor"""
+    """Get inner editor for a given paragraph"""
     container = IParagraphContainer(request.context)
     paragraph = container.get(str(request.params.get('object_name')))
     if paragraph is None: