This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_field.py
1167 lines (893 loc) · 34.5 KB
/
_field.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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""PytSite ODM Plugin Fields
"""
__author__ = 'Oleksandr Shepetko'
__email__ = '[email protected]'
__license__ = 'MIT'
from typing import Any, Union, List as Lst, Optional, Iterable, Type, Tuple
from inspect import isclass
from datetime import datetime
from decimal import Decimal as Dec
from copy import deepcopy
from bson.objectid import ObjectId as BSONObjectId
from frozendict import frozendict
from pytsite import lang, util, validation, formatters
class Base:
"""Base ODM Field
"""
@property
def is_storable(self) -> bool:
"""Get if the field is storable
"""
return self._is_storable
@property
def is_required(self) -> bool:
"""Get if the field is required
"""
return self._is_required
@is_required.setter
def is_required(self, value: bool):
"""Set if the field is required
"""
self._is_required = value
@property
def is_empty(self) -> bool:
"""Check if the field is empty
"""
return not bool(self._value)
@property
def is_modified(self) -> bool:
"""Check if the field is changed
"""
return self._is_modified
@is_modified.setter
def is_modified(self, value: bool):
"""Set field's 'modified' status
"""
self._is_modified = value
@property
def name(self) -> str:
"""Get name of the field
"""
return self._name
@property
def default(self) -> Any:
"""Get default value of the field
"""
return self._default
@default.setter
def default(self, value):
"""Set default value of the field
"""
self._default = value
self.rst_val()
@property
def required(self):
"""Get if the field is required
"""
raise DeprecationWarning('This property is deprecated since version 6.0, please use is_required() instead')
@required.setter
def required(self, value: bool):
"""Set if the field is required
"""
raise DeprecationWarning('This property is deprecated since version 6.0, please use is_required() instead')
def __init__(self, name: str, **kwargs):
"""Init
"""
if 'required' in kwargs:
raise DeprecationWarning("'required' arg is deprecated since version 6.0, please use 'is_required' instead")
self._name = name
self._default = kwargs.get('default')
self._is_required = kwargs.get('is_required', False)
self._is_storable = kwargs.get('is_storable', True)
self._is_modified = False
self._prev_value = None
self._value = None
if self._default is not None:
self.rst_val(update_state=False)
def set_storable_val(self, value: Any):
"""Must be used to set value which can be safely stored directly to the database
"""
self._value = value
def get_storable_val(self) -> Any:
"""Get value of the field which can be safely saved in the storage
"""
return self._value
def get_storable_prev_val(self) -> Any:
"""Get previous value of the field which can be safely saved in the storage
"""
return self._prev_value
def _on_get(self, value, **kwargs):
"""Hook, transforms internal storable value to its external representation
"""
return value
def get_val(self, **kwargs) -> Any:
"""Get value of the field
"""
return self._on_get(self._value, **kwargs)
def get_prev_val(self, **kwargs) -> Any:
"""Get previous value of the field
"""
return self._on_get(self._prev_value, **kwargs)
def _on_get_jsonable(self, value, **kwargs) -> Any:
"""Hook
"""
return value
def as_jsonable(self, **kwargs) -> Any:
"""Get JSONable representation of field's value
"""
return self._on_get_jsonable(self._value, **kwargs)
def _on_set(self, raw_value, **kwargs):
"""Hook, transforms externally set value to its storable representation
"""
return raw_value
def set_val(self, value, **kwargs):
"""Set value of the field
"""
self._prev_value = self._value
self._value = self._on_set(value, **kwargs)
if kwargs.get('update_state', True) and self._prev_value != self._value:
self._is_modified = True
else:
self._prev_value = self._value
return self
def _on_rst(self, raw_value, **kwargs):
"""Hook
"""
return raw_value
def rst_val(self, **kwargs):
"""Reset field's value to default
"""
return self.set_val(self._on_rst(deepcopy(self._default), **kwargs), reset=True, **kwargs)
def _on_add(self, current_value, raw_value_to_add, **kwargs):
"""Hook, called by self.add_val(), must return external value representation
"""
raise NotImplementedError("Value of the field '{}' cannot be added".format(self._name))
def add_val(self, value_to_add, **kwargs):
"""Add a value to the field
"""
return self.set_val(self._on_add(self.get_val(), value_to_add, **kwargs), **kwargs)
def _on_sub(self, current_value, raw_value_to_sub, **kwargs):
"""Hook, called by self.sub_val(), must return external value representation
"""
raise NotImplementedError("Value of the field '{}' cannot be subtracted".format(self._name))
def sub_val(self, value_to_sub, **kwargs):
"""Subtract a value from the field
"""
return self.set_val(self._on_sub(self.get_val(), value_to_sub, **kwargs), **kwargs)
def _on_inc(self, **kwargs):
"""Hook, called by self.inc_val(), must return external value representation
"""
raise NotImplementedError("Value of the field '{}' cannot be incremented".format(self._name))
def inc_val(self, **kwargs):
"""Increment the value of the field
"""
return self.set_val(self.get_val() + self._on_inc(**kwargs), **kwargs)
def _on_dec(self, **kwargs):
"""Hook, called by self.dec_val(), must return external value representation
"""
raise NotImplementedError("Value of the field '{}' cannot be decremented".format(self._name))
def dec_val(self, **kwargs):
"""Decrement the value of the field
"""
return self.set_val(self.get_val() - self._on_dec(**kwargs), **kwargs)
def _on_entity_delete(self, entity):
"""Hook
:type entity: plugins.odm.Entity
"""
pass
def entity_delete(self, entity):
"""Hook method to provide notification mechanism about entity deletion
:type entity: plugins.odm.Entity
"""
self._on_entity_delete(entity)
def sanitize_finder_arg(self, arg):
"""Hook used for sanitizing Finder's query argument
"""
return arg
def __str__(self) -> str:
"""Stringify field's value
"""
return str(self.get_val())
class Virtual(Base):
"""Virtual Field
"""
def __init__(self, name: str, **kwargs):
super().__init__(name, is_storable=False, **kwargs)
class ObjectId(Base):
"""ObjectID Field
"""
def _on_set(self, raw_value: Optional[BSONObjectId], **kwargs):
"""Hook
"""
if not (raw_value is None or isinstance(raw_value, BSONObjectId)):
raise TypeError('ObjectId expected, not {}'.format(type(raw_value)))
return raw_value
class Ref(Base):
"""Reference
"""
def __init__(self, name: str, **kwargs):
"""Init
"""
self._model = kwargs.get('model', ()) # type: Tuple[str, ...]
self._model_cls = kwargs.get('model_cls', ()) # type: Tuple[Type, ...]
if isinstance(self._model, str):
self._model = (self._model,)
elif not isinstance(self._model, tuple):
self._model = tuple(self._model)
if isclass(self._model_cls):
self._model_cls = (self._model_cls,)
elif not isinstance(self._model, tuple):
self._model_cls = tuple(self._model_cls)
super().__init__(name, **kwargs)
@property
def model(self) -> Tuple[str]:
"""Get model
"""
return self._model
@property
def model_cls(self) -> Tuple[Type]:
"""Get model class
"""
return self._model_cls
def _on_set(self, raw_value, **kwargs) -> Optional[str]:
"""Hook
"""
if not raw_value:
return None
# Get first item from a list or a tuple
if isinstance(raw_value, (list, tuple)):
if len(raw_value):
raw_value = raw_value[0]
else:
return None
# Check entity existence
from ._api import get_by_ref
entity = get_by_ref(raw_value)
# Check entity's model
if self._model and entity.model not in self._model:
raise TypeError("Entity of models {} expected, not '{}'".format(self._model, entity.model))
# Check entity's class
if self._model_cls and entity.__class__ not in self._model_cls:
raise TypeError('Instance of {} expected, not {}'.format(self._model_cls, type(entity)))
return entity.ref
def _on_get(self, value: Optional[str], **kwargs):
"""Hook
:rtype: Optional[Entity]
"""
if value is None:
return None
from ._api import get_by_ref
return get_by_ref(value)
def _on_get_jsonable(self, value, **kwargs):
"""Get serializable representation of the field's value
"""
return '_ref:{}:{}'.format(value.collection, value.id) if value else None
def sanitize_finder_arg(self, arg):
"""Hook. Used for sanitizing Finder's query argument.
"""
from . import _model
if isinstance(arg, _model.Entity):
arg = arg.ref
elif isinstance(arg, str):
from ._api import resolve_ref
arg = self.sanitize_finder_arg(arg.split(',')) if ',' in arg else resolve_ref(arg)
elif isinstance(arg, (dict, set, list, tuple)):
clean_arg = []
for v in arg:
clean_arg.append(self.sanitize_finder_arg(v))
arg = clean_arg
return arg
class Bool(Base):
"""Integer Field
"""
def __init__(self, name: str, **kwargs):
"""Init.
"""
kwargs.setdefault('default', False)
super().__init__(name, **kwargs)
def _on_set(self, raw_value, **kwargs) -> bool:
"""Set value of the field.
"""
return bool(raw_value)
def sanitize_finder_arg(self, arg) -> bool:
"""Hook used for sanitizing Finder's query argument
"""
return formatters.Bool().format(arg)
class Integer(Base):
"""Integer Field
"""
@property
def minimum(self) -> Optional[int]:
return self._minimum
@minimum.setter
def minimum(self, value: int):
self._minimum = value
@property
def maximum(self) -> Optional[int]:
return self._maximum
@maximum.setter
def maximum(self, value: int):
self._maximum = value
def __init__(self, name: str, **kwargs):
"""Init
"""
kwargs.setdefault('default', 0)
self._minimum = kwargs.get('minimum')
self._maximum = kwargs.get('maximum')
super().__init__(name, **kwargs)
def _on_set(self, raw_value: int, **kwargs) -> int:
"""Set value of the field
"""
if raw_value is None:
return 0
if not isinstance(raw_value, int):
raw_value = int(raw_value)
if not kwargs.get('reset'):
if self._minimum is not None and raw_value < self._minimum:
raise ValueError("Value of the field '{}' cannot be less than {}".format(self._name, self._minimum))
if self._maximum is not None and raw_value > self._maximum:
raise ValueError("Value of the field '{}' cannot be greater than {}".format(self._name, self._maximum))
return raw_value
def _on_add(self, current_value: int, raw_value_to_add, **kwargs) -> int:
"""Hook
"""
return current_value + int(raw_value_to_add)
def _on_sub(self, current_value: int, raw_value_to_sub: int, **kwargs) -> int:
"""Hook
"""
return current_value - int(raw_value_to_sub)
def _on_inc(self, **kwargs) -> int:
"""Hook
"""
return 1
def _on_dec(self, **kwargs) -> int:
"""Hook
"""
return 1
def sanitize_finder_arg(self, arg) -> Union[int, Lst[int]]:
"""Hook used for sanitizing Finder's query argument
"""
if isinstance(arg, (set, list, tuple, dict)):
return [int(v) for v in arg]
else:
return int(arg)
class Decimal(Base):
"""Decimal Field
"""
@property
def minimum(self) -> Optional[int]:
return self._minimum
@minimum.setter
def minimum(self, value: int):
self._minimum = value
@property
def maximum(self) -> Optional[int]:
return self._maximum
@maximum.setter
def maximum(self, value: int):
self._maximum = value
@property
def round(self) -> Optional[int]:
return self._round
@round.setter
def round(self, value: int):
self._round = value
@property
def precision(self) -> int:
return self._precision
@precision.setter
def precision(self, value: int):
self._precision = value
def __init__(self, name: str, **kwargs):
"""Init
:type minimum: float
:type maximum: float
:type precision: int
:type round: int
"""
self._minimum = kwargs.get('minimum')
self._maximum = kwargs.get('maximum')
self._precision = kwargs.get('precision', 28)
self._round = kwargs.get('round')
default = kwargs.get('default', 0.0)
if self._round:
default = round(default, self._round)
kwargs.setdefault('default', default)
super().__init__(name, **kwargs)
def _on_get(self, value: float, **kwargs) -> Dec:
"""Get value of the field
"""
return Dec(value)
def _on_set(self, raw_value, **kwargs) -> float:
"""Set value of the field.
:type raw_value: _Decimal | float | int | str
"""
if raw_value is None:
return 0.0
if not isinstance(raw_value, float):
try:
raw_value = float(raw_value)
except ValueError:
raise TypeError("'{}' cannot be used as a value of the field '{}'".format(repr(raw_value), self.name))
if self._round:
raw_value = round(raw_value, self._round)
if not kwargs.get('reset'):
if self._minimum is not None and raw_value < self._minimum:
raise ValueError("Value of the field '{}' cannot be less than {}".format(self._name, self._minimum))
if self._maximum is not None and raw_value > self._maximum:
raise ValueError("Value of the field '{}' cannot be greater than {}".format(self._name, self._maximum))
return raw_value
def _on_add(self, current_value: Dec, raw_value_to_add, **kwargs) -> Dec:
"""
:type raw_value_to_add: _Decimal | float | integer | str
"""
try:
return current_value + Dec(raw_value_to_add)
except ValueError:
raise TypeError("'{}' cannot be used as a value of the field '{}'"
.format(type(raw_value_to_add), self.name))
def _on_sub(self, current_value: Dec, raw_value_to_sub, **kwargs) -> Dec:
"""
:type value: _Decimal | float | integer | str
"""
try:
return current_value - Dec(raw_value_to_sub)
except ValueError:
raise TypeError("'{}' cannot be used as a value of the field '{}'"
.format(type(raw_value_to_sub), self.name))
def sanitize_finder_arg(self, arg) -> Union[float, Lst[float]]:
"""Hook used for sanitizing Finder's query argument
"""
if isinstance(arg, (set, list, tuple, dict)):
return [float(v) for v in arg]
else:
return float(arg)
class String(Base):
"""String Field
"""
def __init__(self, name: str, **kwargs):
"""Init
"""
self._min_length = kwargs.get('min_length')
self._max_length = kwargs.get('max_length')
self._strip_html = kwargs.get('strip_html', True)
self._tidyfy_html = kwargs.get('tidyfy_html', True)
self._remove_empty_html_tags = kwargs.get('remove_empty_html_tags', True)
kwargs.setdefault('default', '')
super().__init__(name, **kwargs)
@property
def min_length(self) -> int:
"""Get minimum field's length.
"""
return self._min_length
@min_length.setter
def min_length(self, val: int):
"""Set minimum field's length.
"""
self._min_length = val
@property
def max_length(self) -> int:
"""Get maximum field's length.
"""
return self._max_length
@max_length.setter
def max_length(self, val: int):
"""Set maximum field's length.
"""
self._max_length = val
@property
def strip_html(self) -> int:
"""Get maximum field's length.
"""
return self._strip_html
@strip_html.setter
def strip_html(self, val: bool):
"""Set maximum field's length.
"""
self._strip_html = val
@property
def tidyfy_html(self) -> int:
"""Get maximum field's length.
"""
return self._tidyfy_html
@tidyfy_html.setter
def tidyfy_html(self, val: bool):
"""Set maximum field's length.
"""
self._tidyfy_html = val
@property
def remove_empty_html_tags(self) -> int:
"""Get maximum field's length.
"""
return self._remove_empty_html_tags
@remove_empty_html_tags.setter
def remove_empty_html_tags(self, val: bool):
"""Set maximum field's length.
"""
self._remove_empty_html_tags = val
def _on_set(self, raw_value: str, **kwargs) -> str:
"""Hook
"""
if raw_value is None:
return ''
if not isinstance(raw_value, str):
raise TypeError("Field '{}': string object expected, not {}.".format(self.name, type(raw_value)))
raw_value = raw_value.strip()
if raw_value:
# Strip HTML
if self._strip_html:
raw_value = util.strip_html_tags(raw_value)
# Tidyfy HTML
elif self._tidyfy_html:
raw_value = util.tidyfy_html(raw_value, self._remove_empty_html_tags)
# Checks lengths
if not kwargs.get('reset'):
if self._min_length:
v_msg_id = 'odm@validation_field_string_min_length'
v_msg_args = {'field': self.name}
validation.rule.MinLength(raw_value, v_msg_id, v_msg_args, min_length=self._min_length).validate()
if self._max_length:
v_msg_id = 'odm@validation_field_string_max_length'
v_msg_args = {'field': self.name}
validation.rule.MinLength(raw_value, v_msg_id, v_msg_args, max_length=self._max_length).validate()
return raw_value
class DateTime(Base):
"""Datetime Field
"""
def _on_set(self, raw_value: Optional[datetime], **kwargs) -> Optional[datetime]:
"""Set field's value
"""
if not raw_value:
return None
if not isinstance(raw_value, datetime):
raise TypeError("DateTime expected, not '{}'".format(type(raw_value)))
if raw_value.tzinfo:
raw_value = raw_value.replace(tzinfo=None)
return raw_value
def _on_get(self, value: datetime, **kwargs) -> Union[datetime, str]:
"""Get field's value
"""
fmt = kwargs.get('fmt')
if value and fmt:
if fmt == 'ago':
value = lang.time_ago(value)
elif fmt == 'pretty_date':
value = lang.pretty_date(value)
elif fmt == 'pretty_date_time':
value = lang.pretty_date_time(value)
else:
value = value.strftime(fmt)
return value
def _on_get_jsonable(self, value, **kwargs):
return util.w3c_datetime_str(value)
class List(Base):
"""List Field
"""
def __init__(self, name: str, **kwargs):
"""Init
"""
if 'unique' in kwargs:
raise DeprecationWarning("'unique' arg is deprecated since version 6.0, please use 'is_unique' instead")
self._allowed_types = kwargs.get('allowed_types', (int, str, float, list, dict, tuple))
self._min_len = kwargs.get('min_len')
self._max_len = kwargs.get('max_len')
self._is_unique = kwargs.get('is_unique', False)
self._cleanup = kwargs.get('cleanup', True)
kwargs.setdefault('default', [])
super().__init__(name, **kwargs)
def _on_get(self, value, **kwargs) -> tuple:
"""Hook
"""
return tuple(value)
def _on_set(self, raw_value, **kwargs) -> list:
"""Hook
:type raw_value: list | tuple
"""
if raw_value is None:
return []
if not isinstance(raw_value, (list, tuple)):
raise TypeError(
"Field '{}': list or tuple expected, not {}: {}".format(self._name, type(raw_value), raw_value))
# Internal value is always a list
if isinstance(raw_value, tuple):
raw_value = list(raw_value)
# Checking validness of types of the items
if self._allowed_types:
for v in raw_value:
if not isinstance(v, self._allowed_types):
raise TypeError("Value of the field '{}' cannot contain members of type {}, but only {}."
.format(self.name, type(v), self._allowed_types))
# Uniquize value
if self._is_unique:
clean_val = []
for v in raw_value:
if v and v not in clean_val:
clean_val.append(v)
raw_value = clean_val
# Checking lengths
if not kwargs.get('reset'):
if self._min_len is not None and len(raw_value) < self._min_len:
raise ValueError("Value length cannot be less than {}.".format(self._min_len))
if self._max_len is not None and len(raw_value) > self._max_len:
raise ValueError("Value length cannot be more than {}.".format(self._max_len))
# Cleaning up empty string values
if self._cleanup:
raw_value = util.cleanup_list(raw_value)
return raw_value
def _on_add(self, current_value: tuple, raw_value_to_add, **kwargs) -> list:
"""Add a value to the field.
"""
if not isinstance(raw_value_to_add, self._allowed_types):
raise TypeError("Value of the field '{}' cannot contain members of type {}, but only {}.".
format(self._name, type(raw_value_to_add), self._allowed_types))
# Checking length
if self._max_len is not None and (len(current_value) + 1) > self._max_len:
raise ValueError("Value length cannot be more than {}.".format(self._max_len))
r = list(current_value)
# Checking for unique value
if self._is_unique:
if raw_value_to_add not in r:
r.append(raw_value_to_add)
else:
if self._cleanup:
# Cleaning up empty string values
if isinstance(raw_value_to_add, str):
raw_value_to_add = raw_value_to_add.strip()
if raw_value_to_add:
r.append(raw_value_to_add)
else:
r.append(raw_value_to_add)
return r
def _on_sub(self, current_value: tuple, raw_value_to_sub, **kwargs) -> list:
"""Subtract value from list
"""
if not isinstance(raw_value_to_sub, self._allowed_types):
raise TypeError("Value of the field '{}' cannot contain members of type {}, but only {}.".
format(self._name, type(raw_value_to_sub), self._allowed_types))
# Checking length
if self._min_len is not None and len(current_value) == self._min_len:
raise ValueError("Value length cannot be less than {}.".format(self._min_len))
return [v for v in current_value if v != raw_value_to_sub]
class Dict(Base):
"""Dictionary Field
"""
def __init__(self, name: str, **kwargs):
"""Init
:param default: dict
:param keys: tuple
:param nonempty_keys: tuple
"""
self._keys = kwargs.get('keys', ()) # Required keys
self._nonempty_keys = kwargs.get('nonempty_keys', ()) # Required keys which must be non-empty
self._dotted_keys = kwargs.get('dotted_keys', False)
self._dotted_keys_replacement = kwargs.get('dotted_keys_replacement', ':')
kwargs.setdefault('default', {})
super().__init__(name, **kwargs)
def _on_get(self, value: dict, **kwargs) -> frozendict:
"""Hook
"""
if self._dotted_keys:
new_value = {}
for k, v in value.items():
new_value[k.replace(self._dotted_keys_replacement, '.')] = v
value = new_value
# Don't allow to change value outside the field's object
return frozendict(value)
def _on_set(self, raw_value: Union[dict, frozendict], **kwargs) -> dict:
"""Hook
"""
if raw_value is None:
return {}
if type(raw_value) not in (dict, frozendict):
raise TypeError("Value of the field '{}' should be a dict. not '{}'.".format(self._name, type(raw_value)))
# Internally this field stores ordinary dict
if isinstance(raw_value, frozendict):
raw_value = dict(raw_value)
if self._dotted_keys:
raw_value = {k.replace('.', self._dotted_keys_replacement): v for k, v in raw_value.items()}
if not kwargs.get('reset'):
if self._keys:
for k in self._keys:
if k not in raw_value:
raise ValueError("Value of the field '{}' must contain key '{}'.".format(self._name, k))
if self._nonempty_keys:
for k in self._nonempty_keys:
if k not in raw_value or raw_value[k] is None:
raise ValueError(
"Value of the field '{}' must contain nonempty key '{}'.".format(self._name, k))
return raw_value
def _on_add(self, current_value: frozendict, raw_value_to_add, **kwargs) -> dict:
"""Add a value to the field.
"""
try:
r = dict(current_value)
r.update(dict(raw_value_to_add))
return r
except ValueError:
raise TypeError("Value of the field '{}' must be a dict".format(self._name))
class Enum(Base):
"""Enumerated field
"""
def __init__(self, name: str, **kwargs):
"""Init
"""
self._values = kwargs.get('values')
if not hasattr(self._values, '__iter__'):
raise TypeError("'values' must be an iterable, not {}".format(type(self._values)))
super().__init__(name, **kwargs)
def _on_set(self, raw_value, **kwargs):
if raw_value and raw_value not in self._values:
raise ValueError("Value of the field '{}' can be only one of the following: {}"
.format(self.name, self._values))
return raw_value
@property
def values(self) -> Iterable:
return self._values
class UniqueList(List):
"""Unique List Field
"""
def __init__(self, name: str, **kwargs):
"""Init.
"""
super().__init__(name, is_unique=True, **kwargs)
class RefsList(List):
"""List of References Field
"""
def __init__(self, name: str, **kwargs):
"""Init.
"""
from ._model import Entity
self._model = kwargs.get('model', ()) # type: Tuple[str, ...]
self._model_cls = kwargs.get('model_cls', ()) # type: Tuple[Type, ...]
if isinstance(self._model, str):
self._model = (self._model,)
elif not isinstance(self._model, tuple):
self._model = tuple(self._model)
if isclass(self._model_cls):
self._model_cls = (self._model_cls,)
elif not isinstance(self._model, tuple):
self._model_cls = tuple(self._model_cls)
super().__init__(name, allowed_types=(Entity,), **kwargs)
@property
def model(self) -> Tuple[str]:
"""Get model
"""
return self._model
@property
def model_cls(self) -> Tuple[Type]:
"""Get model class
"""
return self._model_cls
def _on_set(self, raw_value, **kwargs) -> Lst[str]:
"""Set value of the field
"""
if raw_value is None:
return []
if not isinstance(raw_value, (list, tuple)):
raise TypeError(
"List or tuple expected as a value of the field '{}', not '{}'".format(self._name, repr(raw_value)))
from ._api import get_by_ref
# Check value
r = []
for v in raw_value:
# Check entity existence
entity = get_by_ref(v)