Formatting, adding prefix, and updated tables properties
authorThierry Florac <thierry.florac@onf.fr>
Wed, 11 Apr 2018 10:48:24 +0200
changeset 57 adc5232e19dd
parent 56 866affb740b3
child 58 c8a142f76c36
Formatting, adding prefix, and updated tables properties
src/pyams_thesaurus/zmi/extract.py
src/pyams_thesaurus/zmi/manager.py
src/pyams_thesaurus/zmi/term.py
src/pyams_thesaurus/zmi/thesaurus.py
--- a/src/pyams_thesaurus/zmi/extract.py	Wed Apr 11 10:32:30 2018 +0200
+++ b/src/pyams_thesaurus/zmi/extract.py	Wed Apr 11 10:48:24 2018 +0200
@@ -35,6 +35,7 @@
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_template.template import template_config
 from pyams_thesaurus.thesaurus import ThesaurusExtract
+from pyams_skin.container import delete_container_element
 from pyams_skin.table import BaseTable, TrashColumn, ActionColumn, JsActionColumn
 from pyams_skin.viewlet.toolbar import ToolbarAction
 from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
@@ -54,15 +55,16 @@
 class ThesaurusExtractsTable(BaseTable):
     """Thesaurus extracts table"""
 
-    id = 'thesaurus_extracts_table'
+    prefix = 'thesaurus_extracts'
+
     title = _("Thesaurus extracts")
 
     @property
     def data_attributes(self):
         attributes = super(ThesaurusExtractsTable, self).data_attributes
-        table = attributes.setdefault('table', {})
-        table.setdefault('data-ams-location', absolute_url(self.context, self.request, '++extracts++'))
-        table.setdefault('data-ams-delete-target', 'delete-extract.json')
+        attributes.setdefault('table', {}).update({
+            'data-ams-location': absolute_url(IThesaurusExtracts(self.context), self.request)
+        })
         return attributes
 
     cssClasses = {'table': 'table table-bordered table-striped table-hover table-tight datatable extracts'}
@@ -120,6 +122,13 @@
     permission = ADMIN_THESAURUS_PERMISSION
 
 
+@view_config(name='delete-element.json', context=IThesaurusExtracts, request_type=IPyAMSLayer,
+             permission=ADMIN_THESAURUS_PERMISSION, renderer='json', xhr=True)
+def delete_extract(request):
+    """Delete extract from thesaurus"""
+    return delete_container_element(request, ignore_permission=True)
+
+
 @viewlet_config(name='thesaurus.extract.adding', context=IThesaurus, view=ThesaurusExtractsTable,
                 layer=IAdminLayer, manager=IWidgetTitleViewletManager, permission=ADMIN_THESAURUS_PERMISSION)
 class ThesaurusExtractAddAction(ToolbarAction):
@@ -177,6 +186,8 @@
 class ThesaurusExtractEditForm(AdminDialogEditForm):
     """Thesaurus extract edit form"""
 
+    prefix = 'extract_properties.'
+
     @property
     def title(self):
         translate = self.request.localizer.translate
@@ -259,11 +270,13 @@
         for child in INode(term).get_children():
             node = INode(child)
             if extract_name in (node.context.extracts or ()):
