Updated site tree sort
authorThierry Florac <thierry.florac@onf.fr>
Fri, 26 Jan 2018 16:41:37 +0100
changeset 335 2cce5756560f
parent 334 6b2f75e60076
child 336 d9f37252a42d
Updated site tree sort
src/pyams_content/shared/site/zmi/container.py
--- a/src/pyams_content/shared/site/zmi/container.py	Fri Jan 26 16:39:26 2018 +0100
+++ b/src/pyams_content/shared/site/zmi/container.py	Fri Jan 26 16:41:37 2018 +0100
@@ -21,13 +21,14 @@
 from pyams_content.shared.common.interfaces import ISharedContent
 from pyams_content.shared.common.interfaces.zmi import IDashboardTable
 from pyams_content.shared.site.interfaces import ISiteContainer, ISiteManager, IContentLink
-from pyams_content.zmi.interfaces import IUserAddingsMenuLabel, ISiteTreeMenu, ISiteTreeTable
+from pyams_content.skin.zmi.interfaces import ISiteTreeMenu, ISiteTreeTable, IUserAddingsMenuLabel
 from pyams_i18n.interfaces import II18n
 from pyams_skin.interfaces import IInnerPage, IPageHeader
 from pyams_skin.interfaces.container import ITableElementEditor, ITableElementName, ITableWithActions
 from pyams_skin.interfaces.viewlet import IBreadcrumbItem, ITableItemColumnActionsMenu
 from pyams_skin.layer import IPyAMSLayer
 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
+from pyams_utils.interfaces.traversing import IPathElements
 from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowPublicationInfo
 from pyams_zmi.interfaces.menu import ISiteManagementMenu, IPropertiesMenu
 from pyams_zmi.layer import IAdminLayer
@@ -62,6 +63,7 @@
 from pyramid.location import lineage
 from pyramid.view import view_config
 from z3c.form import field
+from zope.copy import copy
 from zope.interface import implementer
 from zope.lifecycleevent import ObjectMovedEvent
 
@@ -499,10 +501,24 @@
 def set_site_order(request):
     """Set site elements order"""
     intids = get_utility(IIntIds)
-    new_parent = intids.queryObject(int(request.params.get('parent')))
+    parent_oid = int(request.params.get('parent'))
+    new_parent = intids.queryObject(parent_oid)
     # check for changing parent
     if request.params.get('action') == 'reparent':
-        child = intids.queryObject(int(request.params.get('child')))
+        child_oid = int(request.params.get('child'))
+        child = intids.queryObject(child_oid)
+        # check if new parent is not a previous child
+        parent_path = IPathElements(new_parent)
+        if child_oid in parent_path.parents:
+            return {
+                'status': 'reload',
+                'smallbox': {
+                    'status': 'error',
+                    'message': request.localizer.translate(_("Can't reparent object to one of it's children. "
+                                                             "Reloading...")),
+                    'timeout': 5000
+                }
+            }
         old_parent = child.__parent__
         new_name = old_name = child.__name__
         if old_name in new_parent:
@@ -511,14 +527,16 @@
             while new_name in new_parent:
                 index += 1
                 new_name = '{name}-{index:02}'.format(name=old_name, index=index)
+        new_parent[new_name] = child
         del old_parent[old_name]
-        new_parent[new_name] = child
         request.registry.notify(ObjectMovedEvent(child, old_parent, old_name, new_parent, new_name))
     # Re-define order
-    names = [child.__name__ for child in [intids.queryObject(oid)
-                                          for oid in map(int, json.loads(request.params.get('order')))]
-             if child.__parent__ is new_parent]
-    new_parent.updateOrder(names)
+    if len(new_parent.keys()) > 1:
+        names = [child.__name__ for child in [intids.queryObject(oid)
+                                              for oid in map(int, json.loads(request.params.get('order')))]
+                 if (child is not None) and (child.__parent__ is new_parent)]
+        if names:
+            new_parent.updateOrder(names)
     # get all new parent child
     table = SiteContainerTreeTable(request.context, request,
                                    can_sort=json.loads(request.params.get('can_sort', 'false')),