From 40f19c8399f661e2eab0e6e472b96b899deb6d3b Mon Sep 17 00:00:00 2001 From: gustavomm19 Date: Wed, 28 Aug 2024 12:00:00 +0000 Subject: [PATCH] fix expandable serializer bug with lessons ipynb --- breathecode/registry/serializers.py | 26 ++++++- .../registry/tests/urls/v1/tests_asset.py | 74 ++++++++++++++++++- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/breathecode/registry/serializers.py b/breathecode/registry/serializers.py index 607dacf64..fdf0259b9 100644 --- a/breathecode/registry/serializers.py +++ b/breathecode/registry/serializers.py @@ -1,3 +1,4 @@ +import re from urllib.parse import urlparse from django.utils import timezone @@ -444,10 +445,31 @@ def to_value(self, instance): elem["technologies"] = self.format_technologies(obj) if "readme" in self.expand: - readme = obj.get_readme(parse=True, remove_frontmatter=True) + url = obj.readme_url + if url is None and obj.asset_type == "LESSON": + url = obj.url + + params = {} + extension = None + if url is not None: + # Extract the extension of the file + ext_extractor = re.compile(r"(?:\.([^.]+))?$") + match = ext_extractor.search(url) + extension = match.group(1) + + if extension is not None and extension != "ipynb": + params["parse"] = True + params["remove_frontmatter"] = True + + readme = obj.get_readme(**params) + + html = obj.html + if html is None: + html = readme["html"] if "html" in readme else None + elem["readme"] = { "decoded": readme["decoded"] if "decoded" in readme else None, - "html": readme["html"] if "html" in readme else None, + "html": html, } return data diff --git a/breathecode/registry/tests/urls/v1/tests_asset.py b/breathecode/registry/tests/urls/v1/tests_asset.py index c1c3be902..4805cc93c 100644 --- a/breathecode/registry/tests/urls/v1/tests_asset.py +++ b/breathecode/registry/tests/urls/v1/tests_asset.py @@ -176,9 +176,10 @@ def test_assets_expand_technologies(bc: Breathecode, client): assert bc.database.list_of("registry.Asset") == bc.format.to_dict(model.asset) -def test_assets_expand_readme(bc: Breathecode, client): +def test_assets_expand_readme_no_readme_url(bc: Breathecode, client): technology = {"slug": "learn-react", "title": "Learn React"} + model = bc.database.create( asset_technology=(1, technology), asset=( @@ -191,6 +192,39 @@ def test_assets_expand_readme(bc: Breathecode, client): response = client.get(url) json = response.json() + asset_readme = model.asset.get_readme() + + expected = [ + get_mid_serializer( + model.asset, + data={ + "updated_at": bc.datetime.to_iso_string(model.asset.updated_at), + "readme": {"decoded": asset_readme["decoded"], "html": None}, + }, + ) + ] + + assert json == expected + assert bc.database.list_of("registry.Asset") == [bc.format.to_dict(model.asset)] + + +def test_assets_expand_readme(bc: Breathecode, client): + + technology = {"slug": "learn-react", "title": "Learn React"} + readme_url = "https://github.com/4GeeksAcademy/03-probability-binomial-with-python.md" + + model = bc.database.create( + asset_technology=(1, technology), + asset=( + 1, + {"technologies": 1, "status": "PUBLISHED", "readme": readme, "readme_url": readme_url}, + ), + ) + + url = reverse_lazy("registry:asset") + f"?expand=readme" + response = client.get(url) + json = response.json() + asset_readme = model.asset.get_readme(parse=True, remove_frontmatter=True) expected = [ @@ -207,14 +241,50 @@ def test_assets_expand_readme(bc: Breathecode, client): assert bc.database.list_of("registry.Asset") == [bc.format.to_dict(model.asset)] +def test_assets_expand_readme_ipynb(bc: Breathecode, client): + + technology = {"slug": "learn-react", "title": "Learn React"} + readme_url_ipynb = "https://github.com/4GeeksAcademy/03-probability-binomial-with-python.ipynb" + html = "

hello

" + + model = bc.database.create( + asset_technology=(1, technology), + asset=( + 1, + {"technologies": 1, "status": "PUBLISHED", "readme": readme, "readme_url": readme_url_ipynb, "html": html}, + ), + ) + + url = reverse_lazy("registry:asset") + f"?expand=readme" + response = client.get(url) + json = response.json() + + asset_readme = model.asset.get_readme() + print(asset_readme) + + expected = [ + get_mid_serializer( + model.asset, + data={ + "updated_at": bc.datetime.to_iso_string(model.asset.updated_at), + "readme": {"decoded": asset_readme["decoded"], "html": model.asset.html}, + }, + ) + ] + + assert json == expected + assert bc.database.list_of("registry.Asset") == [bc.format.to_dict(model.asset)] + + def test_assets_expand_readme_and_technologies(bc: Breathecode, client): technology = {"slug": "learn-react", "title": "Learn React"} + readme_url = "https://github.com/4GeeksAcademy/03-probability-binomial-with-python.md" model = bc.database.create( asset_technology=(1, technology), asset=( 1, - {"technologies": 1, "status": "PUBLISHED", "readme": readme}, + {"technologies": 1, "status": "PUBLISHED", "readme": readme, "readme_url": readme_url}, ), )