-                result.append({'label': node.label.replace("'", "&#039;"),
-                               'view': absolute_url(node.context, self.request, 'properties.html'),
-                               'css_class': node.css_class,
-                               'extracts': [],
-                               'expand': node.has_children(extract_name)})
+                result.append({
+                    'label': node.label.replace("'", "&#039;"),
+                    'view': absolute_url(node.context, self.request, 'properties.html'),
+                    'css_class': node.css_class,
+                    'extracts': [],
+                    'expand': node.has_children(extract_name)
+                })
 
 
 @view_config(name='get-nodes.json', context=IThesaurusExtract, request_type=IPyAMSLayer,
@@ -280,24 +293,7 @@
             raise NotFound
         result = []
         self.get_nodes(term, result)
-        return {'term': label,
-                'nodes': sorted(result, key=lambda x: x['label'])}
-
-
-@view_config(name='delete-extract.json', context=IThesaurusExtracts, request_type=IPyAMSLayer,
-             permission=ADMIN_THESAURUS_PERMISSION, renderer='json', xhr=True)
-def delete_extract(request):
-    """Delete extract from thesaurus"""
-    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!"))}}
-    extracts = IThesaurusExtracts(request.context)
-    if name not in extracts:
-        return {'status': 'message',
-                'messagebox': {'status': 'error',
-                               'content': translate(_("Given extract name doesn't exist!"))}}
-    del extracts[name]
-    return {'status': 'success'}
+        return {
+            'term': label,
+            'nodes': sorted(result, key=lambda x: x['label'])
+        }
--- a/src/pyams_thesaurus/zmi/manager.py	Wed Apr 11 10:32:30 2018 +0200
+++ b/src/pyams_thesaurus/zmi/manager.py	Wed Apr 11 10:48:24 2018 +0200
@@ -74,11 +74,9 @@
     def data_attributes(self):
         manager = self.context.getSiteManager()
         attributes = super(ControlPanelThesaurusTable, self).data_attributes
-        table_attrs = {'data-ams-location': absolute_url(manager, self.request)}
-        if 'table' in attributes:
-            attributes['table'].update(table_attrs)
-        else:
-            attributes['table'] = table_attrs
+        attributes.setdefault('table', {}).update({
+            'data-ams-location': absolute_url(manager, self.request)
+        })
         return attributes
 
 
@@ -87,7 +85,7 @@
     """Thesaurus trash column"""
 
     icon_hint = _("Delete thesaurus")
-    permission = MANAGE_SYSTEM_PERMISSION
+    permission = ADMIN_THESAURUS_PERMISSION
 
 
 @adapter_config(context=(ISite, IAdminLayer, ControlPanelThesaurusTable), provides=IValues)
--- a/src/pyams_thesaurus/zmi/term.py	Wed Apr 11 10:32:30 2018 +0200
+++ b/src/pyams_thesaurus/zmi/term.py	Wed Apr 11 10:48:24 2018 +0200
@@ -119,15 +119,21 @@
             return super(ThesaurusTermAJAXAddForm, self).get_ajax_output(changes)
         else:
             label = changes.label.replace("'", "&#039;")
-            return {'status': 'callback',
-                    'callback': 'PyAMS_thesaurus.tree.findTerm',
-                    'options': {'term': label}}
+            return {
+                'status': 'callback',
+                'callback': 'PyAMS_thesaurus.tree.findTerm',
+                'options': {
+                    'term': label
+                }
+            }
 
 
 @pagelet_config(name='properties.html', context=IThesaurusTerm, layer=IPyAMSLayer, permission=VIEW_PERMISSION)
 class ThesaurusTermEditForm(AdminDialogEditForm):
     """Thesaurus term edit form"""
 
+    prefix = 'term_properties.'
+
     @property
     def title(self):
         return self.context.label
@@ -246,15 +252,23 @@
     def get_ajax_output(self, changes):
         if self.generic_changed:
             label = self.context.label.replace("'", "&#039;")
-            return {'status': 'reload',
-                    'callback': 'PyAMS_thesaurus.tree.findTerm',
-                    'options': {'term': label}}
+            return {
+                'status': 'reload',
+                'callback': 'PyAMS_thesaurus.tree.findTerm',
+                'options': {
+                    'term': label
+                }
+            }
         else:
             term_changes = changes.get(IThesaurusTerm, ())
             if ('status' in term_changes) or ('label' in term_changes):
                 label = (self.context.generic or self.context).label.replace("'", "&#039;")
-                return {'status': 'callback',
-                        'callback': 'PyAMS_thesaurus.tree.updateTerm',
-                        'options': {'term': label}}
+                return {
+                    'status': 'callback',
+                    'callback': 'PyAMS_thesaurus.tree.updateTerm',
+                    'options': {
+                        'term': label
+                    }
+                }
             else:
                 return super(ThesaurusTermAJAXEditForm, self).get_ajax_output(changes)
--- a/src/pyams_thesaurus/zmi/thesaurus.py	Wed Apr 11 10:32:30 2018 +0200
+++ b/src/pyams_thesaurus/zmi/thesaurus.py	Wed Apr 11 10:48:24 2018 +0200
@@ -159,9 +159,11 @@
     """Thesaurus add form, AJAX view"""
 
     def get_ajax_output(self, changes):
-        return {'status': 'redirect',
-                'location': absolute_url(self.context, self.request, 'admin#thesaurus.html'),
-                'close_form': True}
+        return {
+            'status': 'redirect',
+            'location': absolute_url(self.context, self.request, 'admin#thesaurus.html'),
+            'close_form': True
+        }
 
 
 @adapter_config(context=(IThesaurus, IAdminLayer, Interface), provides=ITableElementEditor)