|
1 # |
|
2 # Copyright (c) 2008-2015 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 from pyramid.location import lineage |
|
13 |
|
14 from pyams_content.shared.site.zmi.container import SiteContainerTreeTable, SiteContainerTreeNameColumn |
|
15 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION |
|
16 |
|
17 __docformat__ = 'restructuredtext' |
|
18 |
|
19 |
|
20 # import standard library |
|
21 from uuid import uuid4 |
|
22 |
|
23 # import interfaces |
|
24 from pyams_content.interfaces import CREATE_CONTENT_PERMISSION, MANAGE_CONTENT_PERMISSION |
|
25 from pyams_content.shared.site.interfaces import ISiteContainer, IContentLink |
|
26 from pyams_content.zmi.interfaces import ISiteTreeTable |
|
27 from pyams_i18n.interfaces import II18n |
|
28 from pyams_skin.interfaces.container import ITableElementName |
|
29 from pyams_skin.interfaces.viewlet import IToolbarAddingMenu |
|
30 from pyams_skin.layer import IPyAMSLayer |
|
31 from pyams_zmi.layer import IAdminLayer |
|
32 from zope.intid.interfaces import IIntIds |
|
33 |
|
34 # import packages |
|
35 from pyams_content.shared.site.link import ContentLink |
|
36 from pyams_content.shared.site.zmi.widget import SiteManagerFoldersSelectorFieldWidget |
|
37 from pyams_form.form import AJAXAddForm, AJAXEditForm |
|
38 from pyams_pagelet.pagelet import pagelet_config |
|
39 from pyams_skin.table import get_object_name |
|
40 from pyams_skin.viewlet.toolbar import ToolbarMenuItem |
|
41 from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter |
|
42 from pyams_utils.registry import get_utility |
|
43 from pyams_utils.url import absolute_url |
|
44 from pyams_viewlet.viewlet import viewlet_config |
|
45 from pyams_zmi.form import AdminDialogAddForm, AdminDialogEditForm |
|
46 from pyramid.view import view_config |
|
47 from z3c.form import field |
|
48 from zope.interface import Interface |
|
49 from zope.schema import Int |
|
50 |
|
51 from pyams_content import _ |
|
52 |
|
53 |
|
54 @viewlet_config(name='add-content-link.menu', context=ISiteContainer, layer=IAdminLayer, view=Interface, |
|
55 manager=IToolbarAddingMenu, permission=CREATE_CONTENT_PERMISSION, weight=90) |
|
56 class ContentLinkAddMenu(ToolbarMenuItem): |
|
57 """Content link add menu""" |
|
58 |
|
59 label = _("Rent content...") |
|
60 label_css_class = 'fa fa-fw fa-external-link-square fa-rotate-90' |
|
61 url = 'add-content-link.html' |
|
62 modal_target = True |
|
63 |
|
64 |
|
65 class IContentLinkAddFormFields(IContentLink): |
|
66 """Content link add forms fields interface""" |
|
67 |
|
68 parent = Int(title=_("Parent"), |
|
69 description=_("Folder's parent"), |
|
70 required=True) |
|
71 |
|
72 |
|
73 @pagelet_config(name='add-content-link.html', context=ISiteContainer, layer=IPyAMSLayer, |
|
74 permission=CREATE_CONTENT_PERMISSION) |
|
75 class ContentLinkAddForm(AdminDialogAddForm): |
|
76 """Content link add form""" |
|
77 |
|
78 legend = _("Rent existing content") |
|
79 |
|
80 fields = field.Fields(IContentLinkAddFormFields).select('reference', 'alt_title', 'parent') |
|
81 fields['parent'].widgetFactory = SiteManagerFoldersSelectorFieldWidget |
|
82 |
|
83 ajax_handler = 'add-content-link.json' |
|
84 edit_permission = CREATE_CONTENT_PERMISSION |
|
85 |
|
86 __target = None |
|
87 |
|
88 def updateWidgets(self, prefix=None): |
|
89 super(ContentLinkAddForm, self).updateWidgets(prefix) |
|
90 if 'parent' in self.widgets: |
|
91 self.widgets['parent'].permission = CREATE_CONTENT_PERMISSION |
|
92 |
|
93 def create(self, data): |
|
94 return ContentLink() |
|
95 |
|
96 def update_content(self, content, data): |
|
97 content.reference = data.get('reference') |
|
98 content.alt_title = data['alt_title'] |
|
99 intids = get_utility(IIntIds) |
|
100 parent = intids.queryObject(data.get('parent')) |
|
101 if parent is not None: |
|
102 uuid = str(uuid4()) |
|
103 parent[uuid] = content |
|
104 self.__target = parent |
|
105 |
|
106 def add(self, content): |
|
107 pass |
|
108 |
|
109 def nextURL(self): |
|
110 return absolute_url(self.__target, self.request, 'admin#site-tree.html') |
|
111 |
|
112 |
|
113 @view_config(name='add-content-link.json', context=ISiteContainer, request_type=IPyAMSLayer, |
|
114 permission=CREATE_CONTENT_PERMISSION, renderer='json', xhr=True) |
|
115 class ContentLinkAJAXAddForm(AJAXAddForm, ContentLinkAddForm): |
|
116 """Content link add form, JSOn renderer""" |
|
117 |
|
118 |
|
119 @adapter_config(context=(IContentLink, IPyAMSLayer, ISiteTreeTable), provides=ITableElementName) |
|
120 class ContentLinkTableElementName(ContextRequestViewAdapter): |
|
121 """Content link table element name""" |
|
122 |
|
123 @property |
|
124 def name(self): |
|
125 title = II18n(self.context).query_attribute('alt_title', request=self.request) |
|
126 if not title: |
|
127 target = self.context.get_target() |
|
128 if target is not None: |
|
129 title = get_object_name(target, self.request, self.view) |
|
130 return '<i class="fa fa-fw fa-external-link-square rotate-90"></i>{title}'.format( |
|
131 title=title or '--') |
|
132 |
|
133 |
|
134 @pagelet_config(name='properties.html', context=IContentLink, layer=IPyAMSLayer, |
|
135 permission=VIEW_SYSTEM_PERMISSION) |
|
136 class ContentLinkPropertiesEditForm(AdminDialogEditForm): |
|
137 """Content link properties edit form""" |
|
138 |
|
139 legend = _("Edit content link properties") |
|
140 |
|
141 fields = field.Fields(IContentLink).omit('__parent__', '__name__') |
|
142 |
|
143 ajax_handler = 'properties.json' |
|
144 edit_permission = MANAGE_CONTENT_PERMISSION |
|
145 |
|
146 |
|
147 @view_config(name='properties.json', context=IContentLink, request_type=IPyAMSLayer, |
|
148 permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) |
|
149 class ContentLinkPropertiesAJAXEditForm(AJAXEditForm, ContentLinkPropertiesEditForm): |
|
150 """Content link properties edit form, JSON renderer""" |
|
151 |
|
152 def get_ajax_output(self, changes): |
|
153 output = super(ContentLinkPropertiesAJAXEditForm, self).get_ajax_output(changes) |
|
154 if 'alt_title' in changes.get(IContentLink, ()): |
|
155 intids = get_utility(IIntIds) |
|
156 adapter = ContentLinkTableElementName(self.context, self.request, None) |
|
157 column = SiteContainerTreeNameColumn(self.context, self.request, None) |
|
158 output.setdefault('events', []).append({ |
|
159 'event': 'myams.refresh', |
|
160 'options': { |
|
161 'handler': 'MyAMS.skin.refreshRowCell', |
|
162 'object_id': '{0}::{1}'.format(SiteContainerTreeTable.id, intids.queryId(self.context)), |
|
163 'col_name': 'name', |
|
164 'cell': column.renderCell(self.context, name=adapter.name) |
|
165 } |
|
166 }) |
|
167 return output |