[api] Reorganise and be more forgiving on yml

This allows for more flexible settings in
custom/menu/[custom|system].yml

And it makes the default values explicit
GON-3874-DD-moodle
Evilham 2022-12-06 18:11:18 +01:00
parent 2368a072d1
commit 53674bfb24
No known key found for this signature in database
GPG Key ID: AE3EE30D970886BF
1 changed files with 52 additions and 41 deletions

View File

@ -52,67 +52,78 @@ class Menu:
self.write_headers() self.write_headers()
write_css() write_css()
def patch_url(self, subdomain_part: str, domain: str, url: str) -> str:
"""
Patch a URL only if it is a relative URL, so it conforms to:
https://[{subdomain}.]{domain}{url}
"""
# Only modify if it is a relative URL
if not url or url.startswith("/") or url.startswith("#"):
# Ensure subdomain doesn't have leading dots
subdomain_part = subdomain_part.lstrip(".")
# Ensure the subdomain ends with a trailing dot
if subdomain_part and not subdomain_part.endswith("."):
subdomain_part = f"{subdomain_part}."
# Actually construct the full URL
url = "".join(
[
"https://",
subdomain_part,
domain,
url,
]
)
return url
""" HEADER & APP MENU """ """ HEADER & APP MENU """
def gen_header(self): def gen_header(self):
domain = application.config["DOMAIN"]
with open(r"menu/system.yaml") as yml: with open(r"menu/system.yaml") as yml:
system = yaml.load(yml, Loader=yaml.FullLoader) system = yaml.load(yml, Loader=yaml.FullLoader)
user_menu = [] user_menu = []
for item in system["user_menu"]: for item in system["user_menu"]:
item["href"] = ( # We pop 'subdomain' so it is not in user_menu
"https://" item["href"] = self.patch_url(
+ item["subdomain"] item.pop("subdomain", ""), domain, item["href"]
+ "."
+ application.config["DOMAIN"]
+ item["href"]
) )
del item["subdomain"]
user_menu.append(item) user_menu.append(item)
user_menu_dict = {
"user_menu": user_menu,
"user_avatar": "https://sso."
+ application.config["DOMAIN"]
+ "/auth/realms/master/avatar-provider",
}
apps_internal = [] apps_internal = []
for app in system["apps_internal"]: for app in system.get("apps_internal", []):
app_href = app["href"] if app.get("href", "") else "/" app_href = app["href"] if app.get("href", "") else "/"
# Only modify href if it is a relative URL
if app_href.startswith("/") or app_href.startswith("#"):
# Get the subdomain part, not using it if it is missing
subdomain_part = ""
if "subdomain" in app:
# We pop 'subdomain' so it is not in apps_internal # We pop 'subdomain' so it is not in apps_internal
# note this includes a trailing dot if defined app["href"] = self.patch_url(app.pop("subdomain", ""), domain, app_href)
subdomain_part = f"{ app.pop('subdomain') }.".lstrip(".")
# Actually construct the full URL
app["href"] = "".join([
"https://",
subdomain_part,
application.config["DOMAIN"],
app_href,
])
apps_internal.append(app) apps_internal.append(app)
with open(r"menu/custom.yaml") as yml: with open(r"menu/custom.yaml") as yml:
custom = yaml.load(yml, Loader=yaml.FullLoader) custom = yaml.load(yml, Loader=yaml.FullLoader)
custom["background_login"] = ( custom["background_login"] = self.patch_url(
"https://api." + application.config["DOMAIN"] + custom["background_login"] "api", domain, custom.get("background_login", "/img/background.png")
) )
custom["logo"] = "https://api." + application.config["DOMAIN"] + custom["logo"] custom["background_login"] = self.patch_url(
custom["product_logo"] = ( "api", domain, custom.get("logo", "/img/logo.png")
"https://api." + application.config["DOMAIN"] + custom["product_logo"] )
custom["product_logo"] = self.patch_url(
"api", domain, custom.get("product_logo", "/img/product-logo.svg")
) )
menudict = {**custom, **{"apps_internal": apps_internal, **user_menu_dict}} menudict = custom
menudict["user"] = {} menudict["user"] = {
menudict["user"]["account"] = ( "account": self.patch_url(
"https://sso." + application.config["DOMAIN"] + system["user"]["account"] "sso", domain, system.get("user", {}).get("account", "")
) ),
menudict["user"]["avatar"] = ( "avatar": self.patch_url(
"https://sso." + application.config["DOMAIN"] + system["user"]["avatar"] "sso", domain, system.get("user", {}).get("avatar", "")
),
}
menudict.update(
{
"apps_internal": apps_internal,
"user_menu": user_menu,
"user_avatar": menudict["user"]["avatar"],
}
) )
return menudict return menudict