Refactored renaming form
authorThierry Florac <thierry.florac@onf.fr>
Mon, 25 Jun 2018 16:09:33 +0200
changeset 731 a39cedc655ff
parent 730 8a9dd03f95ac
child 732 b4e70a25236a
child 736 4eed94fdf7c3
child 739 da2ec9739c67
child 749 ac3feac036f4
Refactored renaming form
src/pyams_content/shared/common/zmi/rename.py
src/pyams_content/shared/site/zmi/container.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/common/zmi/rename.py	Mon Jun 25 16:09:33 2018 +0200
@@ -0,0 +1,120 @@
+#
+# Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.interfaces import MANAGE_SITE_PERMISSION
+from pyams_content.shared.common.interfaces import ISharedSite
+from pyams_content.shared.site.interfaces import ISiteFolder
+from pyams_content.skin.zmi.interfaces import ISiteTreeTable
+from pyams_skin.interfaces.viewlet import ITableItemColumnActionsMenu, IContextActions
+from pyams_skin.layer import IPyAMSLayer
+from z3c.form.interfaces import IDataExtractedEvent
+from zope.location.interfaces import ILocation
+
+# import packages
+from pyams_form.form import ajax_config
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.viewlet.toolbar import ToolbarMenuItem
+from pyams_utils.unicode import translate_string
+from pyams_utils.url import absolute_url
+from pyams_viewlet.viewlet import viewlet_config
+from pyams_zmi.form import AdminDialogEditForm
+from pyramid.events import subscriber
+from z3c.form import field
+from zope.interface import Interface, Invalid
+
+from pyams_content import _
+
+
+#
+# Site container rename view
+#
+
+@viewlet_config(name='rename-item.menu', context=ISharedSite, layer=IPyAMSLayer,
+                view=ISiteTreeTable, manager=ITableItemColumnActionsMenu,
+                permission=MANAGE_SITE_PERMISSION, weight=900)
+@viewlet_config(name='rename-item.menu', context=ISharedSite, layer=IPyAMSLayer,
+                view=Interface, manager=IContextActions,
+                permission=MANAGE_SITE_PERMISSION, weight=900)
+@viewlet_config(name='rename-item.menu', context=ISiteFolder, layer=IPyAMSLayer,
+                view=ISiteTreeTable, manager=ITableItemColumnActionsMenu,
+                permission=MANAGE_SITE_PERMISSION, weight=900)
+@viewlet_config(name='rename-item.menu', context=ISiteFolder, layer=IPyAMSLayer,
+                view=Interface, manager=IContextActions,
+                permission=MANAGE_SITE_PERMISSION, weight=900)
+class SiteContainerRenameMenu(ToolbarMenuItem):
+    """Site container rename menu"""
+
+    label = _("Change URL...")
+    label_css_class = 'fa fa-fw fa-edit'
+    url = 'rename-item.html'
+    modal_target = True
+
+
+@pagelet_config(name='rename-item.html', context=ISharedSite, layer=IPyAMSLayer,
+                permission=MANAGE_SITE_PERMISSION)
+@ajax_config(name='rename-item.json', context=ISharedSite, layer=IPyAMSLayer)
+@pagelet_config(name='rename-item.html', context=ISiteFolder, layer=IPyAMSLayer,
+                permission=MANAGE_SITE_PERMISSION)
+@ajax_config(name='rename-item.json', context=ISiteFolder, layer=IPyAMSLayer)
+class SiteContainerRenameForm(AdminDialogEditForm):
+    """Site container rename form"""
+
+    prefix = 'rename_item.'
+
+    legend = _("Change item URL")
+
+    fields = field.Fields(ILocation).select('__name__')
+    edit_permission = MANAGE_SITE_PERMISSION
+
+    def updateWidgets(self, prefix=None):
+        super(SiteContainerRenameForm, self).updateWidgets(prefix)
+        self.widgets['__name__'].label = _("Item URL part")
+
+    def update_content(self, content, data):
+        data = data.get(self, data)
+        old_name = content.__name__
+        new_name = data['__name__'] = translate_string(content.__name__, spaces='-', keep_chars='-')
+        changes = super(SiteContainerRenameForm, self).update_content(content, data)
+        if changes:
+            # revert rename to adjust container properties
+            content.__name__ = old_name
+            parent = content.__parent__
+            parent[new_name] = content
+            del parent[old_name]
+        return changes
+
+    def get_ajax_output(self, changes):
+        if changes:
+            return {
+                'status': 'redirect',
+                'location': absolute_url(self.context, self.request, 'admin'),
+                'smallbox': {
+                    'status': 'success',
+                    'message': self.request.localizer.translate(self.successMessage)
+                }
+            }
+        else:
+            return super(SiteContainerRenameForm, self).get_ajax_output(changes)
+
+
+@subscriber(IDataExtractedEvent, form_selector=SiteContainerRenameForm)
+def handle_rename_form_data_extraction(event):
+    """Handle rename form data extraction"""
+    name = event.data.get('__name__')
+    if not name:
+        event.form.widgets.errors += (Invalid(_("You must provide an URL for this item!")),)
--- a/src/pyams_content/shared/site/zmi/container.py	Sun Jun 24 16:02:31 2018 +0200
+++ b/src/pyams_content/shared/site/zmi/container.py	Mon Jun 25 16:09:33 2018 +0200
@@ -27,7 +27,7 @@
 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, IContextActions
