Skip to content

Commit d578970

Browse files
committed
chore: chore
1 parent 862e4e1 commit d578970

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

‎xblocks_contrib/video/utils.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import json
2+
3+
from xblock.fields import Scope
4+
5+
6+
def deserialize_field(field, value):
7+
"""
8+
Deserialize the string version to the value stored internally.
9+
10+
Note that this is not the same as the value returned by from_json, as model types typically store
11+
their value internally as JSON. By default, this method will return the result of calling json.loads
12+
on the supplied value, unless json.loads throws a TypeError, or the type of the value returned by json.loads
13+
is not supported for this class (from_json throws an Error). In either of those cases, this method returns
14+
the input value.
15+
"""
16+
try:
17+
deserialized = json.loads(value)
18+
if deserialized is None:
19+
return deserialized
20+
try:
21+
field.from_json(deserialized)
22+
return deserialized
23+
except (ValueError, TypeError):
24+
# Support older serialized version, which was just a string, not result of json.dumps.
25+
# If the deserialized version cannot be converted to the type (via from_json),
26+
# just return the original value. For example, if a string value of '3.4' was
27+
# stored for a String field (before we started storing the result of json.dumps),
28+
# then it would be deserialized as 3.4, but 3.4 is not supported for a String
29+
# field. Therefore field.from_json(3.4) will throw an Error, and we should
30+
# actually return the original value of '3.4'.
31+
return value
32+
33+
except (ValueError, TypeError):
34+
# Support older serialized version.
35+
return value
36+
37+
def own_metadata(block):
38+
"""
39+
Return a JSON-friendly dictionary that contains only non-inherited field
40+
keys, mapped to their serialized values
41+
"""
42+
return block.get_explicitly_set_fields_by_scope(Scope.settings)

0 commit comments

Comments
 (0)