-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
383 lines (320 loc) · 13 KB
/
utils.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
# -*- coding: UTF-8 -*-
# Copyright (C) 2007-2011 Henry Obein <[email protected]>
# Copyright (C) 2008-2009, 2011 Nicolas Deram <[email protected]>
# Copyright (C) 2010 Hervé Cauwelier <[email protected]>
# Copyright (C) 2010-2011 Taverne Sylvain <[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 datetime import datetime, timedelta
# Import from itools
from itools.core import freeze, thingy
from itools.datatypes import Boolean, Enumerate, String, XMLContent
from itools.datatypes import Date, DateTime, PathDataType
from itools.gettext import MSG
from itools.stl import stl
from itools.web import get_context, INFO, FormError
from itools.xml import XMLParser
# Import from ikaaro
from ikaaro.autoform import XHTMLBody
from ikaaro.resource_ import DBResource
from ikaaro.utils import make_stl_template
from ikaaro.workflow import WorkflowAware
# Import from itws
from itws.enumerates import DynamicEnumerate
def get_path_and_view(path):
view = ''
name = path.get_name()
# Strip the view
if name and name[0] == ';':
view = '/' + name
path = path[:-1]
return path, view
def bool_to_img(value):
if value is True:
img = '/ui/itws-icons/16x16/yes.png'
else:
img = '/ui/itws-icons/16x16/no.png'
return XMLParser('<img src="%s"/>' % img)
############################################################
# Manage Buttons
############################################################
admin_bar_template = make_stl_template("""
<div class="fancybox-buttons admin-bar">
<a href="${link}" title="${title}" rel="${rel}">
<img src="/ui/icons/16x16/edit.png"/>
<strong stl:omit-tag="not workflow" class="wf-${workflow/state}">
${class_title}
</strong>
</a>
</div>""")
admin_bar_icon_template = make_stl_template("""
<div class="fancybox-buttons admin-icons-bar" stl:if="buttons">
<div class="box-content">
<table>
<tr>
<td stl:repeat="button buttons">
<img src="${button/icon}" title="${button/label}"/>
<a href="${button/path}" target="${button/target}"
rel="${button/rel}">
${button/label}
</a>
</td>
</tr>
</table>
</div>
</div>""")
def get_admin_bar(resource, buttons=[]):
context = get_context()
if is_navigation_mode(context):
return
ac = resource.get_access_control()
if not ac.is_allowed_to_edit(context.user, resource):
return
events = admin_bar_template if not buttons else admin_bar_icon_template
if hasattr(resource, 'get_admin_edit_link'):
link = resource.get_admin_edit_link(context)
else:
link = '%s/;edit' % context.get_link(resource)
use_fancybox = getattr(resource, 'use_fancybox', True)
# workflow
workflow = None
if isinstance(resource, WorkflowAware):
statename = resource.get_statename()
state = resource.get_state()
workflow = {'state': statename,
'title': state['title'].gettext().encode('utf-8')}
title = MSG(u"Edit box '{title}'").gettext(title=resource.get_title())
namespace = {'link': link,
'rel': 'fancybox' if use_fancybox else None,
'buttons': buttons,
'class_title': resource.class_title,
'title': title,
'workflow': workflow}
return stl(events=events, namespace=namespace)
############################################################
# Links
############################################################
MSG_UNPUBLISHED_RESOURCES_LINKED = INFO(
u'You are linking to {n} unpublished resources, '
u'some users may not be able to see them.<br/>'
u'See <a href="{path}/;backlinks">backlinks interface</a>.')
def build_warn_referenced_msg(resource, context, total):
path = context.get_link(resource)
path = XMLContent.encode(path)
message = MSG_UNPUBLISHED_RESOURCES_LINKED(path=path, n=total)
message = message.gettext().encode('utf8')
return XHTMLBody.decode(message)
_warn_workflow_states = {
'public': ['pending', 'private'],
'pending': ['private'],
'private': []}
def get_warn_referenced_message(resource, context, state):
referenced_resources = list(get_linked_resources(resource))
sub_states = _warn_workflow_states[state]
total = 0
for item in referenced_resources:
workflow_state = item.get_workflow_state()
if workflow_state in sub_states:
total += 1
if total:
return build_warn_referenced_msg(resource, context, total)
return None
def get_linked_resources(resource, state='public'):
"""Return the list of resources which are used by resource.
If resource is WorkflowAware:
resource.state is public -> return 'private', 'pending' resources
resource.state is pending -> return 'private' resource
"""
map = {'public': ['pending', 'private'],
'pending': ['private'],
'private': []}
links = resource.get_links()
links = list(set(links))
# default state
resource_state = state
if isinstance(resource, WorkflowAware):
resource_state = resource.get_workflow_state()
filtered_states = map.get(resource_state, [])
for link in links:
item = resource.get_resource(link, soft=True)
if isinstance(item, WorkflowAware) is False:
continue
state = item.get_workflow_state()
if state in filtered_states:
yield item
def get_linked_resources_message(resource, context, state='public'):
# Customize message if webpage uses private/pending resources
referenced_resources = list(get_linked_resources(resource))
if len(referenced_resources) == 0:
return None
message = MSG(u'This {title} uses pending/private resources '
u'please go to '
u'<a href="{path}/;backlinks">backlinks interface</a>.')
path = context.get_link(resource)
path = XMLContent.encode(path)
class_title = resource.class_title.gettext()
message = message.gettext(title=class_title,
path=path).encode('utf8')
message = XHTMLBody.decode(message)
# Return custom message
return message
def render_for_datatype(value, datatype, context, resource=None):
if value is None:
return None
if issubclass(datatype, Boolean):
return MSG(u'Yes') if value else MSG(u'No')
elif issubclass(datatype, DynamicEnumerate):
the_resource = datatype.get_resource(value)
return {'title': the_resource.get_title(),
'link': context.get_link(the_resource)}
elif issubclass(datatype, PathDataType):
the_resource = resource.get_resource(value, soft=True)
if the_resource is None:
return None
return {'title': the_resource.get_title(),
'link': context.get_link(the_resource)}
elif issubclass(datatype, Enumerate):
return datatype.get_value(value)
elif issubclass(datatype, DateTime):
return context.format_datetime(value)
elif issubclass(datatype, Date):
return context.format_date(value)
return value
def build_resource_namespace(resource, context):
namespace = {}
for key, datatype in resource.class_schema.items():
if getattr(datatype, 'source', None) != 'metadata':
continue
value = resource.get_property(key)
if getattr(datatype, 'multiple', False) is True:
render = [render_for_datatype(x, datatype, context, resource)
for x in value]
else:
render = render_for_datatype(value, datatype, context, resource)
namespace[key] = {'value': value,
'render': render}
# Base namespace
namespace['link'] = context.get_link(resource)
return namespace
############################################################
# Autoform
############################################################
class ITWS_Autoform(thingy):
title = MSG(u'Form')
template = '/ui/common/itws_autoform.xml'
scripts = ['/ui/common/js/javascript.js']
schema = {}
widgets = []
actions = []
get_value_method = None
description = None
css = None
advanced_title = None
def render(self, context):
namespace = {'title': self.title,
'action': '.',
'description': self.description,
'css': self.css,
'advanced_title': self.advanced_title,
'actions': self.actions,
'has_required_widget': False,
'widgets': []}
# If any widget advanced, activate advance mode
advanced = False
for widget in self.widgets:
datatype = self.schema[widget.name]
try:
value = self.get_value_method(widget.name, type=datatype)
except FormError:
value = self.get_value_method(widget.name)
error = MSG(u'Invalid value.')
else:
error = None
if issubclass(datatype, Enumerate):
value = datatype.get_namespace(value)
elif datatype.multiple:
value = value[0]
widget.datatype = datatype
widget.value = value
w_advanced = getattr(widget, 'advanced', None)
advanced = advanced or w_advanced
namespace['widgets'].append(
{'name': widget.name,
'id': widget.id,
'title': widget.title,
'multiple': getattr(datatype, 'multiple', False),
'tip': getattr(widget, 'tip', None),
'mandatory': getattr(datatype, 'mandatory', False),
'endline': getattr(widget, 'endline', None),
'class': None,
'advanced': w_advanced and 'advanced',
'suffix': widget.suffix,
'error': error,
'widget': widget})
if namespace['widgets']:
namespace['first_widget'] = namespace['widgets'][0]['name']
namespace['advanced'] = advanced
template = context.resource.get_resource(self.template)
return stl(template, namespace)
############################################################
# Navigation modes
############################################################
def is_navigation_mode(context):
return context.get_cookie('itws_fo_edit', Boolean(default=False)) is False
############################################################
# Resource with cache
############################################################
class ResourceWithCache(DBResource):
"""Resource with cache inside the metadata handler"""
def __init__(self, metadata):
DBResource.__init__(self, metadata)
# Add cache API
timestamp = getattr(metadata, 'timestamp', None)
# If timestamp is None, the metadata handler could not exists on
# filesystem
# -> make_resource, check if the metadata is already loaded before
# setting the cache properties
if timestamp and getattr(metadata, 'cache_mtime', None) is None:
metadata.cache_mtime = None
metadata.cache_data = None
metadata.cache_errors = None
def _update_data(self):
raise NotImplementedError
def get_cached_data(self):
# Download or send the cache ??
metadata = self.metadata
now = datetime.now()
cache_mtime = metadata.cache_mtime
update_delta = timedelta(minutes=5) # 5 minutes
if (cache_mtime is None or
now - cache_mtime > update_delta):
print u'UPDATE CACHE'
self._update_data()
return metadata.cache_data, metadata.cache_errors
class InternalResourcesAware(object):
"""Implement get_internal_use_resource_names
get_internal_use_resource_names MUST adding a trailing slash to folder
path. By doing this we can build more efficient query in Feed_View
"""
class_schema = freeze({
'internal_resource_aware': Boolean(indexed=True),
'internal_resource_names': String(multiple=True, indexed=True,
stored=True)})
def get_catalog_values(self):
internal_names = self.get_internal_use_resource_names()
return {'internal_resource_aware': True,
'internal_resource_names': list(set(internal_names))}
def get_internal_use_resource_names(self):
return freeze([])