Skip to content

Commit

Permalink
fix: Fix single xml file upload (#4)
Browse files Browse the repository at this point in the history
* fix: Fix single xml file upload
  • Loading branch information
ziafazal authored Dec 26, 2023
1 parent d067f91 commit 800bb42
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
36 changes: 19 additions & 17 deletions openedx_cmi5_xblock/openedx_cmi5_xblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@

logger = logging.getLogger(__name__)

CMI5XML_FILENAME = 'cmi5.xml'


def _(text):
return text
Expand Down Expand Up @@ -105,7 +103,8 @@ class CMI5XBlock(XBlock, CompletableXBlockMixin):

width = Integer(
display_name=_('Display width (px)'),
help=_('Width of iframe (default: 100%)'),
help=_('Width of iframe as percentage of container (default: 100)'),
default=100,
scope=Scope.settings,
)

Expand All @@ -116,6 +115,12 @@ class CMI5XBlock(XBlock, CompletableXBlockMixin):
scope=Scope.settings,
)

xml_file_name = String(
help=_('Course structure xml file name'),
default='cmi5.xml',
scope=Scope.settings,
)

has_author_view = True

def author_view(self, context=None):
Expand Down Expand Up @@ -231,8 +236,8 @@ def lrs_auth_endpoint(self, request, _suffix):
def studio_submit(self, request, _suffix):
"""Handles the submission of the CMI5 XBlock studio form."""
self.display_name = request.params['display_name']
self.width = parse_int(request.params['width'], None)
self.height = parse_int(request.params['height'], None)
self.width = parse_int(request.params['width'], 100)
self.height = parse_int(request.params['height'], 450)
self.has_score = request.params['has_score'] == '1'
self.weight = parse_float(request.params['weight'], 1)

Expand Down Expand Up @@ -382,16 +387,12 @@ def index_page_url(self):
if not self.package_meta or not self.index_page_path:
return ''

folder = self.extract_folder_path
if self.storage.exists(os.path.join(self.extract_folder_base_path, self.index_page_path)):
# For backward-compatibility, we must handle the case when the xblock data
# is stored in the base folder.
folder = self.extract_folder_base_path
logger.warning('Serving CMI5 content from old-style path: %s', folder)

lms_cmi5_url = requests.utils.unquote(self.storage.url(os.path.join(folder, self.index_page_path)))
if is_url(self.index_page_path):
lms_cmi5_url = self.index_page_path
else:
folder = self.extract_folder_path
lms_cmi5_url = requests.utils.unquote(self.storage.url(os.path.join(folder, self.index_page_path)))

params_joining_symbol = '&' if is_params_exist(lms_cmi5_url) else '?'
lms_cmi5_url = lms_cmi5_url + params_joining_symbol
return lms_cmi5_url + self.get_launch_url_params()
Expand Down Expand Up @@ -448,7 +449,7 @@ def extract_zip_file(self, package_file):

# Find root folder which contains cmi5.xml
for zipinfo in zipinfos:
if os.path.basename(zipinfo.filename) == CMI5XML_FILENAME:
if os.path.basename(zipinfo.filename) == self.xml_file_name:
depth = len(os.path.split(zipinfo.filename))
if depth < root_depth or root_depth < 0:
root_path = os.path.dirname(zipinfo.filename)
Expand All @@ -465,12 +466,13 @@ def extract_zip_file(self, package_file):

def save_xml_file(self, package_file):
"""Saves an XML file from the CMI5 package."""
dest_path = os.path.join(self.extract_folder_path, package_file.filename)
self.storage.save(dest_path, ContentFile(package_file.filename))
self.xml_file_name = getattr(package_file, 'filename', package_file.name)
dest_path = os.path.join(self.extract_folder_path, self.xml_file_name)
self.storage.save(dest_path, ContentFile(package_file.read()))

def update_package_fields(self):
"""Update version and index page path fields."""
cmi5_path = self.find_file_path(CMI5XML_FILENAME)
cmi5_path = self.find_file_path(self.xml_file_name)
cmi5_file = self.storage.open(cmi5_path)
tree = ET.parse(cmi5_file)
cmi5_file.seek(0)
Expand Down
2 changes: 1 addition & 1 deletion openedx_cmi5_xblock/static/html/openedx_cmi5_xblock.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h3 class="xblock-title mb-2">{{title}}</h3>
<iframe
class="cmi5-embedded"
src="{{ index_page_url }}"
width="{% if cmi5_xblock.width %}{{ cmi5_xblock.width }}{% else %}100%{% endif %}"
width="{% if cmi5_xblock.width %}{{ cmi5_xblock.width }}%{% else %}100%{% endif %}"
height="{% if cmi5_xblock.height %}{{ cmi5_xblock.height }}{% else %}450{% endif %}"
>
</iframe>
Expand Down
3 changes: 2 additions & 1 deletion openedx_cmi5_xblock/utils/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def json_response(data):
def is_url(path):
"""Checks if the given path is a valid URL."""
try:
validator = URLValidator(verify_exists=False)
validator = URLValidator()
validator(path)
except Exception as err:
logger.error("Invalid URL (%s): %s", path, err)
return False
return True

Expand Down

0 comments on commit 800bb42

Please sign in to comment.