-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskin.py
442 lines (366 loc) · 16.2 KB
/
skin.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# -*- coding: UTF-8 -*-
# Copyright (C) 2008-2009 Nicolas Deram <[email protected]>
# Copyright (C) 2008-2011 Henry Obein <[email protected]>
# Copyright (C) 2009 Dumont Sébastien <[email protected]>
# Copyright (C) 2009 J. David Ibanez <[email protected]>
# Copyright (C) 2009-2010 Taverne Sylvain <[email protected]>
# Copyright (C) 2011 Hervé Cauwelier <[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 itools
from itools.core import get_abspath, merge_dicts
from itools.database.ro import ROGitDatabase
from itools.datatypes import Unicode
from itools.gettext import MSG
from itools.stl import stl, set_prefix
from itools.xml import XMLParser
# Import from ikaaro
from ikaaro.menu import Menu, MenuFolder, get_menu_namespace
from ikaaro.skins import Skin as BaseSkin, register_skin
from ikaaro.text import CSS
from ikaaro.tracker import Tracker
from ikaaro.website import WebSite
# Import from itws
from bar import SideBarAware, SideBar_View
from news import NewsItem
from skin_views import AdminBarTemplate, LocationTemplate, LanguagesTemplate
from utils import get_admin_bar, is_navigation_mode
############################################################
# Skin
###########################################################
class Skin(BaseSkin):
title = MSG(u'ITWS skin')
nav_data = {'template': '/ui/neutral/template_nav.xml',
'depth': 1,
'flat': None,
'src': 'theme/menu',
'show_first_child': False}
add_common_nav_css = False
footer_data = {
'template': '/ui/common/template_footer.xml', 'depth': 1,
'flat': None,
'src': 'theme/footer', 'show_first_child': False, 'separator': '|' }
location_template = LocationTemplate
languages_template = LanguagesTemplate
template_title_root = MSG(u"{root_title}")
template_title_base = MSG(u"{root_title} - {here_title}")
def get_backoffice_class(self, context):
# backoffice class
here = context.resource
bo_class = None
current_view = context.view_name
if current_view in ('edit', 'browse_content', 'preview_content',
'login', 'new_resource', 'backlinks',
'edit_state'):
bo_class = 'backoffice'
elif isinstance(here, (Tracker, Tracker.issue_class, CSS, MenuFolder,
Menu)):
bo_class = 'backoffice'
elif isinstance(here, WebSite):
ac = here.get_access_control()
if ac.is_allowed_to_edit(context.user, here):
bo_class = 'backoffice'
return bo_class
def get_rss_feeds(self, context):
rss = []
user = context.user
site_root = context.site_root
site_root_abspath = site_root.abspath
# messages
site_root_rss_title = MSG(u'{ws_title} -- RSS Feeds')
rss_title = MSG(u'{ws_title} {title} -- RSS Feeds')
already_done = []
ws_title = site_root.get_title()
news_folder = site_root.get_news_folder(context)
for resource, template in ((site_root, site_root_rss_title),
(news_folder, rss_title),
(context.resource, rss_title)):
if resource is None or isinstance(resource, tuple(already_done)):
# news_folder can be None
# Do not compute rss link twice
continue
ac = resource.get_access_control()
view = resource.get_view('rss')
if view and ac.is_access_allowed(user, resource, view):
title = template.gettext(ws_title=ws_title,
title=resource.get_title())
if resource.abspath == site_root_abspath:
# context.get_link(site_root) return ./
path = '/;rss'
else:
path = '%s/;rss' % context.get_link(resource)
rss.append({'path': path, 'title': title})
already_done.append(type(resource))
return rss
def get_template_title(self, context):
"""Return the title to give to the template document.
"""
here = context.resource
root = here.get_site_root()
root_title = root.get_title()
# Choose the template
if root is here:
template = self.template_title_root
here_title = None
else:
template = self.template_title_base
# FIXME Check ACL
here_title = here.get_title()
# The view
view_title = context.view.get_title(context)
if type(view_title) is MSG:
view_title = view_title.gettext()
# Ok
return template.gettext(root_title=root_title, here_title=here_title,
view_title=view_title)
def get_sidebar_resource(self, context):
here = context.resource
site_root = context.site_root
sidebar_resource = site_root
if isinstance(here, SideBarAware):
sidebar_resource = here
elif isinstance(here, NewsItem) or\
isinstance(here.parent, site_root.section_class):
sidebar_resource = here.parent
return sidebar_resource
def build_nav_namespace(self, context):
data = self.nav_data
menu = context.site_root.get_resource(data['src'])
ns = get_menu_namespace(context,
data['depth'], data['show_first_child'],
flat=data['flat'], menu=menu)
if is_navigation_mode(context) is True:
return ns
# Add [+] item in menu (To edit)
ac = menu.get_access_control()
if ac.is_allowed_to_edit(context.user, menu):
ns['items'].append(
{'active': False,
'class': None,
'description': None,
'id': 'menu_edit_menu', # FIXME
'in_path': False,
'items': [],
'path': context.get_link(menu),
'real_path': menu.get_abspath(),
'target': '_top',
'title': u'+'})
return ns
def build_footer_namespace(self, context):
data = self.footer_data
ns = get_menu_namespace(context,
data['depth'], data['show_first_child'],
flat=data['flat'], src=data['src'])
here = context.resource
# Manipulate directly the table handler
footer = context.site_root.get_resource('%s/menu' % data['src'])
handler = footer.handler
records = list(handler.get_records_in_order())
get_value = handler.get_record_value
prefix = here.get_pathto(footer)
# HOOK the namespace
for index, item in enumerate(ns.get('items', [])):
record = records[index]
title = get_value(record, 'title')
path = get_value(record, 'path')
html_content = get_value(record, 'html_content')
item['html'] = None
if not path and not title and html_content:
html = set_prefix(html_content, '%s/' % prefix)
item['html'] = html
ns['separator'] = data.get('separator', '|')
# admin bar
ns['admin_bar'] = get_admin_bar(footer.parent) # menu folder
return ns
def build_namespace(self, context):
namespace = BaseSkin.build_namespace(self, context)
here = context.resource
here_ac = here.get_access_control()
site_root = context.site_root
theme = site_root.get_resource('theme')
# banner namespace
banner_ns = {}
banner_ns['title'] = theme.get_property('banner_title')
banner_ns['description'] = site_root.get_property('description')
banner_path = None
path = theme.get_property('banner_path')
if path:
banner = theme.get_resource(path, soft=True)
if banner:
ac = banner.get_access_control()
if ac.is_allowed_to_view(context.user, banner):
banner_path = context.get_link(banner)
banner_ns['path'] = banner_path
namespace['banner'] = banner_ns
# Site search
text = context.get_form_value('text', type=Unicode)
namespace['text'] = text.strip()
# Specific class based on the current resource format
class_id = getattr(here, 'class_id', None)
if class_id:
class_id = class_id.replace('/', '-slash-')
view_name = context.view_name or here.get_default_view_name()
page_css_class = '%s-%s' % (class_id, view_name)
page_css_class = page_css_class.replace('_', '-')
namespace['page_css_class'] = page_css_class.lower()
# resources classes
resource_classes = []
r = here
while not isinstance(r, type(site_root)):
resource_classes.append(r)
r = r.parent
resource_classes = [ r.name for r in reversed(resource_classes) ]
namespace['resource_class'] = ' '.join(resource_classes)
else:
namespace['page_css_class'] = None
namespace['resource_class'] = None
# Add custom data inside the template
custom_data = theme.get_property('custom_data') or ''
namespace['custom_data'] = XMLParser(custom_data)
# RSS Feeds title
namespace['rss_feeds'] = self.get_rss_feeds(context)
# Turning footer
turning_footer = site_root.get_resource('theme/turning-footer',
soft=True)
if turning_footer:
view = turning_footer.view
namespace['turning_footer'] = view.GET(turning_footer, context)
else:
namespace['turning_footer'] = None
# backoffice class
namespace['bo_class'] = self.get_backoffice_class(context)
# Readonly
body_css = None
if here_ac.is_allowed_to_edit(context.user, here) \
and type(context.database) is ROGitDatabase:
body_css = 'readonly'
namespace['body_css'] = body_css
# Sidebar
sidebar = None
display_sidebar = (getattr(here, 'display_sidebar', True)
and getattr(context.view, 'display_sidebar', True))
if display_sidebar and here_ac.is_allowed_to_view(context.user, here):
sidebar_resource = self.get_sidebar_resource(context)
if sidebar_resource:
order_name = sidebar_resource.sidebar_name
sidebar_view = SideBar_View(order_name=order_name)
if not sidebar_view.is_empty(sidebar_resource, context):
# Heuristic, do not compute sidebar view
# if there is no items
sidebar = sidebar_view.GET(sidebar_resource, context)
namespace['sidebar_view'] = sidebar
namespace['sidebar'] = sidebar or namespace['context_menus']
# Language
ws_languages = site_root.get_property('website_languages')
accept = context.accept_language
namespace['lang'] = accept.select_language(ws_languages)
# Nav
# If there is no template specify we simply return the namespace
nav = self.build_nav_namespace(context)
namespace['nav'] = nav
namespace['nav_length'] = len(nav['items'])
nav_template = self.nav_data['template']
if nav_template is not None:
nav_template = self.get_resource(nav_template)
namespace['nav'] = stl(nav_template, {'items': nav['items']})
# Footer
namespace['footer'] = None
footer_template = self.footer_data['template']
if footer_template is not None:
ns_footer = self.build_footer_namespace(context)
footer = None
if len(ns_footer['items']):
footer_template = self.get_resource(footer_template)
footer = stl(footer_template, ns_footer)
namespace['footer'] = footer
# Hide context menus if no user authenticated
if context.user is None:
namespace['context_menus'] = []
# Allow to hide the menu if there is only one item
namespace['display_menu'] = (namespace['nav_length'] > 1)
# Admin bar
site_root = context.site_root
site_root_ac = site_root.get_access_control()
if site_root_ac.is_allowed_to_edit(context.user, site_root):
namespace['admin_bar'] = AdminBarTemplate(context=context)
else:
namespace['admin_bar'] = None
return namespace
def get_styles(self, context):
styles = BaseSkin.get_styles(self, context)
if styles.count('/ui/aruni/aruni.css'):
styles.remove('/ui/aruni/aruni.css')
# insert common style after bo.css
styles.insert(1, '/ui/common/style.css')
if self.add_common_nav_css:
styles.append('/ui/common/menu.css')
styles.append('/ui/common/js/jquery.multiselect2side/style.css')
# In edition mode we add fancybox css
edit_mode = is_navigation_mode(context) is False
ac = context.resource.get_access_control()
is_admin = ac.is_allowed_to_edit(context.user, context.resource)
if edit_mode is True or is_admin:
styles.append('/ui/common/js/fancybox/jquery.fancybox-1.3.1.css')
styles.append('/ui/common/bo.css')
# Do not had a style several times
# Do not tweak cascading by calling reversed before and after
single_styles = []
for style in reversed(styles):
if style not in single_styles:
single_styles.append(style)
return list(reversed(single_styles))
def get_scripts(self, context):
scripts = BaseSkin.get_scripts(self, context)
# In edition mode we add fancybox script
edit_mode = is_navigation_mode(context) is False
ac = context.resource.get_access_control()
is_admin = ac.is_allowed_to_edit(context.user, context.resource)
if edit_mode is True or is_admin:
scripts.append(
'/ui/common/js/fancybox/jquery.fancybox-1.3.1.pack.js')
scripts.append('/ui/common/js/javascript.js')
scripts.append('/ui/common/js/jquery.multiselect2side/javascript.js')
# Do not had a scritp several times
single_sripts = []
for script in scripts:
if script not in single_sripts:
single_sripts.append(script)
return single_sripts
class NeutralSkin(Skin):
title = MSG(u'Neutral Skin 1')
add_common_nav_css = True
class AdminPopupSkin(BaseSkin):
def get_styles(self, context):
styles = BaseSkin.get_styles(self, context)
styles.append('/ui/common/js/jquery.multiselect2side/style.css')
styles.remove('/theme/style/;download')
return styles
def get_scripts(self, context):
scripts = BaseSkin.get_scripts(self, context)
scripts.append('/ui/common/js/jquery.multiselect2side/javascript.js')
return scripts
def build_namespace(self, context):
return merge_dicts(BaseSkin.build_namespace(self, context),
title=context.view.get_title(context),
context_menus=list(self._get_context_menus(context)))
###################
# Register skins
###################
# Internal
register_skin('common', get_abspath('ui/common'))
register_skin('admin-popup', AdminPopupSkin(get_abspath('ui/admin-popup')))
# New skins
register_skin('k2', Skin(get_abspath('ui/k2')))
# Old skins
register_skin('neutral', NeutralSkin(get_abspath('ui/neutral')))