Updated table attributes and JSON events
authorThierry Florac <thierry.florac@onf.fr>
Tue, 10 Apr 2018 16:22:40 +0200
changeset 40 bc5b1fece177
parent 39 9c763bcf5463
child 41 d42af9cfbd97
Updated table attributes and JSON events
src/pyams_catalog/zmi/catalog.py
--- a/src/pyams_catalog/zmi/catalog.py	Tue Apr 10 16:20:36 2018 +0200
+++ b/src/pyams_catalog/zmi/catalog.py	Tue Apr 10 16:22:40 2018 +0200
@@ -19,13 +19,14 @@
 from hypatia.interfaces import ICatalog, IIndexStatistics, IIndexEnumeration
 from pyams_form.interfaces.form import IWidgetsSuffixViewletsManager
 from pyams_skin.layer import IPyAMSLayer
-from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_SYSTEM_PERMISSION
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION, MANAGE_SYSTEM_PERMISSION, ICacheKeyValue
 from pyams_zmi.layer import IAdminLayer
 from z3c.table.interfaces import IColumn, IValues
 
 # import packages
 from pyams_catalog.index import InterfaceSupportIndexMixin
 from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.container import delete_container_element
 from pyams_skin.table import BaseTable, I18nColumn, TrashColumn, TableViewlet
 from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
 from pyams_utils.text import text_to_html
@@ -57,7 +58,8 @@
 class CatalogIndexesTable(BaseTable):
     """Catalog indexes table"""
 
-    id = 'CatalogIndexes'
+    prefix = 'CatalogIndexes'
+
     title = _("Catalog indexes")
     cssClasses = {'table': 'table table-bordered table-striped table-hover table-tight datatable'}
     sortOn = None
@@ -65,17 +67,17 @@
     @property
     def data_attributes(self):
         attributes = super(CatalogIndexesTable, self).data_attributes
-        table_attrs = {'data-ams-location': absolute_url(self.context, self.request),
-                       'data-ams-delete-target': 'delete-index.json',
-                       'data-ams-datatable-global-filter': 'false',
-                       'data-ams-datatable-info': 'false',
-                       'data-ams-datatable-sdom': "t<'dt-row dt-bottom-row'<'text-right'p>>",
-                       'data-ams-datatable-display-length': '20',
-                       'data-ams-datatable-pagination-type': 'bootstrap_prevnext'}
-        if 'table' in attributes:
-            attributes['table'].update(table_attrs)
-        else:
-            attributes['table'] = table_attrs
+        attributes.setdefault('table', {}).update({
+            'data-ams-location': absolute_url(self.context, self.request),
+            'data-ams-datatable-global-filter': 'false',
+            'data-ams-datatable-info': 'false',
+            'data-ams-datatable-sdom': "t<'dt-row dt-bottom-row'<'text-right'p>>",
+            'data-ams-datatable-display-length': '20',
+            'data-ams-datatable-pagination-type': 'bootstrap_prevnext'
+        })
+        attributes.setdefault('tr', {}).update({
+            'id': lambda x, col: '{0}::{1}'.format(self.prefix, ICacheKeyValue(x))
+        })
         return attributes
 
 
@@ -217,19 +219,8 @@
     table_class = CatalogIndexesTable
 
 
-@view_config(name='delete-index.json', context=ICatalog, request_type=IPyAMSLayer,
+@view_config(name='delete-element.json', context=ICatalog, request_type=IPyAMSLayer,
              permission=MANAGE_SYSTEM_PERMISSION, renderer='json', xhr=True)
 def delete_catalog_index(request):
     """Delete index from catalog"""
-    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 index doesn't exist!"))}}
-    del request.context[name]
-    return {'status': 'success'}
+    return delete_container_element(request)