forked from hforge/ikaaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skins_views.py
167 lines (137 loc) · 5.4 KB
/
skins_views.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
# -*- coding: UTF-8 -*-
# Copyright (C) 2005-2008 Juan David Ibáñez Palomar <[email protected]>
# Copyright (C) 2006-2007 Hervé Cauwelier <[email protected]>
# Copyright (C) 2006-2007 Nicolas Deram <[email protected]>
# Copyright (C) 2007 Henry Obein <[email protected]>
# Copyright (C) 2007 Sylvain Taverne <[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 proto_lazy_property
from itools.gettext import get_domain
from itools.i18n import get_language_name
from itools.uri import decode_query
# Import from ikaaro
from utils import CMSTemplate, reduce_string
###########################################################################
# Global language selector
###########################################################################
class LanguagesTemplate(CMSTemplate):
template = '/ui/aruni/languages.xml'
@proto_lazy_property
def languages(self):
context = self.context
# Website languages
ws_languages = context.root.get_value('website_languages')
if len(ws_languages) == 1:
return []
# Select language
accept = context.accept_language
current_language = accept.select_language(ws_languages)
languages = []
gettext = get_domain('itools').gettext
for language in ws_languages:
href = context.uri.replace(language=language)
selected = (language == current_language)
css_class = 'selected' if selected else None
value = get_language_name(language)
languages.append({
'name': language,
'value': gettext(value, language),
'href': href,
'selected': selected,
'class': css_class})
return languages
###########################################################################
# Resource location & menu
###########################################################################
class LocationTemplate(CMSTemplate):
template = '/ui/aruni/location.xml'
keep_view_and_query = False
def get_url(self, path):
if not self.keep_view_and_query:
return path
uri = str(self.context.uri)
view_and_query = ''
if '/;' in uri:
view_and_query = ';' + uri.split('/;')[1]
return path + view_and_query
@proto_lazy_property
def breadcrumb(self):
"""Return a list of dicts [{name, url}...]
"""
context = self.context
root = context.root
# Initialize the breadcrumb with the root resource
path = '/'
title = root.get_title()
if not title:
title = root.class_title.message.encode('utf_8')
breadcrumb = [{
'url': self.get_url(path),
'name': title,
'short_name': reduce_string(title, 15, 30)}]
# Complete the breadcrumb
resource = root
for name in context.uri.path:
path = path + ('%s/' % name)
resource = resource.get_resource(name, soft=True)
if resource is None:
break
# Display resource title only if allowed
if not root.is_allowed_to_view(context.user, resource):
title = name
else:
title = resource.get_title()
# Append
breadcrumb.append({
'url': self.get_url(path),
'name': title,
'short_name': reduce_string(title, 15, 30)})
return breadcrumb
@proto_lazy_property
def tabs(self):
"""Return tabs and subtabs as a dict {tabs, subtabs} of list of dicts
[{name, label, active, style}...].
"""
# Get resource & access control
context = self.context
if context.user is None:
return []
here = context.resource
here_link = context.get_link(here)
# Tabs
tabs = []
for link, view in here.get_views():
# From method?param1=value1¶m2=value2&...
# we separate method and arguments, then we get a dict with
# the arguments and the subview active state
if '?' in link:
name, args = link.split('?')
args = decode_query(args)
else:
name, args = link, {}
# Active
unbound_view = context.view.__bases__[0]
active = (unbound_view is here.get_view(name, args).__bases__[0])
# Add the menu
tabs.append({
'name': '%s/;%s' % (here_link, link),
'label': view.get_title(context),
'active': active,
'class': 'active' if active else None})
return tabs
@proto_lazy_property
def location(self):
return bool(self.breadcrumb) or bool(self.tabs)