forked from hforge/ikaaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_views.py
305 lines (226 loc) · 8.74 KB
/
text_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
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
# -*- coding: UTF-8 -*-
# Copyright (C) 2006-2007 Hervé Cauwelier <[email protected]>
# Copyright (C) 2006-2008 Juan David Ibáñez Palomar <[email protected]>
# Copyright (C) 2008 Nicolas Deram <[email protected]>
# Copyright (C) 2011 Armel FORTUN <[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 operator import itemgetter
# Import from itools
from itools.datatypes import Integer, String, Unicode
from itools.gettext import MSG
from itools.handlers import TextFile
from itools.web import STLView, INFO
# Import from ikaaro
from autoform import AutoForm, EditAreaWidget, get_default_widget
from buttons import Button, Remove_BrowseButton
from fields import File_Field
from file_views import File_Edit
import messages
from views import BrowseForm
class Text_Edit(File_Edit):
title = MSG(u'Edit')
icon = 'edit.png'
fields = ['title', 'data', 'file', 'description', 'subject', 'share']
def get_value(self, resource, context, name, datatype):
if name == 'data':
data = resource.get_value('data')
if data is None:
return ''
return data.to_str()
proxy = super(Text_Edit, self)
return proxy.get_value(resource, context, name, datatype)
def set_value(self, resource, context, name, form):
if name == 'data':
if form.get('file'):
return False
old_data = resource.get_value('data')
old_value = old_data.to_str()
value = form['data']
# Remove ^M character
value = '\n'.join(value.splitlines())
if old_value == value:
return False
# Save new text
old_data.load_state_from_string(value)
context.database.change_resource(resource)
return False
return super(Text_Edit, self).set_value(resource, context, name, form)
class Text_View(STLView):
access = 'is_allowed_to_view'
title = MSG(u'View')
icon = 'view.png'
template = '/ui/text/view.xml'
def get_namespace(self, resource, context):
data = resource.get_value('data')
return {'data': data.to_str() if data else ''}
class CSS_Edit(Text_Edit):
"""Code editor view using the EditArea instead of a basic textarea or
the WYSIWYG tinyMCE (that cannot directly edit source, as far as I known
[Armel]). Mix CSS_Edit and HTMLEditView"""
title = "Edit CSS file"
data = File_Field(required=True, class_handler=TextFile,
widget=EditAreaWidget)
class PO_Edit(STLView):
access = 'is_allowed_to_edit'
title = MSG(u'Edit')
template = '/ui/PO_edit.xml'
schema = {
'msgctxt': Unicode,
'msgid': Unicode(mandatory=True),
'msgstr': Unicode(mandatory=True)}
def get_namespace(self, resource, context):
# Get the translation units (all but the header)
handler = resource.handler
units = handler.get_units()
units.sort(key=lambda x: x.source)
if units and ''.join(units[0].source) == '':
units = units[1:]
# Total, index, etc.
total = len(units)
index = context.get_form_value('messages_index', default='1')
index = int(index)
previous = max(index - 1, 1)
next = min(index + 1, total)
# Msgid and msgstr
if units:
unit = units[index-1]
msgctxt = u'' if unit.context is None else ''.join(unit.context)
msgid = u''.join(unit.source)
msgstr = u''.join(unit.target)
else:
msgctxt = None
msgid = None
msgstr = None
# Ok
uri = context.uri
return {
'messages_total': total,
'messages_index': index,
'messages_first': uri.replace(messages_index='1'),
'messages_last': uri.replace(messages_index=str(total)),
'messages_previous': uri.replace(messages_index=str(previous)),
'messages_next': uri.replace(messages_index=str(next)),
'msgctxt': msgctxt,
'msgid': msgid,
'msgstr': msgstr}
def action(self, resource, context, form):
msgctxt = None if not form['msgctxt'] else form['msgctxt']
msgid = form['msgid'].replace('\r', '')
msgstr = form['msgstr'].replace('\r', '')
resource.handler.set_msgstr(msgid, msgstr, msgctxt)
# Events, change
context.database.change_resource(resource)
# Ok
context.message = messages.MSG_CHANGES_SAVED
class CSV_View(BrowseForm):
# FIXME We need different permissions for GET and POST
access = 'is_allowed_to_edit'
title = MSG(u'View')
schema = {
'ids': Integer(mandatory=True, multiple=True),
}
search_schema = {}
search_widgets = []
def get_items(self, resource, context):
return list(resource.handler.get_rows())
def sort_and_batch(self, resource, context, items):
# Sort
sort_by = context.query['sort_by']
reverse = context.query['reverse']
if sort_by:
handler = resource.handler
if handler.schema is None:
sort_by = int(sort_by)
else:
sort_by = handler.columns.index(sort_by)
items.sort(key=itemgetter(sort_by), reverse=reverse)
# Batch
start = context.query['batch_start']
size = context.query['batch_size']
return items[start:start+size]
def get_table_columns(self, resource, context):
columns = resource.get_columns()
columns.insert(0, ('checkbox', None))
columns.insert(1, ('index', None))
return columns
def get_item_value(self, resource, context, item, column):
if column == 'checkbox':
return item.number, False
elif column == 'index':
index = item.number
return index, ';edit_row?index=%s' % index
# A value from the schema
handler = resource.handler
datatype = handler.get_datatype(column)
if handler.schema is None:
value = item[int(column)]
else:
value = item.get_value(column)
# Columns
is_enumerate = getattr(datatype, 'is_enumerate', False)
if is_enumerate:
return datatype.get_value(value)
return value
table_actions = [Remove_BrowseButton]
def action_remove(self, resource, context, form):
ids = form['ids']
resource.handler.del_rows(ids)
# Ok
context.message = INFO(u'Row deleted.')
class RowForm(AutoForm):
access = 'is_allowed_to_edit'
def get_schema(self, resource, context):
schema = resource.handler.schema
if schema is not None:
return schema
# Default
schema = {}
for name, title in resource.get_columns():
schema[name] = String
return schema
def get_widgets(self, resource, context):
schema = self.get_schema(resource, context)
return [
get_default_widget(schema[name])(name, title=title)
for name, title in resource.get_columns() ]
class CSV_AddRow(RowForm):
title = MSG(u'Add Row')
icon = 'new.png'
actions = [Button(access=True, css='button-ok', title=MSG(u'Add'))]
def action(self, resource, context, form):
row = [ form[name] for name, title in resource.get_columns() ]
row = resource.handler.add_row(row)
# Ok
message = INFO(u'New row added.')
goto = ';edit_row?index=%s' % row.number
return context.come_back(message, goto=goto)
class CSV_EditRow(RowForm):
title = MSG(u'Edit row #{id}')
query_schema = {
'index': Integer,
}
def get_title(self, context):
id = context.query['index']
return self.title.gettext(id=id)
def get_value(self, resource, context, name, datatype):
id = context.query['index']
row = resource.handler.get_row(id)
return row.get_value(name)
def action(self, resource, context, form):
index = context.query['index']
resource.handler.update_row(index, **form)
# Ok
context.message = messages.MSG_CHANGES_SAVED