forked from hforge/ikaaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields.py
763 lines (583 loc) · 23.4 KB
/
fields.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# -*- coding: UTF-8 -*-
# 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 copy import deepcopy
from types import FunctionType
# Import from itools
from itools.core import freeze
from itools.csv import Property
from itools.database import magic_from_buffer
from itools.database import Field as BaseField
from itools.datatypes import Boolean, Decimal, Date, DateTime, Email
from itools.datatypes import Enumerate, Integer, String, Unicode, URI
from itools.handlers import get_handler_class_by_mimetype
from itools.html import XHTMLFile, xhtml_uri
from itools.stl import rewrite_uris
from itools.uri import Reference, get_reference
from itools.web import get_context
from itools.xml import START_ELEMENT
# Import from ikaaro
from autoform import Widget, FileWidget, MultilineWidget, TextWidget
from autoform import CheckboxWidget, RadioWidget, SelectWidget
from autoform import BirthDateWidget, DateWidget, DatetimeWidget
from autoform import PasswordWidget, ChoosePassword_Widget
from autoform import ColorPickerWidget, ProgressBarWidget, RTEWidget
from datatypes import Boolean3, BirthDate, HexadecimalColor, HTMLBody
from datatypes import Password_Datatype, ChoosePassword_Datatype
from datatypes import DaysOfWeek
from utils import get_secure_hash, split_reference
class Field(BaseField):
rest_type = 'undefined' # Used by the rest interface
default = None
multilingual = False
required = False
title = None
hidden_by_default = False
readonly = False # Means the field should not be editable by the user
datatype = None
widget = None
def get_value(self, resource, name, language=None):
raise NotImplementedError
def set_value(self, resource, name, value, language=None, **kw):
"""If value == old value then return False
else make the change and return True
"""
# Check the new value is different from the old value
old_value = self.get_value(resource, name, language)
if value == old_value:
# Check the new parameters are different from the old one
for p_key, p_value in kw.iteritems():
if self.get_value(resource, p_key, language) != p_value:
break
else:
return False
# Set property
self._set_value(resource, name, value, language, **kw)
get_context().database.change_resource(resource)
return True
def get_value_title(self, resource, name, language=None):
return self.get_value(resource, name, language)
# XXX For backwards compatibility
datatype_keys = [
'default', 'multiple', 'multilingual', 'indexed', 'stored',
'hidden_by_default', 'is_valid']
def get_datatype(self):
kw = {}
for key in self.datatype_keys:
value = getattr(self, key, None)
if value is not None:
if type(value) is FunctionType:
value = staticmethod(value)
kw[key] = value
return self.datatype(mandatory=self.required, **kw)
def get_default(self):
if self.default is not None:
return self.default
return self.get_datatype().get_default()
widget_keys = ['title', 'endline', 'size', 'tip']
def get_widget(self, name):
kw = {}
for key in self.widget_keys:
value = getattr(self, key, None)
if value is not None:
kw[key] = value
return self.widget(name, **kw)
# Links
def get_links(self, links, resource, field_name, languages):
pass
def update_links(self, resource, field_name, source, target, languages,
old_base, new_base):
pass
def update_incoming_links(self, resource, field_name, source, languages):
pass
# Rest representation
def rest(self):
rest = {'type': self.rest_type}
# From the datatype
datatype = self.get_datatype()
for key in 'multiple', 'multilingual', 'indexed', 'stored':
value = getattr(datatype, key)
rest[key] = Boolean.encode(value)
# Default value
default = self.get_default()
if default is not None:
rest['default'] = datatype.encode(default)
# Other
for key in 'required', 'readonly':
value = getattr(self, key)
rest[key] = Boolean.encode(value)
# Ok
return rest
###########################################################################
# Metadata properties
###########################################################################
class Metadata_Field(Field):
parameters_schema = freeze({})
parameters_schema_default = None
widget = Widget
def get_value(self, resource, name, language=None):
property = resource.metadata.get_property(name, language=language)
if not property:
return self.get_default()
# Multiple
if type(property) is list:
return [ x.value for x in property ]
# Simple
return property.value
def _set_value(self, resource, name, value, language=None, **kw):
if language:
kw['lang'] = language
if kw:
value = Property(value, **kw)
resource.metadata.set_property(name, value)
def rest(self):
rest = super(Metadata_Field, self).rest()
rest['parameters'] = self.parameters_schema.keys()
return rest
class Birthdate_Field(Metadata_Field):
datatype = BirthDate
widget = BirthDateWidget
class Boolean_Field(Metadata_Field):
datatype = Boolean
widget = RadioWidget
widget_keys = Metadata_Field.widget_keys + ['label', 'oneline']
class Boolean3_Field(Metadata_Field):
datatype = Boolean3
widget = SelectWidget
widget_keys = Metadata_Field.widget_keys + ['label']
def get_value(self, resource, name, language=None):
value = super(Boolean3_Field, self).get_value(resource, name, language)
if value is True:
return '1'
elif value is False:
return '0'
return ''
class Char_Field(Metadata_Field):
datatype = String
widget = TextWidget
rest_type = 'bytes'
class Color_Field(Metadata_Field):
datatype = HexadecimalColor
widget = ColorPickerWidget
class Date_Field(Metadata_Field):
datatype = Date
widget = DateWidget
class Datetime_Field(Metadata_Field):
datatype = DateTime
widget = DatetimeWidget
rest_type = 'datetime'
class Email_Field(Metadata_Field):
datatype = Email
size = 40
class Integer_Field(Metadata_Field):
datatype = Integer
class Decimal_Field(Metadata_Field):
datatype = Decimal
class Password_Field(Metadata_Field):
datatype = Password_Datatype
parameters_schema = {'algo': String, 'salt': String, 'date': DateTime}
widget = PasswordWidget
def access(self, mode, resource):
return mode == 'write'
def set_value(self, resource, name, value, language=None, **kw):
if value is not None:
algo = 'sha256'
value, salt = get_secure_hash(value, algo)
kw['algo'] = algo
kw['salt'] = salt
kw['date'] = get_context().timestamp
# super
proxy = super(Password_Field, self)
return proxy.set_value(resource, name, value, language, **kw)
class ChoosePassword_Field(Password_Field):
datatype = ChoosePassword_Datatype
widget = ChoosePassword_Widget
widget_keys = Password_Field.widget_keys + ['userid']
class ProgressBar_Field(Metadata_Field):
datatype = String
widget = ProgressBarWidget
class Select_Field(Metadata_Field):
rest_type = 'select'
datatype = Enumerate
widget = SelectWidget
options = None # Must be overriden by subclasses: [{}, ...]
datatype_keys = Metadata_Field.datatype_keys + ['options']
widget_keys = Metadata_Field.widget_keys + ['has_empty_option', 'oneline']
def get_value_title(self, resource, name, language=None):
value = self.get_value(resource, name, language)
datatype = self.get_datatype()
if self.multiple:
return [datatype.get_value(x) for x in value]
return datatype.get_value(value)
def rest(self):
rest = super(Select_Field, self).rest()
datatype = self.get_datatype()
rest['choices'] = [ x['name'] for x in datatype.get_options() ]
return rest
class Text_Field(Metadata_Field):
rest_type = 'text'
datatype = Unicode
multilingual = True
parameters_schema = {'lang': String} # useful only when multilingual
widget = TextWidget
class Textarea_Field(Text_Field):
widget = MultilineWidget
rest_type = 'textarea'
class URI_Field(Metadata_Field):
datatype = URI
def get_links(self, links, resource, field_name, languages):
base = resource.abspath
if not self.multilingual:
languages = [None]
for lang in languages:
prop = resource.metadata.get_property(field_name, lang)
if prop is None:
continue
if self.multiple:
# Multiple
for x in prop:
value = x.value
if not value:
continue
# Get the reference, path and view
ref, path, view = split_reference(value)
if ref.scheme:
continue
link = base.resolve2(path)
links.add(str(link))
else:
value = prop.value
if not value:
continue
# Get the reference, path and view
ref, path, view = split_reference(value)
if ref.scheme:
continue
# Singleton
link = base.resolve2(path)
links.add(str(link))
return links
def update_links(self, resource, field_name, source, target, languages,
old_base, new_base):
if not self.multilingual:
languages = [None]
for lang in languages:
prop = resource.metadata.get_property(field_name, lang)
if prop is None:
continue
if self.multiple:
# Multiple
new_values = []
for p in prop:
value = p.value
if not value:
continue
# Get the reference, path and view
ref, path, view = split_reference(value)
if ref.scheme:
continue
path = old_base.resolve2(path)
if path == source:
# Explicitly call str because URI.encode does
# nothing
new_value = str(new_base.get_pathto(target)) + view
new_values.append(new_value)
else:
new_values.append(p)
self._set_value(resource, field_name, new_values, lang)
else:
# Singleton
value = prop.value
if not value:
continue
# Get the reference, path and view
ref, path, view = split_reference(value)
if ref.scheme:
continue
path = old_base.resolve2(path)
if path == source:
# Hit the old name
# Build the new reference with the right path
# Explicitly call str because URI.encode does nothing
new_value = str(new_base.get_pathto(target)) + view
self._set_value(resource, field_name, new_value, lang)
def update_incoming_links(self, resource, field_name, source, languages):
target = resource.abspath
resources_old2new = resource.database.resources_old2new
if not self.multilingual:
languages = [None]
for lang in languages:
prop = resource.metadata.get_property(field_name, lang)
if prop is None:
continue
if self.multiple:
# Multiple
new_values = []
for p in prop:
value = p.value
if not value:
continue
# Get the reference, path and view
ref, path, view = split_reference(value)
if ref.scheme:
continue
# Calculate the old absolute path
old_abs_path = source.resolve2(path)
# Check if the target path has not been moved
new_abs_path = resources_old2new.get(old_abs_path,
old_abs_path)
new_value = str(target.get_pathto(new_abs_path)) + view
new_values.append(new_value)
self._set_value(resource, field_name, new_values, lang)
else:
# Singleton
value = prop.value
if not value:
continue
# Get the reference, path and view
ref, path, view = split_reference(value)
if ref.scheme:
continue
# Calculate the old absolute path
old_abs_path = source.resolve2(path)
# Check if the target path has not been moved
new_abs_path = resources_old2new.get(old_abs_path,
old_abs_path)
# Explicitly call str because URI.encode does nothing
new_value = str(target.get_pathto(new_abs_path)) + view
self._set_value(resource, field_name, new_value, lang)
class Owner_Field(URI_Field):
readonly = True
indexed = True
stored = True
###########################################################################
# File handlers
###########################################################################
class File_Field(Field):
rest_type = 'file'
class_handler = None
datatype = String
widget = FileWidget
def _get_key(self, resource, name, language):
base = resource.metadata.key[:-9]
if language:
return '%s.%s.%s' % (base, name, language)
return '%s.%s' % (base, name)
def get_value(self, resource, name, language=None):
cls = self.class_handler
get_handler = resource.metadata.database.get_handler
# Language negotiation
if self.multilingual and language is None:
root = resource.get_root()
languages = []
for lang in root.get_value('website_languages'):
key = self._get_key(resource, name, lang)
handler = get_handler(key, cls, soft=True)
if handler:
languages.append(lang)
language = select_language(languages)
if language is None:
if not languages:
return None
language = languages[0]
# Ok
key = self._get_key(resource, name, language)
return get_handler(key, cls=cls, soft=True)
def _set_value(self, resource, name, value, language=None, **kw):
"""
value may be:
- None (XXX remove handler?)
- a handler
- a byte string
- a tuple
- something else
"""
if self.multilingual and not language:
raise ValueError, 'expected "language" param not found'
if kw:
raise NotImplementedError, 'keyword arguments not supported'
# FIXME This should remove the handler, the FileWidget should include
# a checkbox to remove the handler
if value is None:
return
# Set handler
handler = self._get_handler_from_value(value)
key = self._get_key(resource, name, language)
database = resource.metadata.database
if database.get_handler(key, soft=True):
database.del_handler(key)
database.set_handler(key, handler)
def _get_handler_from_value(self, value):
if type(value) is tuple:
filename, mimetype, value = value
if type(value) is str:
cls = self.class_handler
if cls is None:
mimetype = magic_from_buffer(value)
cls = get_handler_class_by_mimetype(mimetype)
return cls(string=value)
return value
class TextFile_Field(File_Field):
widget = MultilineWidget
map = {'a': 'href', 'img': 'src', 'iframe': 'src',
# Map
'area': 'href',
# Object (FIXME param tag can have both src and data attributes)
'object': 'data',
'param': 'src'}
class HTMLFile_Field(File_Field):
rest_type = 'file-html'
class_handler = XHTMLFile
datatype = HTMLBody
multilingual = True
widget = RTEWidget
def _get_handler_from_value(self, value):
if type(value) is list:
handler = self.class_handler()
handler.set_body(value)
return handler
return super(HTMLFile_Field, self)._get_handler_from_value(value)
def get_links(self, links, resource, field_name, languages):
base = resource.abspath
for language in languages:
handler = self.get_value(resource, field_name, language)
if not handler:
continue
for event, value, line in handler.events:
if event != START_ELEMENT:
continue
tag_uri, tag_name, attributes = value
if tag_uri != xhtml_uri:
continue
# Get the attribute name and value
attr_name = map.get(tag_name)
if attr_name is None:
continue
attr_name = (None, attr_name)
value = attributes.get(attr_name)
if value is None:
continue
reference = get_reference(value)
# Skip empty links, external links and links to '/ui/'
if reference.scheme or reference.authority:
continue
path = reference.path
if not path or path.is_absolute() and path[0] == 'ui':
continue
# Strip the view
name = path.get_name()
if name and name[0] == ';':
path = path[:-1]
uri = base.resolve2(path)
uri = str(uri)
links.add(uri)
def update_links(self, resource, field_name, source, target, languages,
old_base, new_base):
for language in languages:
handler = self.get_value(resource, field_name, language)
if not handler:
continue
events = []
for event in handler.events:
# Process only elements of the XHTML namespace
type, value, line = event
if type != START_ELEMENT:
events.append(event)
continue
tag_uri, tag_name, attributes = value
if tag_uri != xhtml_uri:
events.append(event)
continue
# Get the attribute name and value
attr_name = map.get(tag_name)
if attr_name is None:
events.append(event)
continue
attr_name = (None, attr_name)
value = attributes.get(attr_name)
if value is None:
events.append(event)
continue
reference = get_reference(value)
# Skip empty links, external links and links to '/ui/'
if reference.scheme or reference.authority:
events.append(event)
continue
path = reference.path
if not path or path.is_absolute() and path[0] == 'ui':
events.append(event)
continue
# Strip the view
name = path.get_name()
if name and name[0] == ';':
view = '/' + name
path = path[:-1]
else:
view = ''
# Check the link points to the resource that is moving
path = old_base.resolve2(path)
if path != source:
events.append(event)
continue
# Update the link
# Build the new reference with the right path
new_reference = deepcopy(reference)
new_reference.path = str(new_base.get_pathto(target)) + view
attributes[attr_name] = str(new_reference)
event = (START_ELEMENT, (tag_uri, tag_name, attributes), line)
events.append(event)
handler.set_changed()
handler.events = events
resource.database.change_resource(resource)
def update_incoming_links(self, resource, field_name, source, languages):
target = resource.abspath
resources_old2new = resource.database.resources_old2new
def my_func(value):
# Skip empty links, external links and links to '/ui/'
uri = get_reference(value)
if uri.scheme or uri.authority or uri.path.is_absolute():
return value
path = uri.path
if not path or path.is_absolute() and path[0] == 'ui':
return value
# Strip the view
name = path.get_name()
if name and name[0] == ';':
view = '/' + name
path = path[:-1]
else:
view = ''
# Resolve Path
# Calcul the old absolute path
old_abs_path = source.resolve2(path)
# Get the 'new' absolute parth
new_abs_path = resources_old2new.get(old_abs_path, old_abs_path)
path = str(target.get_pathto(new_abs_path)) + view
value = Reference('', '', path, uri.query.copy(), uri.fragment)
return str(value)
for language in languages:
handler = self.get_value(resource, field_name, language)
if not handler or handler.database.is_phantom(handler):
continue
events = rewrite_uris(handler.events, my_func)
events = list(events)
handler.set_changed()
handler.events = events
class SelectDays_Field(Select_Field):
datatype = DaysOfWeek
widget = CheckboxWidget
widget_keys = Select_Field.widget_keys + ['oneline']
oneline = True