Refactored tables attributes management
authorThierry Florac <thierry.florac@onf.fr>
Tue, 10 Apr 2018 15:23:45 +0200
changeset 304 9076b2adce67
parent 303 ec53de7f90e3
child 305 1235a0c99263
Refactored tables attributes management
src/pyams_skin/interfaces/container.py
src/pyams_skin/table.py
--- a/src/pyams_skin/interfaces/container.py	Fri Apr 06 23:59:59 2018 +0200
+++ b/src/pyams_skin/interfaces/container.py	Tue Apr 10 15:23:45 2018 +0200
@@ -34,6 +34,8 @@
 class ITable(Interface):
     """Base table interface"""
 
+    prefix = TextLine(title="Table prefix")
+
     id = TextLine(title="Table ID")
 
     title = TextLine(title="Table title")
--- a/src/pyams_skin/table.py	Fri Apr 06 23:59:59 2018 +0200
+++ b/src/pyams_skin/table.py	Tue Apr 10 15:23:45 2018 +0200
@@ -19,7 +19,7 @@
 from pyams_i18n.interfaces import II18n
 from pyams_skin.interfaces.container import ITable, ITableElementEditor, ITableWithActions, ITableElementName
 from pyams_skin.layer import IPyAMSLayer
-from pyams_utils.interfaces import MANAGE_PERMISSION
+from pyams_utils.interfaces import MANAGE_PERMISSION, ICacheKeyValue
 from pyams_viewlet.interfaces import IViewletManager
 from z3c.table.interfaces import IColumn, IValues
 from zope.container.interfaces import IContained, IContainer
@@ -50,6 +50,23 @@
     modal_target = True
 
 
+def get_table_id(table, context=None):
+    """Get ID for selected table"""
+    if context is None:
+        context = table.context
+    return '{0}_{1}'.format(table.prefix, ICacheKeyValue(context))
+
+
+def get_element_id(table, element, table_context=None):
+    """Get ID for selected element"""
+    return '{0}::{1}'.format(get_table_id(table, table_context), ICacheKeyValue(element))
+
+
+def get_element_name(element):
+    """Get name for selected element"""
+    return getattr(element, '__name__', None)
+
+
 def get_element_editor(table, element):
     """Get editor for selected element"""
     registry = table.request.registry
@@ -61,7 +78,10 @@
 class BaseTable(Table):
     """Custom table"""
 
-    id = "TableID"
+    @property
+    def id(self):
+        return get_table_id(self, self.context)
+
     title = _("Container elements")
 
     cssClasses = {'table': 'table table-bordered table-striped table-hover table-tight datatable'}
@@ -70,8 +90,8 @@
     def data_attributes(self):
         return {
             'tr': {
-                'id': lambda x, col: '{0}::{1}'.format(self.id, getattr(x, '__name__', '')),
-                'data-ams-element-name': lambda x, col: getattr(x, '__name__', None),
+                'id': lambda x, col: get_element_id(self, x),
+                'data-ams-element-name': lambda x, col: get_element_name(x),
                 'data-ams-url': lambda x, col: getattr(get_element_editor(self, x), 'url', None),
                 'data-toggle': lambda x, col: 'modal' if getattr(get_element_editor(self, x), 'modal_target',
                                                                  None) else None
@@ -310,6 +330,7 @@
     cssClasses = {'th': 'action',
                   'td': 'action switcher'}
 
+    switch_attribute = 'visible'
     visible_icon_class = 'fa fa-fw fa-eye'
     hidden_icon_class = 'fa fa-fw fa-eye-slash text-danger'
 
@@ -319,7 +340,7 @@
     weight = 5
 
     def get_icon(self, item):
-        if item.visible:
+        if getattr(item, self.switch_attribute):
             icon_class = self.visible_icon_class
         else:
             icon_class = self.hidden_icon_class