Use new PyAMS helpers and classes
authorThierry Florac <thierry.florac@onf.fr>
Fri, 06 Apr 2018 11:54:25 +0200
changeset 522 73e404a01d95
parent 521 c9e046041e8f
child 523 193ad9281a1c
child 646 2087ddaca9e2
Use new PyAMS helpers and classes
src/pyams_content/component/association/zmi/__init__.py
src/pyams_content/component/association/zmi/templates/associations.pt
--- a/src/pyams_content/component/association/zmi/__init__.py	Fri Apr 06 08:55:26 2018 +0200
+++ b/src/pyams_content/component/association/zmi/__init__.py	Fri Apr 06 11:54:25 2018 +0200
@@ -20,7 +20,6 @@
 from pyams_content.component.association.interfaces import IAssociationTarget, IAssociationContainer, IAssociationInfo
 from pyams_content.component.association.zmi.interfaces import IAssociationsParentForm, IAssociationsView
 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
-from pyams_content.skin import pyams_content
 from pyams_form.interfaces.form import IInnerSubForm
 from pyams_skin.layer import IPyAMSLayer
 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_PERMISSION
@@ -31,18 +30,18 @@
 from pyams_form.form import AJAXAddForm, AJAXEditForm
 from pyams_form.security import ProtectedFormObjectMixin
 from pyams_pagelet.pagelet import pagelet_config, Pagelet
-from pyams_skin.table import BaseTable, SorterColumn, JsActionColumn, NameColumn, ImageColumn, I18nColumn, TrashColumn
+from pyams_skin.table import BaseTable, SorterColumn, NameColumn, ImageColumn, I18nColumn, TrashColumn, \
+    VisibilitySwitcherColumn
+from pyams_skin.container import switch_element_visibility
 from pyams_skin.viewlet.menu import MenuItem
-from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
-from pyams_utils.fanstatic import get_resource_path
 from pyams_utils.traversing import get_parent
 from pyams_utils.url import absolute_url
 from pyams_viewlet.viewlet import viewlet_config
-from pyams_zmi.form import InnerAdminDisplayForm, AdminDialogDisplayForm
+from pyams_zmi.form import AdminDialogDisplayForm
 from pyams_zmi.view import 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
@@ -59,18 +58,20 @@
     """Association item add form, JSON renderer"""
 
     def get_ajax_output(self, changes):
-        associations_table = AssociationsTable(self.context, self.request)
-        associations_table.update()
-        return {'status': 'success',
-                'message': self.request.localizer.translate(_("Association was correctly added.")),
-                'events': [{
-                    'event': 'myams.refresh',
-                    'options': {
-                        'handler': 'PyAMS_content.associations.refreshAssociations',
-                        'object_id': associations_table.id,
-                        'table': associations_table.render()
-                    }
-                }]}
+        table = AssociationsTable(self.context, self.request)
+        table.update()
+        return {
+            'status': 'success',
+            'message': self.request.localizer.translate(_("Association was correctly added.")),
+            'events': [{
+                'event': 'myams.refresh',
+                'options': {
+                    'handler': 'MyAMS.skin.refreshSwitchedTable',
+                    'object_id': table.id,
+                    'table': table.render()
+                }
+            }]
+        }
 
 
 class AssociationItemAJAXEditForm(AJAXEditForm):
@@ -78,18 +79,20 @@
 
     def get_associations_table(self):
         target = get_parent(self.context, IAssociationTarget)
-        associations_table = AssociationsTable(target, self.request)
-        associations_table.update()
-        return {'status': 'success',
-                'message': self.request.localizer.translate(self.successMessage),
-                'events': [{
-                    'event': 'myams.refresh',
-                    'options': {
-                        'handler': 'PyAMS_content.associations.refreshAssociations',
-                        'object_id': associations_table.id,
-                        'table': associations_table.render()
-                    }
-                }]}
+        table = AssociationsTable(target, self.request)
+        table.update()
+        return {
+            'status': 'success',
+            'message': self.request.localizer.translate(self.successMessage),
+            'events': [{
+                'event': 'myams.refresh',
+                'options': {
+                    'handler': 'MyAMS.skin.refreshSwitchedTable',
+                    'object_id': table.id,
+                    'table': table.render()
+                }
+            }]
+        }
 
 
 #
@@ -127,13 +130,16 @@
     @property
     def data_attributes(self):
         attributes = super(AssociationsTable, self).data_attributes
-        attributes['table'] = {'id': self.id,
-                               'data-ams-plugins': 'pyams_content',
-                               'data-ams-plugin-pyams_content-src': get_resource_path(pyams_content),
-                               'data-ams-location': absolute_url(IAssociationContainer(self.context), self.request),
-                               'data-ams-tablednd-drag-handle': 'td.sorter',
-                               'data-ams-tablednd-drop-target': 'set-associations-order.json'}
-        attributes.setdefault('tr', {}).setdefault('data-ams-delete-target', 'delete-association.json')
+        attributes['table'] = {
+            'id': self.id,
+            'data-ams-location': absolute_url(IAssociationContainer(self.context), self.request),
+            'data-ams-tablednd-drag-handle': 'td.sorter',
+            'data-ams-tablednd-drop-target': 'set-associations-order.json',
+            'data-ams-visibility-target': 'switch-association-visibility.json'
+        }
+        attributes.setdefault('tr', {}).update({
+            'data-ams-delete-target': 'delete-association.json'
+        })
         return attributes
 
     @reify
