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

SPP12087 help centre structure refactor #385

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
54 changes: 54 additions & 0 deletions sml_builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
app.config["FREEZER_IGNORE_404_NOT_FOUND"] = True
app.config["FREEZER_DEFAULT_MIMETYPE"] = "text/html"
app.config["FREEZER_DESTINATION"] = "../build"
app.cache = {}

# F401 module import but unused
# We have to import the modules below here and they get
Expand Down Expand Up @@ -84,3 +85,56 @@ def set_variables():
return {
"docs_integration_active": docs_integration["enabled"],
}


# On request the method runs, pulling the help centre pages from contentful and constructs the
# navigation dictionary which creates the headers and links available on the help centre.
# On first run it removes itself from the before_request_funcs list to prevent it
# running on subsequent requests.
@app.before_request
gibbardsteve marked this conversation as resolved.
Show resolved Hide resolved
def build_help_centre_structure():
gibbardsteve marked this conversation as resolved.
Show resolved Hide resolved
category_urls = {
"Information": "information",
"Access (and usage)": "access",
"Feedback": "feedback",
"Support": "support",
}
# Remove this method from the before_requests_funcs to prevent it from running on every user request
app.before_request_funcs[None].remove(build_help_centre_structure)
gibbardsteve marked this conversation as resolved.
Show resolved Hide resolved
if content_management["enabled"]:
nav = {"categories": []}
contents = getContent("helpCentreInformation")
submit_request = getContent("helpCentreMethodRequest")
unique = set(d["help_centre_category"] for d in contents)
for category in unique:
nav["categories"].append(
{
"name": category_urls[category],
"label": category,
"subcategories": [],
}
)
for content in contents:
if content["help_centre_category"] == category:
nav["categories"][-1]["subcategories"].append(
{"name": content["id"], "label": content["title"]}
)
if not checkEmptyList(submit_request):
if category == "Information" and not any(
d["name"] == "methods-request"
for d in nav["categories"][-1]["subcategories"]
):
nav["categories"][-1]["subcategories"].append(
{
"name": "methods-request",
"label": "Submit a method request",
}
)
nav["categories"] = sorted(nav["categories"], key=lambda x: x["name"])
# sort headers and links alphabetically, contentful returns content based on last updated date
# so order would constantly change
for category in nav["categories"]:
category["subcategories"] = sorted(
category["subcategories"], key=lambda x: x["label"]
)
app.cache["help_centre_nav"] = nav
18 changes: 12 additions & 6 deletions sml_builder/help_centre.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
def help_centre(category=None):
try:
if content_management["enabled"]:
contents = getContent("helpCentreStructure")["structure"]
contents = app.cache["help_centre_nav"]
if checkEmptyList(contents):
_page_not_found("helpCentreStructure content not found")
_page_not_found(
"helpCentreStructure content encountered an error whilst building"
)
else:
with open(
"./content/help_centre/help_centre.json", encoding="utf-8"
Expand All @@ -48,9 +50,11 @@ def guidances(category, sub_category=None):
help_centre_nav = _help_centre_nav(category)

if content_management["enabled"]:
contents = getContent("helpCentreStructure")["structure"]
contents = app.cache["help_centre_nav"]
if checkEmptyList(contents):
_page_not_found("helpCentreStructure content not found")
_page_not_found(
"helpCentreStructure content encountered an error whilst building"
)
category_label, sub_category_label, sub_category = category_labels(
contents, category, sub_category
)
Expand Down Expand Up @@ -140,9 +144,11 @@ def _help_centre_nav(
current_category,
):
if content_management["enabled"]:
contents = getContent("helpCentreStructure")["structure"]
contents = app.cache["help_centre_nav"]
if checkEmptyList(contents):
_page_not_found("helpCentreStructure content not found")
_page_not_found(
"helpCentreStructure content encountered an error whilst building"
)
else:
with open(
"./content/help_centre/help_centre.json", encoding="utf-8"
Expand Down