forked from hforge/ikaaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoedit.py
397 lines (314 loc) · 12.9 KB
/
autoedit.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
# -*- coding: UTF-8 -*-
# Copyright (C) 2010 Hervé Cauwelier <[email protected]>
# Copyright (C) 2010-2011 Henry Obein <[email protected]>
# Copyright (C) 2010-2011 Taverne Sylvain <[email protected]>
# Copyright (C) 2011 Juan David Ibáñez Palomar <[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, date
# Import from itools
from itools.core import is_prototype
from itools.datatypes import DateTime, Time, String, URI
from itools.gettext import MSG
from itools.i18n import get_language_name
from itools.uri import Reference
from itools.web import get_context
# Import from ikaaro
from autoform import AutoForm, HiddenWidget
from autoform import timestamp_widget
from datatypes import BirthDate
from datatypes import Days, Months, Years
from fields import Field
import messages
from views import ContextMenu
class EditLanguageMenu(ContextMenu):
title = MSG(u'Configuration')
template = '/ui/generic/edit_language_menu.xml'
view = None
def action(self):
uri = self.context.uri
return Reference(uri.scheme, uri.authority, uri.path, {}, None)
def get_fields(self):
context = self.context
resource = self.resource
view = self.view
widgets = view._get_widgets(resource, context)
# Build widgets list
fields, to_keep = view._get_query_fields(resource, context)
return [ {'name': widget.name,
'title': getattr(widget, 'title', 'name'),
'selected': widget.name in fields}
for widget in widgets if widget.name not in to_keep ]
def fields(self):
items = self.get_fields()
# Defaults
for item in items:
for name in ['class', 'src', 'items']:
item.setdefault(name, None)
return items
def _get_items(self):
multilingual = False
schema = self.view._get_schema(self.resource, self.context)
for key, datatype in schema.iteritems():
if getattr(datatype, 'multilingual', False):
multilingual = True
break
if multilingual is False:
# No multilingual fields
return []
root = self.resource.get_root()
languages = root.get_value('website_languages')
edit_languages = self.resource.get_edit_languages(self.context)
return [ {'title': get_language_name(x), 'name': x,
'selected': x in edit_languages}
for x in languages ]
def get_items(self):
"""Do not return item if unique."""
items = self._get_items()
if len(items) == 1:
return []
return items
def get_hidden_fields(self):
return self.view._get_query_to_keep(self.resource, self.context)
def hidden_fields(self):
return self.get_hidden_fields()
def display(self):
"""Do not display the form is there is nothing to show"""
items = self.get_items()
if len(items):
return True
fields = self.get_fields()
if len(fields):
return True
return len(self.hidden_fields())
class AutoEdit(AutoForm):
access = 'is_allowed_to_edit'
title = MSG(u'Edit')
fields = ['title', 'description', 'subject']
def get_fields(self):
context = self.context
resource = self.resource
for name in self.fields:
field = self.get_field(resource, name)
if not is_prototype(field, Field):
field = resource.get_field(name)
if not field:
continue
# Access control
if field.access('write', resource):
yield name
def get_field(self, resource, name):
field = getattr(self, name, None)
if field is not None and is_prototype(field, Field):
return field
return resource.get_field(name)
context_menus = []
def get_context_menus(self):
context_menus = self.context_menus[:] # copy
context_menus.append(EditLanguageMenu(view=self))
return context_menus
#######################################################################
# GET
#######################################################################
def get_query_schema(self):
context = get_context()
resource = context.resource
schema = self._get_schema(resource, context)
for name, datatype in schema.items():
if getattr(datatype, 'mandatory', False) is True:
schema[name] = datatype(mandatory=False)
return schema
# XXX method name sucks
def _get_query_to_keep(self, resource, context):
"""Return a list of dict {'name': name, 'value': value}"""
return []
def _get_query_fields(self, resource, context):
"""Return query fields and readonly or mandatory fields
"""
schema = self._get_schema(resource, context)
default = set()
to_keep = set()
for key, datatype in schema.iteritems():
if getattr(datatype, 'hidden_by_default', False):
continue
# Keep readonly and mandatory widgets
if getattr(datatype, 'mandatory', False):
to_keep.add(key)
if getattr(datatype, 'readonly', False):
to_keep.add(key)
default.add(key)
fields = context.get_query_value('fields', type=String(multiple=True),
default=default)
return set(fields), to_keep
def _get_datatype(self, resource, context, name):
field = self.get_field(resource, name)
if field is None:
field = resource.get_field(name)
field = field(resource=resource) # bind
return field.get_datatype()
def _get_schema(self, resource, context):
schema = {'timestamp': DateTime(readonly=True),
'referrer': URI}
# Add schema from the resource
for name in self.get_fields():
datatype = self._get_datatype(resource, context, name)
# Special case: datetime
if issubclass(datatype, DateTime):
schema['%s_time' % name] = Time
# Special case: birthdate
elif issubclass(datatype, BirthDate):
schema['%s_day' % name] = Days
schema['%s_month' % name] = Months
schema['%s_year' % name] = Years
# Standard case
schema[name] = datatype
return schema
def get_schema(self, resource, context):
"""Return reduced schema
i.e. schema without 'hidden by default' datatypes.
"""
base_schema = self._get_schema(resource, context)
fields, to_keep = self._get_query_fields(resource, context)
schema = {}
for key in fields | to_keep:
schema[key] = base_schema[key]
return schema
def _get_widget(self, resource, context, name):
field = self.get_field(resource, name)
if field is None:
field = resource.get_field(name)
return field.get_widget(name)
def _get_widgets(self, resource, context):
widgets = [timestamp_widget,
HiddenWidget('referrer')]
for name in self.get_fields():
widget = self._get_widget(resource, context, name)
widgets.append(widget)
return widgets
def get_widgets(self, resource, context):
"""Return reduced widgets
i.e. skip hide by default widgets.
"""
base_widgets = self._get_widgets(resource, context)
fields, to_keep = self._get_query_fields(resource, context)
# Reduce widgets
return [ widget for widget in base_widgets
if widget.name in fields or widget.name in to_keep ]
def get_value(self, resource, context, name, datatype):
# Timestamp
if name == 'timestamp':
return context.timestamp
elif name == 'referrer':
referrer = context.query.get('referrer')
return referrer or context.get_referrer()
# Datetime
if name[-5:] == '_time' and issubclass(datatype, Time):
value = self.get_value(resource, context, name[:-5], DateTime)
if type(value) is not datetime:
return None
return value.time()
# BirthDate
elif name[-4:] == '_day' and issubclass(datatype, Days):
value = self.get_value(resource, context, name[:-4], BirthDate)
if type(value) is not date:
return None
value = str(value.day)
context.query[name] = value
return value
elif name[-6:] == '_month' and issubclass(datatype, Months):
value = self.get_value(resource, context, name[:-6], BirthDate)
if type(value) is not date:
return None
value = str(value.month)
context.query[name] = value
return value
elif name[-5:] == '_year' and issubclass(datatype, Years):
value = self.get_value(resource, context, name[:-5], BirthDate)
if type(value) is not date:
return None
value = str(value.year)
context.query[name] = value
return value
# Standard
if not getattr(datatype, 'multilingual', False):
return resource.get_value(name)
# Multilingual
value = {}
for language in resource.get_edit_languages(context):
value[language] = resource.get_value(name, language=language)
return value
#######################################################################
# POST
#######################################################################
def check_edit_conflict(self, resource, context, form):
context.edit_conflict = False
timestamp = form['timestamp']
if timestamp is None:
context.message = messages.MSG_EDIT_CONFLICT
context.edit_conflict = True
return
mtime = resource.get_value('mtime')
if mtime is not None and timestamp < mtime:
# Conflict unless we are overwriting our own work
last_author = resource.get_value('last_author')
if last_author != context.user.name:
user = context.root.get_user_title(last_author)
context.message = messages.MSG_EDIT_CONFLICT2(user=user)
context.edit_conflict = True
def set_value(self, resource, context, name, form):
"""Return True if an error occurs otherwise False. If an error
occurs, the context.message must be an ERROR instance.
"""
if name.endswith(('_time', '_year', '_day', '_month', 'referrer')):
return False
value = form[name]
if type(value) is dict:
for language, data in value.iteritems():
resource.set_value(name, data, language=language)
else:
resource.set_value(name, value)
return False
action_goto = None
action_msg = messages.MSG_CHANGES_SAVED
def action(self, resource, context, form):
# Check edit conflict
self.check_edit_conflict(resource, context, form)
if context.edit_conflict:
return
# Get submit field names
schema = self._get_schema(resource, context)
fields, to_keep = self._get_query_fields(resource, context)
# Save changes
for key in fields | to_keep:
datatype = schema[key]
if getattr(datatype, 'readonly', False):
continue
if self.set_value(resource, context, key, form):
return
# Notify
from cc import Observable
if isinstance(resource, Observable):
resource.notify_subscribers(context)
# Ok
if self.action_goto:
# Get same redirection from x/y/z/;edit and x/y/z and x/y/z/
goto = self.action_goto
if goto[0] not in ('/', 'http://'):
path = str(context.uri.path)
if ('/;' not in path and '/?' not in path
and not path.endswith('/')):
goto = '%s/%s' % (resource.name, goto)
return context.come_back(self.action_msg, goto=goto)
context.message = self.action_msg