19 # import interfaces |
19 # import interfaces |
20 from pyams_content.interfaces import MANAGE_SITE_PERMISSION, MANAGE_CONTENT_PERMISSION |
20 from pyams_content.interfaces import MANAGE_SITE_PERMISSION, MANAGE_CONTENT_PERMISSION |
21 from pyams_content.shared.common.interfaces import ISharedContent |
21 from pyams_content.shared.common.interfaces import ISharedContent |
22 from pyams_content.shared.common.interfaces.zmi import IDashboardTable |
22 from pyams_content.shared.common.interfaces.zmi import IDashboardTable |
23 from pyams_content.shared.site.interfaces import ISiteContainer, ISiteManager, IContentLink |
23 from pyams_content.shared.site.interfaces import ISiteContainer, ISiteManager, IContentLink |
24 from pyams_content.zmi.interfaces import IUserAddingsMenuLabel, ISiteTreeMenu, ISiteTreeTable |
24 from pyams_content.skin.zmi.interfaces import ISiteTreeMenu, ISiteTreeTable, IUserAddingsMenuLabel |
25 from pyams_i18n.interfaces import II18n |
25 from pyams_i18n.interfaces import II18n |
26 from pyams_skin.interfaces import IInnerPage, IPageHeader |
26 from pyams_skin.interfaces import IInnerPage, IPageHeader |
27 from pyams_skin.interfaces.container import ITableElementEditor, ITableElementName, ITableWithActions |
27 from pyams_skin.interfaces.container import ITableElementEditor, ITableElementName, ITableWithActions |
28 from pyams_skin.interfaces.viewlet import IBreadcrumbItem, ITableItemColumnActionsMenu |
28 from pyams_skin.interfaces.viewlet import IBreadcrumbItem, ITableItemColumnActionsMenu |
29 from pyams_skin.layer import IPyAMSLayer |
29 from pyams_skin.layer import IPyAMSLayer |
30 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION |
30 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION |
|
31 from pyams_utils.interfaces.traversing import IPathElements |
31 from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowPublicationInfo |
32 from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowPublicationInfo |
32 from pyams_zmi.interfaces.menu import ISiteManagementMenu, IPropertiesMenu |
33 from pyams_zmi.interfaces.menu import ISiteManagementMenu, IPropertiesMenu |
33 from pyams_zmi.layer import IAdminLayer |
34 from pyams_zmi.layer import IAdminLayer |
34 from z3c.table.interfaces import IColumn, IValues |
35 from z3c.table.interfaces import IColumn, IValues |
35 from zope.intid.interfaces import IIntIds |
36 from zope.intid.interfaces import IIntIds |
60 from pyams_zmi.view import AdminView |
61 from pyams_zmi.view import AdminView |
61 from pyramid.exceptions import NotFound |
62 from pyramid.exceptions import NotFound |
62 from pyramid.location import lineage |
63 from pyramid.location import lineage |
63 from pyramid.view import view_config |
64 from pyramid.view import view_config |
64 from z3c.form import field |
65 from z3c.form import field |
|
66 from zope.copy import copy |
65 from zope.interface import implementer |
67 from zope.interface import implementer |
66 from zope.lifecycleevent import ObjectMovedEvent |
68 from zope.lifecycleevent import ObjectMovedEvent |
67 |
69 |
68 from pyams_content import _ |
70 from pyams_content import _ |
69 |
71 |
497 @view_config(name='set-site-order.json', context=ISiteContainer, request_type=IPyAMSLayer, |
499 @view_config(name='set-site-order.json', context=ISiteContainer, request_type=IPyAMSLayer, |
498 permission=MANAGE_SITE_PERMISSION, renderer='json', xhr=True) |
500 permission=MANAGE_SITE_PERMISSION, renderer='json', xhr=True) |
499 def set_site_order(request): |
501 def set_site_order(request): |
500 """Set site elements order""" |
502 """Set site elements order""" |
501 intids = get_utility(IIntIds) |
503 intids = get_utility(IIntIds) |
502 new_parent = intids.queryObject(int(request.params.get('parent'))) |
504 parent_oid = int(request.params.get('parent')) |
|
505 new_parent = intids.queryObject(parent_oid) |
503 # check for changing parent |
506 # check for changing parent |
504 if request.params.get('action') == 'reparent': |
507 if request.params.get('action') == 'reparent': |
505 child = intids.queryObject(int(request.params.get('child'))) |
508 child_oid = int(request.params.get('child')) |
|
509 child = intids.queryObject(child_oid) |
|
510 # check if new parent is not a previous child |
|
511 parent_path = IPathElements(new_parent) |
|
512 if child_oid in parent_path.parents: |
|
513 return { |
|
514 'status': 'reload', |
|
515 'smallbox': { |
|
516 'status': 'error', |
|
517 'message': request.localizer.translate(_("Can't reparent object to one of it's children. " |
|
518 "Reloading...")), |
|
519 'timeout': 5000 |
|
520 } |
|
521 } |
506 old_parent = child.__parent__ |
522 old_parent = child.__parent__ |
507 new_name = old_name = child.__name__ |
523 new_name = old_name = child.__name__ |
508 if old_name in new_parent: |
524 if old_name in new_parent: |
509 index = 1 |
525 index = 1 |
510 new_name = '{name}-{index:02}'.format(name=old_name, index=index) |
526 new_name = '{name}-{index:02}'.format(name=old_name, index=index) |
511 while new_name in new_parent: |
527 while new_name in new_parent: |
512 index += 1 |
528 index += 1 |
513 new_name = '{name}-{index:02}'.format(name=old_name, index=index) |
529 new_name = '{name}-{index:02}'.format(name=old_name, index=index) |
|
530 new_parent[new_name] = child |
514 del old_parent[old_name] |
531 del old_parent[old_name] |
515 new_parent[new_name] = child |
|
516 request.registry.notify(ObjectMovedEvent(child, old_parent, old_name, new_parent, new_name)) |
532 request.registry.notify(ObjectMovedEvent(child, old_parent, old_name, new_parent, new_name)) |
517 # Re-define order |
533 # Re-define order |
518 names = [child.__name__ for child in [intids.queryObject(oid) |
534 if len(new_parent.keys()) > 1: |
519 for oid in map(int, json.loads(request.params.get('order')))] |
535 names = [child.__name__ for child in [intids.queryObject(oid) |
520 if child.__parent__ is new_parent] |
536 for oid in map(int, json.loads(request.params.get('order')))] |
521 new_parent.updateOrder(names) |
537 if (child is not None) and (child.__parent__ is new_parent)] |
|
538 if names: |
|
539 new_parent.updateOrder(names) |
522 # get all new parent child |
540 # get all new parent child |
523 table = SiteContainerTreeTable(request.context, request, |
541 table = SiteContainerTreeTable(request.context, request, |
524 can_sort=json.loads(request.params.get('can_sort', 'false')), |
542 can_sort=json.loads(request.params.get('can_sort', 'false')), |
525 rows_state='plus') |
543 rows_state='plus') |
526 table.update() |
544 table.update() |