Added custom view for association deletion
authortflorac@xsup98-004.onf.fr
Thu, 12 Oct 2017 22:25:19 +0200
changeset 199 d82d8670735e
parent 198 4d90e2502e5d
child 200 af969d925a91
Added custom view for association deletion
src/pyams_content/component/association/zmi/__init__.py
--- a/src/pyams_content/component/association/zmi/__init__.py	Thu Oct 12 22:24:23 2017 +0200
+++ b/src/pyams_content/component/association/zmi/__init__.py	Thu Oct 12 22:25:19 2017 +0200
@@ -22,7 +22,7 @@
 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
 from pyams_form.interfaces.form import IInnerSubForm
 from pyams_skin.layer import IPyAMSLayer
-from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_PERMISSION
 from pyams_zmi.interfaces.menu import IPropertiesMenu
 from z3c.table.interfaces import IValues, IColumn
 
@@ -63,7 +63,7 @@
                 'message': self.request.localizer.translate(_("Association was correctly added.")),
                 'events': [{
                     'event': 'PyAMS_content.changed_item',
-                    'options': {'object_type': 'associations',
+                    'options': {'handler': 'PyAMS_content.associations.refreshAssociations',
                                 'object_name': associations_table.id,
                                 'table': associations_table.render()}
                 }]}
@@ -80,7 +80,7 @@
                 'message': self.request.localizer.translate(self.successMessage),
                 'events': [{
                     'event': 'PyAMS_content.changed_item',
-                    'options': {'object_type': 'associations',
+                    'options': {'handler': 'PyAMS_content.associations.refreshAssociations',
                                 'object_name': associations_table.id,
                                 'table': associations_table.render()}
                 }]}
@@ -128,6 +128,7 @@
                                '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');
         return attributes
 
     @reify
@@ -263,6 +264,39 @@
     """Associations table trash column"""
 
 
+@view_config(name='delete-association.json', context=IAssociationContainer, request_type=IPyAMSLayer,
+             permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+def delete_association(request):
+    """Delete association"""
+
+    from pyams_content.component.paragraph.zmi.container \
+        import ParagraphContainerTable, ParagraphTitleToolbarViewletManager
+
+    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!"))}}
+    if name not in request.context:
+        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': 'PyAMS_content.changed_item',
+                'options': {'handler': 'PyAMS_content.paragraphs.updateToolbar',
+                            'object_name': parent.__name__,
+                            'toolbar_tag': viewlet.render()}
+            }]}
+
+
 @pagelet_config(name='associations.html', context=IAssociationTarget, layer=IPyAMSLayer,
                 permission=VIEW_SYSTEM_PERMISSION)
 @implementer(IAssociationsView)