Use default container delete helper
authorThierry Florac <thierry.florac@onf.fr>
Wed, 11 Apr 2018 12:14:12 +0200
changeset 61 2ae762f667cb
parent 60 87b52b69492d
child 62 2ed574c8fcd1
Use default container delete helper
src/pyams_zmi/zmi/control_panel.py
--- a/src/pyams_zmi/zmi/control_panel.py	Thu Apr 05 23:03:58 2018 +0200
+++ b/src/pyams_zmi/zmi/control_panel.py	Wed Apr 11 12:14:12 2018 +0200
@@ -29,7 +29,7 @@
 
 # import packages
 from pyams_pagelet.pagelet import pagelet_config
-from pyams_skin.container import ContainerView
+from pyams_skin.container import ContainerView, delete_container_element
 from pyams_skin.help import ContentHelp
 from pyams_skin.page import DefaultPageHeaderAdapter
 from pyams_skin.table import BaseTable, TrashColumn
@@ -74,8 +74,10 @@
     @property
     def data_attributes(self):
         attributes = super(UtilitiesTable, self).data_attributes
-        attributes['table'] = {'data-ams-location': absolute_url(self.context, self.request),
-                               'data-ams-delete-target': 'delete-utility.json'}
+        attributes['table'] = {
+            'data-ams-location': absolute_url(self.context, self.request),
+            'data-ams-delete-target': 'delete-utility.json'
+        }
         return attributes
 
 
@@ -89,6 +91,13 @@
     checker = lambda col, x: IOptionalUtility.providedBy(x)
 
 
+@view_config(name='delete-utility.json', context=ISite, request_type=IPyAMSLayer,
+             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
+def delete_utility(request):
+    """Delete utility from site manager"""
+    return delete_container_element(request, lambda x: x.getSiteManager(), ignore_permission=True)
+
+
 @adapter_config(context=(ISite, IAdminLayer, UtilitiesTable), provides=IValues)
 class UtilitiesValuesAdapter(ContextRequestViewAdapter):
     """Utilities values adapter"""
@@ -116,25 +125,6 @@
     icon_class = 'fa fa-fw fa-codepen'
 
 
-@view_config(name='delete-utility.json', context=ISite, request_type=IPyAMSLayer,
-             permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
-def delete_utility(request):
-    """Delete utility from site manager"""
-    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!"))}}
-    manager = request.context.getSiteManager()
-    if name not in manager:
-        return {'status': 'message',
-                'messagebox': {'status': 'error',
-                               'content': translate(_("Given utility name doesn't exist!"))}}
-    del manager[name]
-    return {'status': 'success'}
-
-
 #
 # Common registrations views and adapters
 #