From 800bb421010e001969f04b7905a736c0fc197865 Mon Sep 17 00:00:00 2001 From: Zia Fazal Date: Tue, 26 Dec 2023 12:08:42 +0500 Subject: [PATCH] fix: Fix single xml file upload (#4) * fix: Fix single xml file upload --- openedx_cmi5_xblock/openedx_cmi5_xblock.py | 36 ++++++++++--------- .../static/html/openedx_cmi5_xblock.html | 2 +- openedx_cmi5_xblock/utils/utility.py | 3 +- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/openedx_cmi5_xblock/openedx_cmi5_xblock.py b/openedx_cmi5_xblock/openedx_cmi5_xblock.py index 2198d22..f32a01e 100644 --- a/openedx_cmi5_xblock/openedx_cmi5_xblock.py +++ b/openedx_cmi5_xblock/openedx_cmi5_xblock.py @@ -38,8 +38,6 @@ logger = logging.getLogger(__name__) -CMI5XML_FILENAME = 'cmi5.xml' - def _(text): return text @@ -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, ) @@ -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): @@ -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) @@ -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() @@ -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) @@ -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) diff --git a/openedx_cmi5_xblock/static/html/openedx_cmi5_xblock.html b/openedx_cmi5_xblock/static/html/openedx_cmi5_xblock.html index 3cc9c7c..1d0bfda 100644 --- a/openedx_cmi5_xblock/static/html/openedx_cmi5_xblock.html +++ b/openedx_cmi5_xblock/static/html/openedx_cmi5_xblock.html @@ -7,7 +7,7 @@

{{title}}

diff --git a/openedx_cmi5_xblock/utils/utility.py b/openedx_cmi5_xblock/utils/utility.py index fea74a0..f0fdf2f 100644 --- a/openedx_cmi5_xblock/utils/utility.py +++ b/openedx_cmi5_xblock/utils/utility.py @@ -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