From bacce3e65625b733cdcca3960e4241d481240730 Mon Sep 17 00:00:00 2001
From: Calen Pennington <calen.pennington@gmail.com>
Date: Fri, 29 Jun 2012 23:29:36 -0400
Subject: [PATCH] Load module contents from the file specified by the filename
 attribute

---
 common/lib/xmodule/html_module.py |  5 +++++
 common/lib/xmodule/xml_module.py  | 25 ++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/common/lib/xmodule/html_module.py b/common/lib/xmodule/html_module.py
index cf45ec3a186..aac97b5fd3a 100644
--- a/common/lib/xmodule/html_module.py
+++ b/common/lib/xmodule/html_module.py
@@ -24,6 +24,11 @@ class HtmlDescriptor(RawDescriptor):
     """
     mako_template = "widgets/html-edit.html"
     module_class = HtmlModule
+    filename_extension = "html"
 
     js = {'coffee': [resource_string(__name__, 'js/module/html.coffee')]}
     js_module = 'HTML'
+
+    @classmethod
+    def definition_from_file(cls, file, system):
+        return {'data': file.read()}
diff --git a/common/lib/xmodule/xml_module.py b/common/lib/xmodule/xml_module.py
index a224e4391db..79b90c20038 100644
--- a/common/lib/xmodule/xml_module.py
+++ b/common/lib/xmodule/xml_module.py
@@ -56,14 +56,29 @@ class XmlDescriptor(XModuleDescriptor):
     Mixin class for standardized parsing of from xml
     """
 
+    # Extension to append to filename paths
+    filename_extension = 'xml'
+
     @classmethod
     def definition_from_xml(cls, xml_object, system):
         """
         Return the definition to be passed to the newly created descriptor
         during from_xml
+
+        xml_object: An etree Element
         """
         raise NotImplementedError("%s does not implement definition_from_xml" % cls.__name__)
 
+    @classmethod
+    def definition_from_file(cls, file, system):
+        """
+        Return the definition to be passed to the newly created descriptor
+        during from_xml
+
+        file: File pointer
+        """
+        return cls.definition_from_xml(etree.parse(file), system)
+
     @classmethod
     def from_xml(cls, xml_data, system, org=None, course=None):
         """
@@ -93,9 +108,17 @@ class XmlDescriptor(XModuleDescriptor):
 
             return metadata
 
+        def definition_loader():
+            filename = xml_object.get('filename')
+            if filename is None:
+                return cls.definition_from_xml(xml_object, system)
+            else:
+                filepath = '{type}/{name}.{ext}'.format(type=xml_object.tag, name=filename, ext=cls.filename_extension)
+                return cls.definition_from_file(system.resources_fs.open(filepath), system)
+
         return cls(
             system,
-            LazyLoadingDict(lambda: cls.definition_from_xml(xml_object, system)),
+            LazyLoadingDict(definition_loader),
             location=['i4x',
                       org,
                       course,
-- 
GitLab