forked from hforge/shop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_views.py
More file actions
297 lines (248 loc) · 10.7 KB
/
utils_views.py
File metadata and controls
297 lines (248 loc) · 10.7 KB
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
# -*- coding: UTF-8 -*-
# Copyright (C) 2009 Sylvain Taverne <sylvain@itaapy.com>
#
# 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.datatypes import Boolean, DateTime, Enumerate
from itools.gettext import MSG
from itools.i18n import format_datetime
from itools.uri import get_reference
from itools.web import BaseForm, ERROR, STLView
from itools.xapian import OrQuery, PhraseQuery, AndQuery, RangeQuery, StartQuery
# Import from ikaaro
from ikaaro.table_views import Table_View
# Import from itws
from itws.views import BrowseFormBatchNumeric
# Import from shop
from datatypes import RangeDatatype
from utils import bool_to_img, get_non_empty_widgets
def get_search_query(search_schema, context, query):
base_query = []
if query is not None:
base_query.extend(query)
form = context.query
for key, datatype in search_schema.items():
if form[key] and issubclass(datatype, RangeDatatype):
minimum, maximum = form[key]
if minimum or maximum:
base_query.append(RangeQuery(key, minimum, maximum))
elif form[key] and key == 'abspath':
base_query.append(StartQuery(key, form[key]))
elif form[key] and datatype.multiple is True:
base_query.append(OrQuery(*[PhraseQuery(key, x) for x in form[key]]))
elif form[key]:
base_query.append(PhraseQuery(key, form[key]))
if len(base_query) > 1:
return AndQuery(*base_query)
elif len(base_query) == 1:
return base_query[0]
return None
class SearchTable_View(Table_View):
search_title = MSG(u'Search')
search_template = '/ui/backoffice/utils_table_search.xml'
search_widgets = []
search_schema = {}
def on_query_error(self, resource, context):
# XXX Should be done in itools
kw = {}
for key in context.uri.query:
if not (key in context.query_error.missing or
key in context.query_error.invalid):
kw[key] = context.uri.query[key]
context.uri.query = kw
msg = ERROR(u'Formulaire invalide')
return context.come_back(msg, goto=context.uri)
def get_search_namespace(self, resource, context):
query = context.query
namespace = {'title': self.search_title,
'submit_value': MSG(u'Rechercher'),
'action': '.',
'submit_class': 'button-ok',
'has_required_widget': False,
'widgets': []}
widgets = get_non_empty_widgets(self.search_schema, self.search_widgets)
for widget in widgets:
value = context.query[widget.name]
datatype = self.search_schema[widget.name]
if issubclass(datatype, Enumerate):
value = datatype.get_namespace(value)
elif datatype.multiple:
value = value[0]
html = widget.to_html(datatype, value)
namespace['widgets'].append(
{'name': widget.name,
'title': widget.title,
'multiple': getattr(datatype, 'multiple', False),
'tip': getattr(widget, 'tip', None),
'mandatory': getattr(datatype, 'mandatory', False),
'class': None,
'suffix': widget.suffix,
'widget': html})
if namespace['widgets']:
namespace['first_widget'] = namespace['widgets'][0]['name']
return namespace
def get_items(self, resource, context, query=None):
if context.uri.query.has_key('search') is False:
return resource.handler.search()
query = get_search_query(self.search_schema, context, query)
return resource.handler.search(query)
class SearchTableFolder_View(BrowseFormBatchNumeric):
search_title = MSG(u'Search')
search_template = '/ui/backoffice/utils_table_search.xml'
search_widgets = []
search_schema = {}
def on_query_error(self, resource, context):
# XXX Should be done in itools
kw = {}
for key in context.uri.query:
if not (key in context.query_error.missing or
key in context.query_error.invalid):
kw[key] = context.uri.query[key]
context.uri.query = kw
msg = ERROR(u'Formulaire invalide')
return context.come_back(msg, goto=context.uri)
def get_search_namespace(self, resource, context):
namespace = {'title': self.search_title,
'submit_value': MSG(u'Rechercher'),
'action': '.',
'submit_class': 'button-ok',
'has_required_widget': False,
'widgets': []}
widgets = get_non_empty_widgets(self.search_schema, self.search_widgets)
for widget in widgets:
value = context.query[widget.name]
datatype = self.search_schema[widget.name]
if issubclass(datatype, Enumerate):
value = datatype.get_namespace(value)
elif datatype.multiple:
value = value[0]
html = widget.to_html(datatype, value)
namespace['widgets'].append(
{'name': widget.name,
'title': widget.title,
'multiple': getattr(datatype, 'multiple', False),
'tip': getattr(widget, 'tip', None),
'mandatory': getattr(datatype, 'mandatory', False),
'class': None,
'suffix': widget.suffix,
'widget': html})
if namespace['widgets']:
namespace['first_widget'] = namespace['widgets'][0]['name']
return namespace
def get_items(self, resource, context, query=[]):
query = get_search_query(self.search_schema, context, query)
results = context.root.search(query)
sort_by = context.query['sort_by']
reverse = context.query['reverse']
return results.get_documents(sort_by=sort_by, reverse=reverse)
def get_item_value(self, resource, context, item, column):
item_brain, item_resource = item
# Default columns
if column == 'name':
name = item_brain.name
return name, name
elif column == 'title':
return item_resource.get_title(), context.get_link(item_resource)
elif column == 'backlinks':
abspath = item_resource.get_canonical_path()
search = context.root.search(links=str(abspath))
return len(search), '%s/;backlinks' % context.get_link(item_resource)
# Guess from schema
schema = item_resource.get_metadata_schema()
computed_schema = getattr(item_resource, 'computed_schema', {})
if schema.has_key(column):
datatype = schema[column]
value = item_resource.get_property(column)
if issubclass(datatype, Enumerate):
return datatype.get_value(value)
elif issubclass(datatype, Boolean):
return bool_to_img(value)
elif issubclass(datatype, DateTime):
if value is None:
return u'-'
accept = context.accept_language
return format_datetime(value, accept)
return value
# Computed columns
elif computed_schema.has_key(column):
return getattr(item_resource, column)
# Other column
proxy = super(SearchTableFolder_View, self)
return proxy.get_item_value(resource, context, item, column)
def sort_and_batch(self, resource, context, items):
root = context.root
user = context.user
# Batch
start = context.query['batch_start']
size = context.query['batch_size']
# ACL
allowed_items = []
for item in items[start:start+size]:
resource = root.get_resource(item.abspath)
ac = resource.get_access_control()
if ac.is_allowed_to_view(user, resource):
allowed_items.append((item, resource))
return allowed_items
class RedirectPermanent(BaseForm):
"""Copied from GoToSpecificPage, but keep query"""
access = True
specific_document = None
def get_specific_document(self, resource, context):
return self.specific_document
def GET(self, resource, context):
# We do a redirect permantent
context.status = 301
# Build goto
query = context.uri.query
specific_document = self.get_specific_document(resource, context)
goto = '%s/%s' % (context.get_link(resource), specific_document)
goto = get_reference(goto)
goto.query = context.uri.query
return goto
class Viewbox_View(STLView):
template = '/ui/shop/repeat_viewboxes.xml'
viewbox = None
sort_by = None
sort_reverse= False
def get_namespace(self, resource, context):
viewboxes = []
for item_resource in self.get_items(resource, context):
viewbox = item_resource.viewbox.GET(item_resource, context)
viewboxes.append(viewbox)
return {'viewboxes': viewboxes,
'show_title': resource.get_property('show_title'),
'title': resource.get_title()}
def get_items(self, resource, context):
search = self.get_items_search(resource, context)
items = search.get_documents(sort_by=self.sort_by,
reverse=self.sort_reverse)
return self.sort_and_batch(resource, context, items)
def get_items_search(self, resource, context):
return None
def sort_and_batch(self, resource, context, items):
root = context.root
user = context.user
# ACL
allowed_items = []
nb_items_to_show = self.get_nb_items_to_show(resource)
if nb_items_to_show:
items = items[:nb_items_to_show]
for item in items:
resource = root.get_resource(item.abspath)
ac = resource.get_access_control()
if ac.is_allowed_to_view(user, resource):
allowed_items.append(resource)
return allowed_items
def get_nb_items_to_show(self, resource):
return None