-
Notifications
You must be signed in to change notification settings - Fork 217
/
fields.py
1147 lines (902 loc) · 38.3 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
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
"""
Fields declare storage for XBlock data. They use abstract notions of
**scopes** to associate each field with particular sets of blocks and users.
The hosting runtime application decides what actual storage mechanism to use
for each scope.
"""
from collections import namedtuple
import copy
import datetime
import hashlib
import itertools
import json
import re
import traceback
import warnings
import dateutil.parser
from lxml import etree
import pytz
import yaml
# __all__ controls what classes end up in the docs, and in what order.
__all__ = [
'BlockScope', 'UserScope', 'Scope', 'ScopeIds',
'Field',
'Boolean', 'Dict', 'Float', 'Integer', 'List', 'Set', 'String', 'XMLString',
]
class FailingEnforceTypeWarning(DeprecationWarning):
"""
A warning triggered when enforce_type would cause a exception if enabled
"""
class ModifyingEnforceTypeWarning(DeprecationWarning):
"""
A warning triggered when enforce_type would change a value if enabled
"""
class Sentinel:
"""
Class for implementing sentinel objects (only equal to themselves).
"""
def __init__(self, name):
"""
`name` is the name used to identify the sentinel (which will
be displayed as the __repr__) of the sentinel.
"""
self.name = name
def __repr__(self):
return self.name
@property
def attr_name(self):
""" TODO: Look into namespace collisions. block.name_space == block_name.space
"""
return self.name.lower().replace('.', '_')
def __eq__(self, other):
""" Equality is based on being of the same class, and having same name
"""
return isinstance(other, Sentinel) and self.name == other.name
def __hash__(self):
"""
Use a hash of the name of the sentinel
"""
return hash(self.name)
class BlockScope:
"""
Enumeration of block scopes.
The block scope specifies how a field relates to blocks. A
:class:`.BlockScope` and a :class:`.UserScope` are combined to make a
:class:`.Scope` for a field.
USAGE: The data is related to a particular use of a block in a course.
DEFINITION: The data is related to the definition of the block. Although
unusual, one block definition can be used in more than one place in a
course.
TYPE: The data is related to all instances of this type of XBlock.
ALL: The data is common to all blocks. This can be useful for storing
information that is purely about the student.
"""
USAGE = Sentinel('BlockScope.USAGE')
DEFINITION = Sentinel('BlockScope.DEFINITION')
TYPE = Sentinel('BlockScope.TYPE')
ALL = Sentinel('BlockScope.ALL')
@classmethod
def scopes(cls):
"""
Return a list of valid/understood class scopes.
"""
# Why do we need this? This should either
# * Be bubbled to the places where it is used (AcidXBlock).
# * Be automatic. Look for all members of a type.
return [cls.USAGE, cls.DEFINITION, cls.TYPE, cls.ALL]
class UserScope:
"""
Enumeration of user scopes.
The user scope specifies how a field relates to users. A
:class:`.BlockScope` and a :class:`.UserScope` are combined to make a
:class:`.Scope` for a field.
NONE: Identifies data agnostic to the user of the :class:`.XBlock`. The
data is related to no particular user. All users see the same data.
For instance, the definition of a problem.
ONE: Identifies data particular to a single user of the :class:`.XBlock`.
For instance, a student's answer to a problem.
ALL: Identifies data aggregated while the block is used by many users.
The data is related to all the users. For instance, a count of how
many students have answered a question, or a histogram of the answers
submitted by all students.
"""
NONE = Sentinel('UserScope.NONE')
ONE = Sentinel('UserScope.ONE')
ALL = Sentinel('UserScope.ALL')
@classmethod
def scopes(cls):
"""
Return a list of valid/understood class scopes.
Why do we need this? I believe it is not used anywhere.
"""
return [cls.NONE, cls.ONE, cls.ALL]
UNSET = Sentinel("fields.UNSET")
ScopeBase = namedtuple('ScopeBase', 'user block name')
class Scope(ScopeBase):
"""
Defines six types of scopes to be used: `content`, `settings`,
`user_state`, `preferences`, `user_info`, and `user_state_summary`.
The `content` scope is used to save data for all users, for one particular
block, across all runs of a course. An example might be an XBlock that
wishes to tabulate user "upvotes", or HTML content to display literally on
the page (this example being the reason this scope is named `content`).
The `settings` scope is used to save data for all users, for one particular
block, for one specific run of a course. This is like the `content` scope,
but scoped to one run of a course. An example might be a due date for a
problem.
The `user_state` scope is used to save data for one user, for one block,
for one run of a course. An example might be how many points a user scored
on one specific problem.
The `preferences` scope is used to save data for one user, for all
instances of one specific TYPE of block, across the entire platform. An
example might be that a user can set their preferred default speed for the
video player. This default would apply to all instances of the video
player, across the whole platform, but only for that student.
The `user_info` scope is used to save data for one user, across the entire
platform. An example might be a user's time zone or language preference.
The `user_state_summary` scope is used to save data aggregated across many
users of a single block. For example, a block might store a histogram of
the points scored by all users attempting a problem.
"""
content = ScopeBase(UserScope.NONE, BlockScope.DEFINITION, 'content')
settings = ScopeBase(UserScope.NONE, BlockScope.USAGE, 'settings')
user_state = ScopeBase(UserScope.ONE, BlockScope.USAGE, 'user_state')
preferences = ScopeBase(UserScope.ONE, BlockScope.TYPE, 'preferences')
user_info = ScopeBase(UserScope.ONE, BlockScope.ALL, 'user_info')
user_state_summary = ScopeBase(UserScope.ALL, BlockScope.USAGE, 'user_state_summary')
@classmethod
def named_scopes(cls):
"""Return all named Scopes."""
return [
cls.content,
cls.settings,
cls.user_state,
cls.preferences,
cls.user_info,
cls.user_state_summary
]
@classmethod
def scopes(cls):
"""Return all possible Scopes."""
named_scopes = cls.named_scopes()
return named_scopes + [
cls(user, block)
for user in UserScope.scopes()
for block in BlockScope.scopes()
if cls(user, block) not in named_scopes
]
def __new__(cls, user, block, name=None):
"""Create a new Scope, with an optional name."""
if name is None:
name = f'{user}_{block}'
return ScopeBase.__new__(cls, user, block, name)
children = Sentinel('Scope.children')
parent = Sentinel('Scope.parent')
def __str__(self):
return self.name
def __eq__(self, other):
return isinstance(other, Scope) and self.user == other.user and self.block == other.block
def __hash__(self):
return hash(('xblock.fields.Scope', self.user, self.block))
class ScopeIds(namedtuple('ScopeIds', 'user_id block_type def_id usage_id')):
"""
A simple wrapper to collect all of the ids needed to correctly identify an XBlock
(or other classes deriving from ScopedStorageMixin) to a FieldData.
These identifiers match up with BlockScope and UserScope attributes, so that,
for instance, the `def_id` identifies scopes that use BlockScope.DEFINITION.
"""
__slots__ = ()
# Define special reference that can be used as a field's default in field
# definition to signal that the field should default to a unique string value
# calculated at runtime.
UNIQUE_ID = Sentinel("fields.UNIQUE_ID")
# define a placeholder ('nil') value to indicate when nothing has been stored
# in the cache ("None" may be a valid value in the cache, so we cannot use it).
NO_CACHE_VALUE = Sentinel("fields.NO_CACHE_VALUE")
# define a placeholder value that indicates that a value is explicitly dirty,
# because it was explicitly set
EXPLICITLY_SET = Sentinel("fields.EXPLICITLY_SET")
# Fields that cannot have runtime-generated defaults. These are special,
# because they define the structure of XBlock trees.
NO_GENERATED_DEFAULTS = ('parent', 'children')
class Field:
"""
A field class that can be used as a class attribute to define what data the
class will want to refer to.
When the class is instantiated, it will be available as an instance
attribute of the same name, by proxying through to the field-data service on
the containing object.
Parameters:
help (str): documentation for the field, suitable for presenting to a
user (defaults to None).
default: field's default value. Can be a static value or the special
xblock.fields.UNIQUE_ID reference. When set to xblock.fields.UNIQUE_ID,
the field defaults to a unique string that is deterministically calculated
for the field in the given scope (defaults to None).
scope: this field's scope (defaults to Scope.content).
display_name: the display name for the field, suitable for presenting
to a user (defaults to name of the field).
values: a specification of the valid values for this field. This can be
specified as either a static specification, or a function that
returns the specification. For example specification formats, see
the values property definition.
enforce_type: whether the type of the field value should be enforced
on set, using self.enforce_type, raising an exception if it's not
possible to convert it. This provides a guarantee on the stored
value type.
xml_node: if set, the field will be serialized as a
separate node instead of an xml attribute (default: False).
force_export: if set, the field value will be exported to XML even if normal
export conditions are not met (i.e. the field has no explicit value set)
kwargs: optional runtime-specific options/metadata. Will be stored as
runtime_options.
"""
MUTABLE = True
_default = None
# Indicates if a field's None value should be sent to the XML representation.
none_to_xml = False
__name__ = None
# We're OK redefining built-in `help`
def __init__(self, help=None, default=UNSET, scope=Scope.content, # pylint:disable=redefined-builtin
display_name=None, values=None, enforce_type=False,
xml_node=False, force_export=False, **kwargs):
self.warned = False
self.help = help
self._enable_enforce_type = enforce_type
if default is not UNSET:
if default is UNIQUE_ID:
self._default = UNIQUE_ID
else:
self._default = self._check_or_enforce_type(default)
self.scope = scope
self._display_name = display_name
self._values = values
self.runtime_options = kwargs
self.xml_node = xml_node
self.force_export = force_export
@property
def default(self):
"""Returns the static value that this defaults to."""
if self.MUTABLE:
return copy.deepcopy(self._default)
else:
return self._default
@staticmethod
def needs_name(field):
"""
Returns whether the given ) is yet to be named.
"""
return not field.__name__
@property
def name(self):
"""Returns the name of this field."""
# This is set by ModelMetaclass
return self.__name__ or 'unknown'
@property
def values(self):
"""
Returns the valid values for this class. This is useful
for representing possible values in a UI.
Example formats:
* A finite set of elements::
[1, 2, 3]
* A finite set of elements where the display names differ from the
values::
[
{"display_name": "Always", "value": "always"},
{"display_name": "Past Due", "value": "past_due"},
]
* A range for floating point numbers with specific increments::
{"min": 0 , "max": 10, "step": .1}
If this field class does not define a set of valid values, this
property will return None.
"""
if callable(self._values):
return self._values()
else:
return self._values
@property
def display_name(self):
"""
Returns the display name for this class, suitable for use in a GUI.
If no display name has been set, returns the name of the class.
"""
return self._display_name if self._display_name is not None else self.name
def _get_cached_value(self, xblock):
"""
Return a value from the xblock's cache, or a marker value if either the cache
doesn't exist or the value is not found in the cache.
"""
return getattr(xblock, '_field_data_cache', {}).get(self.name, NO_CACHE_VALUE)
def _set_cached_value(self, xblock, value):
"""Store a value in the xblock's cache, creating the cache if necessary."""
# pylint: disable=protected-access
if not hasattr(xblock, '_field_data_cache'):
xblock._field_data_cache = {}
xblock._field_data_cache[self.name] = value
def _del_cached_value(self, xblock):
"""Remove a value from the xblock's cache, if the cache exists."""
# pylint: disable=protected-access
if hasattr(xblock, '_field_data_cache') and self.name in xblock._field_data_cache:
del xblock._field_data_cache[self.name]
def _mark_dirty(self, xblock, value):
"""Set this field to dirty on the xblock."""
# pylint: disable=protected-access
# Deep copy the value being marked as dirty, so that there
# is a baseline to check against when saving later
if self not in xblock._dirty_fields:
xblock._dirty_fields[self] = copy.deepcopy(value)
def _is_dirty(self, xblock):
"""
Return whether this field should be saved when xblock.save() is called
"""
# pylint: disable=protected-access
if self not in xblock._dirty_fields:
return False
baseline = xblock._dirty_fields[self]
return baseline is EXPLICITLY_SET or xblock._field_data_cache[self.name] != baseline
def _is_lazy(self, value):
"""
Detect if a value is being evaluated lazily by Django.
"""
return 'django.utils.functional.' in str(type(value))
def _check_or_enforce_type(self, value):
"""
Depending on whether enforce_type is enabled call self.enforce_type and
return the result or call it and trigger a silent warning if the result
is different or a Traceback
To aid with migration, enable the warnings with:
warnings.simplefilter("always", FailingEnforceTypeWarning)
warnings.simplefilter("always", ModifyingEnforceTypeWarning)
"""
if self._enable_enforce_type:
return self.enforce_type(value)
try:
new_value = self.enforce_type(value)
except: # pylint: disable=bare-except
message = "The value {!r} could not be enforced ({})".format(
value, traceback.format_exc().splitlines()[-1])
warnings.warn(message, FailingEnforceTypeWarning, stacklevel=3)
else:
try:
equal = value == new_value
except TypeError:
equal = False
if not equal:
message = "The value {!r} would be enforced to {!r}".format(
value, new_value)
warnings.warn(message, ModifyingEnforceTypeWarning, stacklevel=3)
return value
def _calculate_unique_id(self, xblock):
"""
Provide a default value for fields with `default=UNIQUE_ID`.
Returned string is a SHA1 hex digest that is deterministically calculated
for the field in its given scope.
"""
key = scope_key(self, xblock)
return hashlib.sha1(key.encode('utf-8')).hexdigest()
def _get_default_value_to_cache(self, xblock):
"""
Perform special logic to provide a field's default value for caching.
"""
try:
# pylint: disable=protected-access
return self.from_json(xblock._field_data.default(xblock, self.name))
except KeyError:
if self._default is UNIQUE_ID:
return self._check_or_enforce_type(self._calculate_unique_id(xblock))
else:
return self.default
def _sanitize(self, value):
"""
Allow the individual fields to sanitize the value being set -or- "get".
For example, a String field wants to remove control characters.
"""
return value
def __get__(self, xblock, xblock_class):
"""
Gets the value of this xblock. Prioritizes the cached value over
obtaining the value from the field-data service. Thus if a cached value
exists, that is the value that will be returned.
"""
if xblock is None:
return self
field_data = xblock._field_data
value = self._get_cached_value(xblock)
if value is NO_CACHE_VALUE:
if field_data.has(xblock, self.name):
value = self.from_json(field_data.get(xblock, self.name))
elif self.name not in NO_GENERATED_DEFAULTS:
# Cache default value
value = self._get_default_value_to_cache(xblock)
else:
value = self.default
self._set_cached_value(xblock, value)
# If this is a mutable type, mark it as dirty, since mutations can occur without an
# explicit call to __set__ (but they do require a call to __get__)
if self.MUTABLE:
self._mark_dirty(xblock, value)
return self._sanitize(value)
def __set__(self, xblock, value):
"""
Sets the `xblock` to the given `value`.
Setting a value does not update the underlying data store; the
new value is kept in the cache and the xblock is marked as
dirty until `save` is explicitly called.
Note, if there's already a cached value and it's equal to the value
we're trying to cache, we won't do anything.
"""
value = self._check_or_enforce_type(value)
value = self._sanitize(value)
cached_value = self._get_cached_value(xblock)
try:
value_has_changed = cached_value != value
except Exception: # pylint: disable=broad-except
# if we can't compare the values for whatever reason
# (i.e. timezone aware and unaware datetimes), just reset the value.
value_has_changed = True
if value_has_changed:
# Mark the field as dirty and update the cache
self._mark_dirty(xblock, EXPLICITLY_SET)
self._set_cached_value(xblock, value)
def __delete__(self, xblock):
"""
Deletes `xblock` from the underlying data store.
Deletes are not cached; they are performed immediately.
"""
# Try to perform the deletion on the field_data, and accept
# that it's okay if the key is not present. (It may never
# have been persisted at all.)
try:
xblock._field_data.delete(xblock, self.name)
except KeyError:
pass
# We also need to clear this item from the dirty fields, to prevent
# an erroneous write of its value on implicit save. OK if it was
# not in the dirty fields to begin with.
try:
del xblock._dirty_fields[self]
except KeyError:
pass
# Since we know that the field_data no longer contains the value, we can
# avoid the possible database lookup that a future get() call would
# entail by setting the cached value now to its default value.
self._set_cached_value(xblock, self._get_default_value_to_cache(xblock))
def __repr__(self):
return "<{0.__class__.__name__} {0.name}>".format(self)
def _warn_deprecated_outside_JSONField(self): # pylint: disable=invalid-name
"""Certain methods will be moved to JSONField.
This warning marks calls when the object is not
derived from that class.
"""
if not isinstance(self, JSONField) and not self.warned:
warnings.warn(
f"Deprecated. JSONifiable fields should derive from JSONField ({self.name})",
DeprecationWarning,
stacklevel=3
)
self.warned = True
def to_json(self, value):
"""
Return value in the form of nested lists and dictionaries (suitable
for passing to json.dumps).
This is called during field writes to convert the native python
type to the value stored in the database
"""
self._warn_deprecated_outside_JSONField()
return value
def from_json(self, value):
"""
Return value as a native full featured python type (the inverse of to_json)
Called during field reads to convert the stored value into a full featured python
object
"""
self._warn_deprecated_outside_JSONField()
return value
def to_string(self, value):
"""
Return a JSON serialized string representation of the value.
"""
self._warn_deprecated_outside_JSONField()
value = json.dumps(
self.to_json(value),
indent=2,
sort_keys=True,
separators=(',', ': '),
)
return value
def from_string(self, serialized):
"""
Returns a native value from a YAML serialized string representation.
Since YAML is a superset of JSON, this is the inverse of to_string.)
"""
self._warn_deprecated_outside_JSONField()
value = yaml.safe_load(serialized)
return self.enforce_type(value)
def enforce_type(self, value):
"""
Coerce the type of the value, if necessary
Called on field sets to ensure that the stored type is consistent if the
field was initialized with enforce_type=True
This must not have side effects, since it will be executed to trigger
a DeprecationWarning even if enforce_type is disabled
"""
return value
def read_from(self, xblock):
"""
Retrieve the value for this field from the specified xblock
"""
return self.__get__(xblock, xblock.__class__) # pylint: disable=unnecessary-dunder-call
def read_json(self, xblock):
"""
Retrieve the serialized value for this field from the specified xblock
"""
self._warn_deprecated_outside_JSONField()
return self.to_json(self.read_from(xblock))
def write_to(self, xblock, value):
"""
Set the value for this field to value on the supplied xblock
"""
self.__set__(xblock, value) # pylint: disable=unnecessary-dunder-call
def delete_from(self, xblock):
"""
Delete the value for this field from the supplied xblock
"""
self.__delete__(xblock) # pylint: disable=unnecessary-dunder-call
def is_set_on(self, xblock):
"""
Return whether this field has a non-default value on the supplied xblock
"""
# pylint: disable=protected-access
return self._is_dirty(xblock) or xblock._field_data.has(xblock, self.name)
def __hash__(self):
return hash(self.name)
class JSONField(Field):
"""
Field type which has a convenient JSON representation.
"""
# for now; we'll bubble functions down when we finish deprecation in Field
class Integer(JSONField):
"""
A field that contains an integer.
The value, as loaded or enforced, can be None, '' (which will be treated as
None), a Python integer, or a value that will parse as an integer, ie.,
something for which int(value) does not throw an error.
Note that a floating point value will convert to an integer, but a string
containing a floating point number ('3.48') will throw an error.
"""
MUTABLE = False
def from_json(self, value):
if value is None or value == '':
return None
return int(value)
enforce_type = from_json
class Float(JSONField):
"""
A field that contains a float.
The value, as loaded or enforced, can be None, '' (which will be treated as
None), a Python float, or a value that will parse as an float, ie.,
something for which float(value) does not throw an error.
"""
MUTABLE = False
def from_json(self, value):
if value is None or value == '':
return None
return float(value)
enforce_type = from_json
class Boolean(JSONField):
"""
A field class for representing a boolean.
The value, as loaded or enforced, can be either a Python bool, a string, or
any value that will then be converted to a bool in the from_json method.
Examples:
::
True -> True
'true' -> True
'TRUE' -> True
'any other string' -> False
[] -> False
['123'] -> True
None - > False
"""
MUTABLE = False
# We're OK redefining built-in `help`
def __init__(self, help=None, default=UNSET, scope=Scope.content, display_name=None,
**kwargs): # pylint: disable=redefined-builtin
super().__init__(help, default, scope, display_name,
values=({'display_name': "True", "value": True},
{'display_name': "False", "value": False}), **kwargs)
def from_json(self, value):
if isinstance(value, bytes):
value = value.decode('ascii', errors='replace')
if isinstance(value, str):
return value.lower() == 'true'
else:
return bool(value)
enforce_type = from_json
class Dict(JSONField):
"""
A field class for representing a Python dict.
The value, as loaded or enforced, must be either be None or a dict.
"""
_default = {}
def from_json(self, value):
if value is None or isinstance(value, dict):
return value
else:
raise TypeError('Value stored in a Dict must be None or a dict, found %s' % type(value))
enforce_type = from_json
def to_string(self, value):
"""
In python3, json.dumps() cannot sort keys of different types,
so preconvert None to 'null'.
"""
self.enforce_type(value)
if isinstance(value, dict) and None in value:
value = value.copy()
value['null'] = value[None]
del value[None]
return super().to_string(value)
class List(JSONField):
"""
A field class for representing a list.
The value, as loaded or enforced, can either be None or a list.
"""
_default = []
def from_json(self, value):
if value is None or isinstance(value, list):
return value
else:
raise TypeError('Value stored in a List must be None or a list, found %s' % type(value))
enforce_type = from_json
class Set(JSONField):
"""
A field class for representing a set.
The stored value can either be None or a set.
"""
_default = set()
def __init__(self, *args, **kwargs):
"""
Set class constructor.
Redefined in order to convert default values to sets.
"""
super().__init__(*args, **kwargs)
self._default = set(self._default)
def from_json(self, value):
if value is None or isinstance(value, set):
return value
else:
return set(value)
enforce_type = from_json
class String(JSONField):
"""
A field class for representing a string.
The value, as loaded or enforced, can either be None or a basestring instance.
"""
MUTABLE = False
BAD_REGEX = re.compile('[\x00-\x08\x0b\x0c\x0e-\x1f\ud800-\udfff\ufffe\uffff]', flags=re.UNICODE)
def _sanitize(self, value):
"""
Remove the control characters that are not allowed in XML:
https://www.w3.org/TR/xml/#charsets
Leave all other characters.
"""
if isinstance(value, bytes):
value = value.decode('utf-8')
if isinstance(value, str):
if re.search(self.BAD_REGEX, value):
new_value = re.sub(self.BAD_REGEX, "", value)
# The new string will be equivalent to the original string if no control characters are present.
# If equivalent, return the original string - some tests
# check for object equality instead of string equality.
return value if value == new_value else new_value
else:
return value
else:
return value
def from_json(self, value):
if value is None:
return None
elif isinstance(value, (bytes, str)):
return self._sanitize(value)
elif self._is_lazy(value):
# Allow lazily translated strings to be used as String default values.
# The translated values are *not* sanitized.
return value
else:
raise TypeError('Value stored in a String must be None or a string, found %s' % type(value))
def from_string(self, serialized):
"""String gets serialized and deserialized without quote marks."""
return self.from_json(serialized)
def to_string(self, value):
"""String gets serialized and deserialized without quote marks."""
if isinstance(value, bytes):
value = value.decode('utf-8')
return self.to_json(value)
@property
def none_to_xml(self):
"""Returns True to use a XML node for the field and represent None as an attribute."""
return True
enforce_type = from_json
class XMLString(String):
"""
A field class for representing an XML string.
The value, as loaded or enforced, can either be None or a basestring instance.
If it is a basestring instance, it must be valid XML. If it is not valid XML,
an lxml.etree.XMLSyntaxError will be raised.
"""
def to_json(self, value):
"""
Serialize the data, ensuring that it is valid XML (or None).
Raises an lxml.etree.XMLSyntaxError if it is a basestring but not valid
XML.
"""
if self._enable_enforce_type:
value = self.enforce_type(value)
return super().to_json(value)
def enforce_type(self, value):
if value is not None:
etree.XML(value)
return value
class DateTime(JSONField):
"""
A field for representing a datetime.
The value, as loaded or enforced, can either be an ISO-formatted date string, a native datetime,
a timedelta (for a time relative to course start), or None.
"""
DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%f'
def from_json(self, value):
"""
Parse the date from an ISO-formatted date string, or None.
"""
if value is None:
return None
if isinstance(value, bytes):
value = value.decode('utf-8')
if isinstance(value, str):
# Parser interprets empty string as now by default
if value == "":
return None
try:
value = dateutil.parser.parse(value)
except (TypeError, ValueError):
raise ValueError(f"Could not parse {value} as a date") # pylint: disable= raise-missing-from
# Interpret raw numbers as a relative dates
if isinstance(value, (int, float)):
value = datetime.timedelta(seconds=value)
if not isinstance(value, (datetime.datetime, datetime.timedelta)):
raise TypeError(
"Value should be loaded from a string, a datetime object, a timedelta object, or None, not {}".format(
type(value)
)
)
if isinstance(value, datetime.datetime):
if value.tzinfo is not None:
return value.astimezone(pytz.utc)
else:
return value.replace(tzinfo=pytz.utc)
else:
return value
def to_json(self, value):
"""
Serialize the date as an ISO-formatted date string, or None.
"""
if isinstance(value, datetime.datetime):
return value.strftime(self.DATETIME_FORMAT)
if isinstance(value, datetime.timedelta):
return value.total_seconds()