-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
STUD-640 Handle the case of the double-quotes appearing within the string. We don't need to worry about the whole concatenated ID being double-quoted, since the concatenation was done by overriding xml_module's export method. Use the method already fined in XmlDescriptor.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,7 +293,10 @@ def _parse_youtube(data): | |
pieces = video.split(':') | ||
try: | ||
speed = '%.2f' % float(pieces[0]) # normalize speed | ||
youtube_id = pieces[1] | ||
# Handle the fact that youtube IDs got double-quoted for a period of time. | ||
# Note: we pass in "VideoFields.youtube_id_1_0" so we deserialize as a String-- | ||
# it doesn't matter what the actual speed is for the purposes of deserializing. | ||
youtube_id = VideoDescriptor._deserialize(VideoFields.youtube_id_1_0.name, pieces[1]) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
cahrens
|
||
ret[speed] = youtube_id | ||
except (ValueError, IndexError): | ||
log.warning('Invalid YouTube ID: %s' % video) | ||
|
@@ -310,7 +313,6 @@ def _parse_video_xml(xml_data): | |
model_data = {} | ||
|
||
conversions = { | ||
'show_captions': json.loads, | ||
'start_time': VideoDescriptor._parse_time, | ||
'end_time': VideoDescriptor._parse_time | ||
} | ||
|
@@ -349,6 +351,11 @@ def _parse_video_xml(xml_data): | |
# Convert XML attrs into Python values. | ||
if attr in conversions: | ||
value = conversions[attr](value) | ||
else: | ||
# We export values with json.dumps (well, except for Strings, but | ||
# for about a month we did it for Strings also). | ||
value = VideoDescriptor._deserialize(attr, value) | ||
|
||
model_data[attr] = value | ||
|
||
return model_data | ||
|
@@ -367,6 +374,12 @@ def _parse_time(str_time): | |
seconds=obj_time.tm_sec | ||
).total_seconds() | ||
|
||
@staticmethod | ||
def _deserialize(attr, value): | ||
""" | ||
Handles deserializing values that may have been encoded with json.dumps. | ||
""" | ||
return VideoDescriptor.get_map_for_field(VideoDescriptor, attr).from_xml(value) | ||
|
||
def _create_youtube_string(module): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,8 +165,13 @@ class XmlDescriptor(XModuleDescriptor): | |
|
||
metadata_to_export_to_policy = ('discussion_topics') | ||
|
||
@classmethod | ||
@staticmethod | ||
This comment has been minimized.
Sorry, something went wrong.
vasylnakvasiuk
Contributor
|
||
def get_map_for_field(cls, attr): | ||
""" | ||
Returns a serialize/deserialize AttrMap for the given field of a class. | ||
Searches through fields defined by cls to find one named attr. | ||
""" | ||
for field in set(cls.fields + cls.lms.fields): | ||
if field.name == attr: | ||
from_xml = lambda val: deserialize_field(field, val) | ||
|
@@ -280,7 +285,7 @@ def load_metadata(cls, xml_object): | |
# don't load these | ||
continue | ||
|
||
attr_map = cls.get_map_for_field(attr) | ||
attr_map = XmlDescriptor.get_map_for_field(cls, attr) | ||
metadata[attr] = attr_map.from_xml(val) | ||
return metadata | ||
|
||
|
@@ -291,7 +296,7 @@ def apply_policy(cls, metadata, policy): | |
through the attrmap. Updates the metadata dict in place. | ||
""" | ||
for attr in policy: | ||
attr_map = cls.get_map_for_field(attr) | ||
attr_map = XmlDescriptor.get_map_for_field(cls, attr) | ||
metadata[cls._translate(attr)] = attr_map.from_xml(policy[attr]) | ||
|
||
@classmethod | ||
|
@@ -407,7 +412,7 @@ def val_for_xml(attr): | |
"""Get the value for this attribute that we want to store. | ||
(Possible format conversion through an AttrMap). | ||
""" | ||
attr_map = self.get_map_for_field(attr) | ||
attr_map = XmlDescriptor.get_map_for_field(self, attr) | ||
return attr_map.to_xml(self._model_data[attr]) | ||
|
||
# Add the non-inherited metadata | ||
|
Could we rework
_deserialize
method so, that method can pass types, like String, Integer, Boolean?Cause,
VideoFields.youtube_id_1_0.name
confuses me.