Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions cms/djangoapps/contentstore/tests/test_contentstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,68 @@ def test_export_course_with_metadata_only_video(self):

shutil.rmtree(root_dir)

def test_export_course_with_metadata_only_word_cloud(self):
"""
Similar to `test_export_course_with_metadata_only_video`.
"""
module_store = modulestore('direct')
draft_store = modulestore('draft')
content_store = contentstore()

import_from_xml(module_store, 'common/test/data/', ['word_cloud'])
location = CourseDescriptor.id_to_location('HarvardX/ER22x/2013_Spring')

verticals = module_store.get_items(['i4x', 'HarvardX', 'ER22x', 'vertical', None, None])

self.assertGreater(len(verticals), 0)

parent = verticals[0]

ItemFactory.create(parent_location=parent.location, category="word_cloud", display_name="untitled")

root_dir = path(mkdtemp_clean())

print 'Exporting to tempdir = {0}'.format(root_dir)

# export out to a tempdir
export_to_xml(module_store, content_store, location, root_dir, 'test_export', draft_modulestore=draft_store)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good, I think, to assert something about the results of the export.

In particular, I think the thing that concerned me about the fix in the raw module is whether it would cause issues for other subclasses of RawDescriptor that didn't expect self.data to be empty, and whether an an export/import pair would leave self.data in the same state.

To be even more specific: If I del foo.data, and then export, and then re-import, won't foo.data == '<foomodule/>', and foo.data != None. That seems like it could cause problems.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpennington Did you mean that it would cause problems for other subclasses of RawDescriptor that DID expect self.data to be empty? If you del foo.data, then I think there would be a problem on export (the same problem that word cloud is having right now). What would be the use case for expecting foo.data to be empty?

Peter can go back to the wordcloud-specific fix, and perhaps that is the best approach. But I am concerned that the same bug will creep up in new xmodules (with no data). Although perhaps we artificially created the no-data situation by too zealously moving things into fields with Scope.settings (vs. Scope.content).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my point was that, given the code in this PR, you can end up w/ a situation where you del foo.data, and then reimport, and foo.data won't be the default value of None, it'll be <foo/>.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix that, we might just want the RawDescriptor to correctly clear out self.data if it's an empty xml element.

But basically, to start, let's get a test that

  1. Deletes the .data attribute of a module
  2. Exports the module
  3. Imports the module
  4. Asserts that .data is None


shutil.rmtree(root_dir)

def test_empty_data_roundtrip(self):
"""
Test that an empty `data` field is preserved through
export/import.
"""
module_store = modulestore('direct')
draft_store = modulestore('draft')
content_store = contentstore()

import_from_xml(module_store, 'common/test/data/', ['toy'])
location = CourseDescriptor.id_to_location('edX/toy/2012_Fall')

verticals = module_store.get_items(['i4x', 'edX', 'toy', 'vertical', None, None])

self.assertGreater(len(verticals), 0)

parent = verticals[0]

# Create a module, and ensure that its `data` field is empty
word_cloud = ItemFactory.create(parent_location=parent.location, category="word_cloud", display_name="untitled")
del word_cloud.data
self.assertEquals(word_cloud.data, '')

# Export the course
root_dir = path(mkdtemp_clean())
export_to_xml(module_store, content_store, location, root_dir, 'test_roundtrip', draft_modulestore=draft_store)

# Reimport and get the video back
import_from_xml(module_store, root_dir)
imported_word_cloud = module_store.get_item(Location(['i4x', 'edX', 'toy', 'word_cloud', 'untitled', None]))

# It should now contain empty data
self.assertEquals(imported_word_cloud.data, '')

def test_course_handouts_rewrites(self):
module_store = modulestore('direct')

Expand Down
19 changes: 19 additions & 0 deletions common/lib/xmodule/xmodule/raw_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,22 @@ def definition_to_xml(self, resource_fs):
context=lines[line - 1][offset - 40:offset + 40],
loc=self.location))
raise Exception, msg, sys.exc_info()[2]


class EmptyDataRawDescriptor(XmlDescriptor, XMLEditingDescriptor):
"""
Version of RawDescriptor for modules which may have no XML data,
but use XMLEditingDescriptor for import/export handling.
"""
data = String(default='', scope=Scope.content)

@classmethod
def definition_from_xml(cls, xml_object, system):
if len(xml_object) == 0 and len(xml_object.items()) == 0:
return {'data': ''}, []
return {'data': etree.tostring(xml_object, pretty_print=True, encoding='unicode')}, []

def definition_to_xml(self, resource_fs):
if self.data:
return etree.fromstring(self.data)
return etree.Element(self.category)
14 changes: 5 additions & 9 deletions common/lib/xmodule/xmodule/video_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.http import Http404

from xmodule.x_module import XModule
from xmodule.raw_module import RawDescriptor
from xmodule.raw_module import EmptyDataRawDescriptor
from xmodule.editing_module import MetadataOnlyEditingDescriptor
from xblock.core import Integer, Scope, String, Float, Boolean

Expand Down Expand Up @@ -97,7 +97,7 @@ def get_html(self):

class VideoDescriptor(VideoFields,
MetadataOnlyEditingDescriptor,
RawDescriptor):
EmptyDataRawDescriptor):
module_class = VideoModule

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -130,19 +130,15 @@ def from_xml(cls, xml_data, system, org=None, course=None):
_parse_video_xml(video, video.data)
return video

def definition_to_xml(self, resource_fs):
"""
Override the base implementation. We don't actually have anything in the 'data' field
(it's an empty string), so we just return a simple XML element
"""
return etree.Element('video')


def _parse_video_xml(video, xml_data):
"""
Parse video fields out of xml_data. The fields are set if they are
present in the XML.
"""
if not xml_data:
return

xml = etree.fromstring(xml_data)

display_name = xml.get('display_name')
Expand Down
4 changes: 2 additions & 2 deletions common/lib/xmodule/xmodule/word_cloud_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging

from pkg_resources import resource_string
from xmodule.raw_module import RawDescriptor
from xmodule.raw_module import EmptyDataRawDescriptor
from xmodule.editing_module import MetadataOnlyEditingDescriptor
from xmodule.x_module import XModule

Expand Down Expand Up @@ -240,7 +240,7 @@ def get_html(self):
return self.content


class WordCloudDescriptor(WordCloudFields, MetadataOnlyEditingDescriptor, RawDescriptor):
class WordCloudDescriptor(WordCloudFields, MetadataOnlyEditingDescriptor, EmptyDataRawDescriptor):
"""Descriptor for WordCloud Xmodule."""
module_class = WordCloudModule
template_dir_name = 'word_cloud'