+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
@@ -58,7 +58,6 @@
 from pyams_utils.registry import get_utility
 from pyams_utils.timezone import tztime
 from pyams_utils.traversing import get_parent
-from pyams_utils.unicode import translate_string
 from pyams_utils.url import absolute_url
 from pyams_viewlet.manager import viewletmanager_config
 from pyams_viewlet.viewlet import viewlet_config
@@ -633,62 +632,3 @@
     def url(self):
         version = IWorkflowVersions(self.context).get_last_versions(count=1)[0]
         return absolute_url(version, self.request, self.view_name)
-
-
-#
-# Site container rename view
-#
-
-@viewlet_config(name='rename-item.menu', context=ISiteContainer, layer=IPyAMSLayer,
-                view=ISiteTreeTable, manager=ITableItemColumnActionsMenu,
-                permission=MANAGE_SITE_PERMISSION, weight=900)
-@viewlet_config(name='rename-item.menu', context=ISiteContainer, layer=IPyAMSLayer,
-                view=Interface, manager=IContextActions,
-                permission=MANAGE_SITE_PERMISSION, weight=900)
-class SiteContainerRenameMenu(ToolbarMenuItem):
-    """Site container rename menu"""
-
-    label = _("Change URL...")
-    label_css_class = 'fa fa-fw fa-edit'
-    url = 'rename-item.html'
-    modal_target = True
-
-
-@pagelet_config(name='rename-item.html', context=ISiteContainer, layer=IPyAMSLayer,
-                permission=MANAGE_SITE_PERMISSION)
-@ajax_config(name='rename-item.json', context=ISiteContainer, layer=IPyAMSLayer)
-class SiteContainerRenameForm(AdminDialogEditForm):
-    """Site container rename form"""
-
-    prefix = 'rename_item.'
-
-    legend = _("Change item URL")
-
-    fields = field.Fields(ISiteContainer).select('__name__')
-    edit_permission = MANAGE_SITE_PERMISSION
-
-    def updateWidgets(self, prefix=None):
-        super(SiteContainerRenameForm, self).updateWidgets(prefix)
-        self.widgets['__name__'].label = _("Item URL part")
-
-    def update_content(self, content, data):
-        old_name = content.__name__
-        changes = super(SiteContainerRenameForm, self).update_content(content, data)
-        if changes:
-            # revert rename to adjust container properties
-            new_name = translate_string(content.__name__, spaces='-', keep_chars='-')
-            content.__name__ = old_name
-            parent = content.__parent__
-            parent[new_name] = content
-            del parent[old_name]
-        return changes
-
-    def get_ajax_output(self, changes):
-        if changes:
-            return {
-                'status': 'redirect',
-                'location': absolute_url(self.context, self.request, 'admin'),
-                'message': self.request.localizer.translate(self.successMessage)
-            }
-        else:
-            return super(SiteContainerRenameForm, self).get_ajax_output(changes)