[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 explicitGON-3874-DD-moodle
parent
2368a072d1
commit
53674bfb24
|
@ -52,67 +52,78 @@ class Menu:
|
|||
self.write_headers()
|
||||
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 """
|
||||
|
||||
def gen_header(self):
|
||||
domain = application.config["DOMAIN"]
|
||||
with open(r"menu/system.yaml") as yml:
|
||||
system = yaml.load(yml, Loader=yaml.FullLoader)
|
||||
|
||||
user_menu = []
|
||||
for item in system["user_menu"]:
|
||||
item["href"] = (
|
||||
"https://"
|
||||
+ item["subdomain"]
|
||||
+ "."
|
||||
+ application.config["DOMAIN"]
|
||||
+ item["href"]
|
||||
# We pop 'subdomain' so it is not in user_menu
|
||||
item["href"] = self.patch_url(
|
||||
item.pop("subdomain", ""), domain, item["href"]
|
||||
)
|
||||
del item["subdomain"]
|
||||
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 = []
|
||||
for app in system["apps_internal"]:
|
||||
for app in system.get("apps_internal", []):
|
||||
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
|
||||
# note this includes a trailing dot if defined
|
||||
subdomain_part = f"{ app.pop('subdomain') }.".lstrip(".")
|
||||
# Actually construct the full URL
|
||||
app["href"] = "".join([
|
||||
"https://",
|
||||
subdomain_part,
|
||||
application.config["DOMAIN"],
|
||||
app_href,
|
||||
])
|
||||
app["href"] = self.patch_url(app.pop("subdomain", ""), domain, app_href)
|
||||
apps_internal.append(app)
|
||||
|
||||
with open(r"menu/custom.yaml") as yml:
|
||||
custom = yaml.load(yml, Loader=yaml.FullLoader)
|
||||
custom["background_login"] = (
|
||||
"https://api." + application.config["DOMAIN"] + custom["background_login"]
|
||||
custom["background_login"] = self.patch_url(
|
||||
"api", domain, custom.get("background_login", "/img/background.png")
|
||||
)
|
||||
custom["logo"] = "https://api." + application.config["DOMAIN"] + custom["logo"]
|
||||
custom["product_logo"] = (
|
||||
"https://api." + application.config["DOMAIN"] + custom["product_logo"]
|
||||
custom["background_login"] = self.patch_url(
|
||||
"api", domain, custom.get("logo", "/img/logo.png")
|
||||
)
|
||||
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["user"] = {}
|
||||
menudict["user"]["account"] = (
|
||||
"https://sso." + application.config["DOMAIN"] + system["user"]["account"]
|
||||
)
|
||||
menudict["user"]["avatar"] = (
|
||||
"https://sso." + application.config["DOMAIN"] + system["user"]["avatar"]
|
||||
menudict = custom
|
||||
menudict["user"] = {
|
||||
"account": self.patch_url(
|
||||
"sso", domain, system.get("user", {}).get("account", "")
|
||||
),
|
||||
"avatar": self.patch_url(
|
||||
"sso", domain, system.get("user", {}).get("avatar", "")
|
||||
),
|
||||
}
|
||||
menudict.update(
|
||||
{
|
||||
"apps_internal": apps_internal,
|
||||
"user_menu": user_menu,
|
||||
"user_avatar": menudict["user"]["avatar"],
|
||||
}
|
||||
)
|
||||
return menudict
|
||||
|
||||
|
|
Loading…
Reference in New Issue