Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Review fix of un-imported Content of Common Cartridge to OLX #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/cc2olx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def convert_one_file(input_file, workspace, link_file=None, passport_file=None):
(str(olx_filename), "course.xml"),
(str(policy_filename), "policies/course/policy.json"),
(str(cartridge.directory / "web_resources"), "/{}/".format(OLX_STATIC_DIR)),
(str(cartridge.directory / "resources"), "/{}/".format(OLX_STATIC_DIR)),
]

# Add static files that are outside of web_resources directory
Expand Down
41 changes: 33 additions & 8 deletions src/cc2olx/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,17 @@ def get_resource_content(self, identifier):

res_type = res["type"]

if res_type == "webcontent":
is_html_resource = False
try:
if re.match(r".*/imscc_xmlv\d+p\d+/learning-application-resource$", res_type):
res_relative_path = res["children"][0].href
res_filename = self._res_filename(res_relative_path)
if res_filename.suffix == ".html":
is_html_resource = True
except IndexError:
logger.info("Content is not html for Resource type: %s ", res_type)

if res_type == "webcontent" or is_html_resource:
res_relative_path = res["children"][0].href
res_filename = self._res_filename(res_relative_path)
if res_filename.suffix == ".html":
Expand All @@ -354,12 +364,21 @@ def get_resource_content(self, identifier):
# So we need to manually copy it to OLX_STATIC_DIR
self.extra_static_files.append(res_relative_path)
olx_static_path = "/{}/{}".format(OLX_STATIC_DIR, res_relative_path)
html = (
'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>'
'</head><body><p><a href="{}" alt="{}">{}<a></p></body></html>'.format(
olx_static_path, res_relative_path, res_relative_path

if imghdr.what(str(res_filename)):
html = (
'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>'
'</head><body><p><img src="{}" alt="{}"></p></body></html>'.format(
olx_static_path, res_relative_path
)
)
else:
html = (
'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>'
'</head><body><p><a href="{}" alt="{}">{}<a></p></body></html>'.format(
olx_static_path, res_relative_path, res_relative_path
)
)
)
return "html", {"html": html}
else:
logger.info("Skipping webcontent: %s", res_filename)
Expand Down Expand Up @@ -791,8 +810,14 @@ def _parse_discussion(self, res, res_type):
tree = filesystem.get_xml_tree(self._res_filename(child.href))
root = tree.getroot()
ns = {"dt": namespaces[res_type]}
data["title"] = root.find("dt:title", ns).text
data["text"] = root.find("dt:text", ns).text
try:
data["title"] = root.find("dt:title", ns).text
except AttributeError:
data["title"] = ''
try:
data["text"] = root.find("dt:text", ns).text
except AttributeError:
data["text"] = ''
elif isinstance(child, ResourceDependency):
data["dependencies"].append(self.get_resource_content(child.identifierref))
return data
4 changes: 3 additions & 1 deletion src/cc2olx/olx.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ def process_ims_cc_filebase(item, html):
"""
Replace $IMS-CC-FILEBASE$ with /static
"""
new_item = urllib.parse.unquote(item).replace("$IMS-CC-FILEBASE$", "/static")

new_item = urllib.parse.unquote(item).replace("$IMS-CC-FILEBASE$../resources", "/static")
new_item = urllib.parse.unquote(new_item).replace("$IMS-CC-FILEBASE$", "/static")
# skip query parameters for static files
new_item = new_item.split("?")[0]
# &amp; is not valid in an URL. But some file seem to have it when it should be &
Expand Down
2 changes: 1 addition & 1 deletion src/cc2olx/qti.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def parse_qti(self):
# when we're getting malformed course (due to a weird Canvas behaviour)
# with equal identifiers. LMS doesn't support blocks with the same identifiers.
data["ident"] = attributes["ident"] + str(i)
data["title"] = attributes["title"]
data["title"] = attributes.get("title", "")

cc_profile = self._parse_problem_profile(problem)
data["cc_profile"] = cc_profile
Expand Down
Binary file added tests/fixtures_data/imscc_file/CREADOR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/fixtures_data/imscc_file/iframe3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><iframe src="https://www.youtube.com/embed/zE-a5eqvlv8" width="727" height="409" frameborder="0" allowfullscreen=""></iframe></p>
8 changes: 8 additions & 0 deletions tests/fixtures_data/imscc_file/imsmanifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,13 @@
<resource identifier="resource_module-|-introduction" type="webcontent" href="wiki_content/module-|-introduction.html">
<file href="wiki_content/module-|-introduction.html"/>
</resource>
<resource identifier="resource_10_video" type="associatedcontent/imscc_xmlv1p2/learning-application-resource"
href="iframe3.html">
<file href="iframe3.html"/>
</resource>
<resource identifier="resource_6_image_file" type="webcontent"
href="CREADOR.png">
<file href="CREADOR.png"/>
</resource>
</resources>
</manifest>
18 changes: 17 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_load_manifest_extracted(imscc_file, settings, temp_workspace_dir):
"version": cartridge_version,
}

assert len(cartridge.resources) == 18
assert len(cartridge.resources) == 20
assert len(cartridge.resources[0]["children"]) == 6
assert isinstance(cartridge.resources[0]["children"][0], ResourceFile)

Expand Down Expand Up @@ -382,3 +382,19 @@ def test_cartridge_get_resource_content(cartridge):
"\n</body>\n</html>\n"
},
)

assert cartridge.get_resource_content("resource_10_video") == (
"html",
{
"html": '<p><iframe src="https://www.youtube.com/embed/zE-a5eqvlv8" width="727" '
'height="409" frameborder="0" allowfullscreen=""></iframe></p>'
},
)

assert cartridge.get_resource_content("resource_6_image_file") == (
"html",
{
"html": '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head>'
'<body><p><img src="/static/CREADOR.png" alt="CREADOR.png"></p></body></html>'
},
)
1 change: 1 addition & 0 deletions tests/test_video_download_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_find_all_video_urls_in_cc(imscc_file):
expected = [
"https://cdnapisec.kaltura.com/p/2019031/sp/201903100/playManifest/entryId/1_zeqnrfgw/format/url/protocol/https", # noqa: E501
"https://cdnapisec.kaltura.com/p/2019031/sp/201903100/playManifest/entryId/1_zeqnrfgw/format/url/protocol/https", # noqa: E501
"https://www.youtube.com/watch?v=zE-a5eqvlv8",
]
assert urls == expected

Expand Down