forked from hforge/ikaaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_menu.py
324 lines (262 loc) · 10.7 KB
/
config_menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# -*- coding: UTF-8 -*-
# Copyright (C) 2010 Henry Obein <[email protected]>
# Copyright (C) 2011 Juan David Ibáñez Palomar <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from the Standard Library
from copy import deepcopy
# Import from itools
from itools.gettext import MSG
from itools.handlers import checkid
from itools.uri import Path
# Import from ikaaro
from autoadd import AutoAdd
from autoedit import AutoEdit
from autoform import PathSelectorWidget
from config import Configuration
from config_common import NewResource_Local
from buttons import Remove_BrowseButton
from fields import Select_Field, URI_Field
from order import OrderedFolder, OrderedFolder_BrowseContent
from utils import split_reference
class Target_Field(Select_Field):
default = '_top'
options = [{'name': '_top', 'value': MSG(u'Current page')},
{'name': '_blank', 'value': MSG(u'New page')}]
required = True
title = MSG(u'Target')
has_empty_option = False
class MenuItem_Browse(OrderedFolder_BrowseContent):
search_widgets = None
table_columns = [
('checkbox', None),
('abspath', MSG(u'Path')),
('title', MSG(u'Title')),
('target', MSG(u'Target')),
('mtime', MSG(u'Last Modified')),
('last_author', MSG(u'Last Author')),
('order', MSG(u'Order'))]
table_actions = [Remove_BrowseButton]
def get_item_value(self, resource, context, item, column):
if column == 'title':
return item.get_title(), item.get_value('path')
proxy = super(MenuItem_Browse, self)
return proxy.get_item_value(resource, context, item, column)
class AddMenu(NewResource_Local):
title = MSG(u'Add item')
def get_items(self, resource, context):
return tuple(resource.get_document_types())
class MenuItem(OrderedFolder):
class_id = 'config-menu-item'
class_title = MSG(u'Menu')
# Fields
path = URI_Field(required=True, title=MSG(u'Path'),
widget=PathSelectorWidget)
target = Target_Field
def get_document_types(self):
return [MenuItem]
# Views
class_views = ['edit', 'browse_content', 'add_menu', 'commit_log']
_fields = ['title', 'path', 'target']
new_instance = AutoAdd(fields=_fields)
edit = AutoEdit(fields=_fields)
browse_content = MenuItem_Browse
add_menu = AddMenu
# API
def _is_allowed_to_access(self, context, uri):
# Get the reference and path
ref, path, view = split_reference(uri)
# Broken entry
if ref is None or path == '':
return False
# External link
if ref.scheme:
return True
# Broken link
resource = self.get_resource(path, soft=True)
if resource is None:
return False
if view:
# Remove the first '/;' of the view
view = resource.get_view(view[2:], ref.query)
if not view:
return False
# Check ACL
return context.is_access_allowed(resource, view)
# Check if the user can access to resource views
# get_views checks ACLs with by calling is_access_allowed
resource_views = list(resource.get_views())
return len(resource_views) > 0
def get_menu_namespace_level(self, context, url, use_first_child=False):
menu_abspath = self.abspath
here = context.resource
here_abspath = here.abspath
here_view_name = url[-1]
here_abspath_and_view = '%s/%s' % (here_abspath, here_view_name)
items = []
for resource in self.get_resources_in_order():
uri = resource.get_value('path')
if not self._is_allowed_to_access(context, uri):
continue
ref, path, view = split_reference(uri)
title = resource.get_value('title')
target = resource.get_value('target')
# Case 1: External link
if ref.scheme:
items.append({
'id': 'menu_%s' % resource.name,
'path': str(ref),
'real_path': None,
'title': title,
'description': None,
'in_path': False,
'active': False,
'class': None,
'target': target,
'items': []})
continue
# Case 2: Internal link
# Sub level
subtabs = resource.get_menu_namespace_level(context, url,
use_first_child)
resource = self.get_resource(path, soft=True)
item_id = 'menu_%s' % resource.name
# Use first child by default we use the resource itself
resource_path = path
# Keep the real path to avoid highlight problems
resource_original_path = path
# use first child
if use_first_child and subtabs:
sub_path = subtabs[0]['real_path']
# if the first child is an external link => skip it
if sub_path is not None:
resource_path = sub_path
# Set active, in_path
active = in_path = False
# add default view
if view:
resource_method = view[2:]
item_id += '_%s' % resource_method
else:
resource_method = resource.get_default_view_name()
resource_abspath_and_view = '%s/;%s' % (resource.abspath,
resource_method)
if here_abspath_and_view == resource_abspath_and_view:
active = True
else:
# Use the original path for the highlight
res_abspath = menu_abspath.resolve2(resource_original_path)
common_prefix = here_abspath.get_prefix(res_abspath)
# Avoid to always set the root entree 'in_path'
# If common prefix equals root abspath set in_path to False
# otherwise compare common_prefix and res_abspath
if common_prefix != Path('/'):
in_path = (common_prefix == res_abspath)
# Build the new reference with the right path
ref2 = deepcopy(ref)
resource = self.get_resource(resource_path)
ref2.path = context.get_link(resource)
if view:
ref2.path += view
items.append({
'id': item_id,
'path': str(ref2),
'real_path': resource.abspath,
'title': title,
'description': None, # FIXME
'in_path': active or in_path,
'active': active,
'class': None,
'target': target,
'items': subtabs})
# Set class
x = None
for i, item in enumerate(items):
if item['active']:
x = i
break
if item['in_path'] and x is None:
x = i
break
if x is not None:
items[x]['class'] = 'in-path'
if len(items) > 0:
# Add class "first" to the first item
css = items[0]['class'] or ''
items[0]['class'] = css + ' first'
# Add class "last" to the last item
css = items[-1]['class'] or ''
items[-1]['class'] = css + ' last'
return items
###########################################################################
# Configuration
###########################################################################
class ConfigMenu(MenuItem):
class_id = 'config-menu'
class_title = MSG(u'Configuration Menu')
class_description = MSG(u'Edit the global menu.')
class_icon48 = 'icons/48x48/menu.png'
def init_resource(self, **kw):
super(ConfigMenu, self).init_resource(**kw)
# Menu
order = []
menus = [('/', u'Home'), ('/;contact', u'Contact')]
for path, title in menus:
name = checkid(title)
order.append(name)
self.make_resource(name, MenuItem, path=path,
title={'en': title})
self.set_property('order', order)
# Configuration
config_name = 'menu'
config_group = 'webmaster'
# Views
class_views = ['browse_content', 'add_menu', 'edit', 'commit_log']
def get_menu_namespace(self, context, show_first_child=False, src=None):
"""Return dict with the following structure:
{'items': [item_dic01, ..., item_dic0N]}
Where the list of items is the first level
and item_dic = {'active': True or False,
'class': 'active' or 'in_path' or None,
'description': MSG(u'About Python'),
'id': 'tab_python',
'in_path': True or False,
'items': [item_dic11, ..., item_dic1N] or None,
'name': 'python',
'path': '../python',
'target': '_top' or '_blank' or None,
'title': MSG(u'Python')}
"items" contains the first level. Each item_dic contains in turn an
'items' with its children.
"class" is a CSS class decorating the item when active on within the
current path.
"id" is a CSS id to uniquely identify the item, based on the title.
"target" is the anchor target (opening a new window or not).
Activate "use_first_child" to automatically point to the first child
of each item instead of the item itself.
"""
resource = context.resource
url = list(context.uri.path)
if not url or url[-1][0] != ';':
method = resource.get_default_view_name()
url.append(';%s' % method)
# Get the menu
menu = self
if src:
menu = self.get_resource(src, soft=True)
if menu is None:
return []
return menu.get_menu_namespace_level(context, url, show_first_child)
# Register
Configuration.register_module(ConfigMenu)