|
1 # |
|
2 # Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 |
|
18 # import interfaces |
|
19 from pyams_content.interfaces import MANAGE_SITE_PERMISSION |
|
20 from pyams_content.shared.common.interfaces import ISharedSite |
|
21 from pyams_content.shared.site.interfaces import ISiteFolder |
|
22 from pyams_content.skin.zmi.interfaces import ISiteTreeTable |
|
23 from pyams_skin.interfaces.viewlet import ITableItemColumnActionsMenu, IContextActions |
|
24 from pyams_skin.layer import IPyAMSLayer |
|
25 from z3c.form.interfaces import IDataExtractedEvent |
|
26 from zope.location.interfaces import ILocation |
|
27 |
|
28 # import packages |
|
29 from pyams_form.form import ajax_config |
|
30 from pyams_pagelet.pagelet import pagelet_config |
|
31 from pyams_skin.viewlet.toolbar import ToolbarMenuItem |
|
32 from pyams_utils.unicode import translate_string |
|
33 from pyams_utils.url import absolute_url |
|
34 from pyams_viewlet.viewlet import viewlet_config |
|
35 from pyams_zmi.form import AdminDialogEditForm |
|
36 from pyramid.events import subscriber |
|
37 from z3c.form import field |
|
38 from zope.interface import Interface, Invalid |
|
39 |
|
40 from pyams_content import _ |
|
41 |
|
42 |
|
43 # |
|
44 # Site container rename view |
|
45 # |
|
46 |
|
47 @viewlet_config(name='rename-item.menu', context=ISharedSite, layer=IPyAMSLayer, |
|
48 view=ISiteTreeTable, manager=ITableItemColumnActionsMenu, |
|
49 permission=MANAGE_SITE_PERMISSION, weight=900) |
|
50 @viewlet_config(name='rename-item.menu', context=ISharedSite, layer=IPyAMSLayer, |
|
51 view=Interface, manager=IContextActions, |
|
52 permission=MANAGE_SITE_PERMISSION, weight=900) |
|
53 @viewlet_config(name='rename-item.menu', context=ISiteFolder, layer=IPyAMSLayer, |
|
54 view=ISiteTreeTable, manager=ITableItemColumnActionsMenu, |
|
55 permission=MANAGE_SITE_PERMISSION, weight=900) |
|
56 @viewlet_config(name='rename-item.menu', context=ISiteFolder, layer=IPyAMSLayer, |
|
57 view=Interface, manager=IContextActions, |
|
58 permission=MANAGE_SITE_PERMISSION, weight=900) |
|
59 class SiteContainerRenameMenu(ToolbarMenuItem): |
|
60 """Site container rename menu""" |
|
61 |
|
62 label = _("Change URL...") |
|
63 label_css_class = 'fa fa-fw fa-edit' |
|
64 url = 'rename-item.html' |
|
65 modal_target = True |
|
66 |
|
67 |
|
68 @pagelet_config(name='rename-item.html', context=ISharedSite, layer=IPyAMSLayer, |
|
69 permission=MANAGE_SITE_PERMISSION) |
|
70 @ajax_config(name='rename-item.json', context=ISharedSite, layer=IPyAMSLayer) |
|
71 @pagelet_config(name='rename-item.html', context=ISiteFolder, layer=IPyAMSLayer, |
|
72 permission=MANAGE_SITE_PERMISSION) |
|
73 @ajax_config(name='rename-item.json', context=ISiteFolder, layer=IPyAMSLayer) |
|
74 class SiteContainerRenameForm(AdminDialogEditForm): |
|
75 """Site container rename form""" |
|
76 |
|
77 prefix = 'rename_item.' |
|
78 |
|
79 legend = _("Change item URL") |
|
80 |
|
81 fields = field.Fields(ILocation).select('__name__') |
|
82 edit_permission = MANAGE_SITE_PERMISSION |
|
83 |
|
84 def updateWidgets(self, prefix=None): |
|
85 super(SiteContainerRenameForm, self).updateWidgets(prefix) |
|
86 self.widgets['__name__'].label = _("Item URL part") |
|
87 |
|
88 def update_content(self, content, data): |
|
89 data = data.get(self, data) |
|
90 old_name = content.__name__ |
|
91 new_name = data['__name__'] = translate_string(content.__name__, spaces='-', keep_chars='-') |
|
92 changes = super(SiteContainerRenameForm, self).update_content(content, data) |
|
93 if changes: |
|
94 # revert rename to adjust container properties |
|
95 content.__name__ = old_name |
|
96 parent = content.__parent__ |
|
97 parent[new_name] = content |
|
98 del parent[old_name] |
|
99 return changes |
|
100 |
|
101 def get_ajax_output(self, changes): |
|
102 if changes: |
|
103 return { |
|
104 'status': 'redirect', |
|
105 'location': absolute_url(self.context, self.request, 'admin'), |
|
106 'smallbox': { |
|
107 'status': 'success', |
|
108 'message': self.request.localizer.translate(self.successMessage) |
|
109 } |
|
110 } |
|
111 else: |
|
112 return super(SiteContainerRenameForm, self).get_ajax_output(changes) |
|
113 |
|
114 |
|
115 @subscriber(IDataExtractedEvent, form_selector=SiteContainerRenameForm) |
|
116 def handle_rename_form_data_extraction(event): |
|
117 """Handle rename form data extraction""" |
|
118 name = event.data.get('__name__') |
|
119 if not name: |
|
120 event.form.widgets.errors += (Invalid(_("You must provide an URL for this item!")),) |