Skip to content

Commit

Permalink
Merge pull request breatheco-de#1447 from gustavomm19/expandable-seri…
Browse files Browse the repository at this point in the history
…alizer

fix expandable serializer bug with lessons ipynb
  • Loading branch information
jefer94 authored Aug 28, 2024
2 parents 3fc44a9 + 40f19c8 commit d9b9b48
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
26 changes: 24 additions & 2 deletions breathecode/registry/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from urllib.parse import urlparse

from django.utils import timezone
Expand Down Expand Up @@ -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
Expand Down
74 changes: 72 additions & 2 deletions breathecode/registry/tests/urls/v1/tests_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=(
Expand All @@ -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 = [
Expand All @@ -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 = "<h1>hello</h1>"

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},
),
)

Expand Down

0 comments on commit d9b9b48

Please sign in to comment.