@@ -166,43 +172,15 @@
 
 @adapter_config(name='show-hide', context=(IAssociationTarget, IPyAMSLayer, AssociationsTable),
                 provides=IColumn)
-class AssociationsTableShowHideColumn(ProtectedFormObjectMixin, JsActionColumn):
+class AssociationsTableShowHideColumn(ProtectedFormObjectMixin, VisibilitySwitcherColumn):
     """Associations container visibility switcher column"""
 
-    cssClasses = {'th': 'action',
-                  'td': 'action switcher'}
 
-    icon_class = 'fa fa-fw fa-eye'
-    icon_hint = _("Switch association visibility")
-
-    url = 'PyAMS_content.associations.switchVisibility'
-
-    weight = 5
-
-    def get_icon(self, item):
-        if item.visible:
-            icon_class = 'fa fa-fw fa-eye'
-        else:
-            icon_class = 'fa fa-fw fa-eye-slash text-danger'
-        return '<i class="{icon_class}"></i>'.format(icon_class=icon_class)
-
-    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(AssociationsTableShowHideColumn, self).renderCell(item)
-
-
-@view_config(name='set-association-visibility.json', context=IAssociationContainer, request_type=IPyAMSLayer,
+@view_config(name='switch-association-visibility.json', context=IAssociationContainer, request_type=IPyAMSLayer,
              permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
 def set_association_visibility(request):
     """Set association visibility"""
-    container = IAssociationContainer(request.context)
-    association = container.get(str(request.params.get('object_name')))
-    if association is None:
-        raise NotFound()
-    association.visible = not association.visible
-    return {'visible': association.visible}
+    return switch_element_visibility(request, IAssociationContainer)
 
 
 @adapter_config(name='pictogram', context=(IAssociationTarget, IPyAMSLayer, AssociationsTable), provides=IColumn)
@@ -283,26 +261,38 @@
     translate = request.localizer.translate
     name = request.params.get('object_name')
     if not name:
-        return {'status': 'message',
-                'messagebox': {'status': 'error',
-                               'content': translate(_("No provided object_name argument!"))}}
+        return {
+            'status': 'message',
+            'messagebox': {
+                'status': 'error',
+                'content': translate(_("No provided object_name argument!"))
+            }
+        }
     if name not in request.context:
-        return {'status': 'message',
-                'messagebox': {'status': 'error',
-                               'content': translate(_("Given association name doesn't exist!"))}}
+        return {
+            'status': 'message',
+            'messagebox': {
+                'status': 'error',
+                'content': translate(_("Given association name doesn't exist!"))
+            }
+        }
     del request.context[name]
     parent = get_parent(request.context, IAssociationTarget)
     table = ParagraphContainerTable(parent, request)
     viewlet = ParagraphTitleToolbarViewletManager(parent, request, table)
     viewlet.update()
-    return {'status': 'success',
-            'handle_json': True,
-            'events': [{
-                'event': 'myams.refresh',
-                'options': {'handler': 'PyAMS_content.paragraphs.updateToolbar',
-                            'object_name': parent.__name__,
-                            'toolbar_tag': viewlet.render()}
-            }]}
+    return {
+        'status': 'success',
+        'handle_json': True,
+        'events': [{
+            'event': 'myams.refresh',
+            'options': {
+                'handler': 'PyAMS_content.paragraphs.updateToolbar',
+                'object_name': parent.__name__,
+                'toolbar_tag': viewlet.render()
+            }
+        }]
+    }
 
 
 @pagelet_config(name='associations.html', context=IAssociationTarget, layer=IPyAMSLayer,
@@ -330,19 +320,11 @@
 
 @adapter_config(name='associations', context=(IAssociationTarget, IPyAMSLayer, IAssociationsParentForm),
                 provides=IInnerSubForm)
-@template_config(template='templates/associations.pt', layer=IPyAMSLayer)
 @implementer(IAssociationsView)
-class AssociationsView(InnerAdminDisplayForm):
+class AssociationsView(InnerTableView):
     """Associations view"""
 
-    fields = field.Fields(Interface)
-    weight = 90
+    title = _("Associations")
 
-    def __init__(self, context, request, view):
-        super(AssociationsView, self).__init__(context, request, view)
-        self.table = AssociationsTable(context, request)
-        self.table.view = self
-
-    def update(self):
-        super(AssociationsView, self).update()
-        self.table.update()
+    table_class = AssociationsTable
+    weight = 90
--- a/src/pyams_content/component/association/zmi/templates/associations.pt	Fri Apr 06 08:55:26 2018 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-<div class="form-group" i18n:domain="pyams_content">
-	<fieldset class="margin-top-10 padding-top-5 padding-bottom-0">
-		<legend
-			class="inner switcher margin-bottom-5 padding-right-10 no-y-padding pull-left width-auto"
-			tal:attributes="data-ams-switcher-state 'open' if view.table.values else None">
-			<i18n:var translate="">Associations</i18n:var>
-		</legend>
-		<div class="pull-left persistent">
-			<tal:var content="structure provider:pyams.widget_title" />
-		</div>
-		<div class="clearfix"></div>
-		<tal:var content="structure view.table.render()" />
-	</fieldset>
-</div>