Skip to content

Commit

Permalink
Fix tutorials key errors
Browse files Browse the repository at this point in the history
  • Loading branch information
carkod committed Nov 30, 2021
1 parent 7056fd0 commit fea187a
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions canonicalwebteam/discourse/parsers/tutorials.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
# Local
from canonicalwebteam.discourse.parsers.base_parser import BaseParser

allowed_tutorial_keys = ["summary", "categories", "difficulty", "author"]


class TutorialParser(BaseParser):
def __init__(self, api, index_topic_id, url_prefix):
self.tutorials = None

self.errors = []
return super().__init__(api, index_topic_id, url_prefix)

def parse(self):
Expand Down Expand Up @@ -132,12 +134,24 @@ def _get_tutorials_topics(self):
)

metadata = {"id": topic[0], "title": topic[1], "link": link}
error_message = None
for row in rows:
key = row.select_one("td:first-child").text.lower()
value = row.select_one("td:last-child").text
metadata[key] = value

tutorial_data.append(metadata)
# Markdown errors made by discourse users
if key not in allowed_tutorial_keys:
error_message = (
f'The tutorial "{topic[1]}" contains an incorrect'
f' key error "{key}", only'
f' {", ".join(allowed_tutorial_keys)} are allowed.'
f'This tutorial has been skipped'
)
self.errors.append(error_message)
break
else:
value = row.select_one("td:last-child").text
metadata[key] = value
if not error_message:
tutorial_data.append(metadata)

# Tutorial will be in the same order as in the URLs table
return sorted(tutorial_data, key=lambda x: topics.index(x["id"]))

0 comments on commit fea187a

Please sign in to comment.