From 6c4fd8e28d38c58a719cb70b2b8b6079dc3ae847 Mon Sep 17 00:00:00 2001 From: Oscar Domingo Date: Mon, 29 Jan 2024 12:16:20 +0000 Subject: [PATCH] Fix settings not correctly loading all addons defined in bundle --- api/settings/settings.py | 235 ++++++++++++++++++++------------------- 1 file changed, 119 insertions(+), 116 deletions(-) diff --git a/api/settings/settings.py b/api/settings/settings.py index 51a059dc..4b6fcf2d 100644 --- a/api/settings/settings.py +++ b/api/settings/settings.py @@ -71,137 +71,140 @@ async def get_all_settings( ) -> AllSettingsResponseModel | None: addons_settings = [] - enabled_addons = await AddonsLibrary.get_enabled_addons() - for bundle, bundle_dict in enabled_addons.items(): - if variant not in ("production", "staging") and bundle != variant: - continue - elif bundle_name and bundle != bundle_name: - continue - - for addon_name, addon_version in bundle_dict["addons"]: - addon_name_and_version = slugify(f"{addon_name}-{addon_version}") - addon_library = AddonsLibrary.get_instance() - addon = addon_library.initialized_addons.get(addon_name_and_version, None) - - if not addon: - logging.warning( - f"Addon {addon_name} {addon_version} " - f"declared in {bundle_name} not initialized." - ) + library = AddonsLibrary.get_instance() + + if bundle_name: + addons = await library.get_bundle_addons(bundle_name) + else: + variants_bundle_names = await library.get_variants_bundle_name() + bundle_name = variants_bundle_names.get(variant) + addons = await library.get_bundle_addons(bundle_name) + + for addon in addons: + addon_name_and_version, addon = addon + + if not addon: + addon_name, addon_version = addon_name_and_version.split("-", 1) + logging.warning( + f"Addon {addon_name} {addon_version} " + f"declared in {bundle_name} not initialized." + ) - is_broken = addon_library.broke_addons.get(addon_name_and_version, None) + is_broken = library.broken_addons.get(addon_name_and_version, None) - broken_reason = {"error": "Addon is not initialized"} + broken_reason = {"error": "Addon is not initialized"} - if is_broken: - broken_reason["traceback"] = str(is_broken) + if is_broken: + broken_reason["traceback"] = str(is_broken) - addons_settings.append( - AddonSettingsItemModel( - name=addon_name, - title=addon_name, - version=addon_version, - settings={}, - site_settings=None, - is_broken=bool(is_broken), - reason=broken_reason, - ) + addons_settings.append( + AddonSettingsItemModel( + name=addon_name, + title=addon_name, + version=addon_version, + settings={}, + site_settings=None, + is_broken=bool(is_broken), + reason=broken_reason, ) - continue + ) + continue + + addon_name = addon.name + addon_version = addon.version + # Determine which scopes addon has settings for + model = addon.get_settings_model() + has_settings = False + has_project_settings = False + has_project_site_settings = False + has_site_settings = bool(addon.site_settings_model) - # Determine which scopes addon has settings for - model = addon.get_settings_model() - has_settings = False + if model: has_project_settings = False - has_project_site_settings = False - has_site_settings = bool(addon.site_settings_model) - - if model: - has_project_settings = False - for field_name, field in model.__fields__.items(): - scope = field.field_info.extra.get("scope", ["studio", "project"]) - if "project" in scope: - has_project_settings = True - if "site" in scope: - has_project_site_settings = True - if "studio" in scope: - has_settings = True - - # Load settings for the addon - site_settings = None - settings: BaseSettingsModel | None = None - - try: - if site_id: - site_settings = await addon.get_site_settings(user.name, site_id) - - if project_name is None: - # Studio level settings (studio level does not have) - # site overrides per se but it can have site settings - settings = await addon.get_studio_settings(variant) - else: - # Project and site is requested, so we are returning - # project level settings WITH site overrides - settings = await addon.get_project_site_settings( - project_name, - user.name, - site_id, - variant, - ) - elif project_name: - # Project level settings (no site overrides) - settings = await addon.get_project_settings(project_name, variant) - else: - # Just studio level settings (no project, no site) + for field_name, field in model.__fields__.items(): + scope = field.field_info.extra.get("scope", ["studio", "project"]) + if "project" in scope: + has_project_settings = True + if "site" in scope: + has_project_site_settings = True + if "studio" in scope: + has_settings = True + + # Load settings for the addon + site_settings = None + settings: BaseSettingsModel | None = None + + try: + if site_id: + site_settings = await addon.get_site_settings(user.name, site_id) + + if project_name is None: + # Studio level settings (studio level does not have) + # site overrides per se but it can have site settings settings = await addon.get_studio_settings(variant) - - except Exception: - log_traceback(f"Unable to load {addon_name} {addon_version} settings") - addons_settings.append( - AddonSettingsItemModel( - name=addon_name, - title=addon_name, - version=addon_version, - settings={}, - site_settings=None, - is_broken=True, - reason={ - "error": "Unable to load settings", - "traceback": traceback.format_exc(), - }, + else: + # Project and site is requested, so we are returning + # project level settings WITH site overrides + settings = await addon.get_project_site_settings( + project_name, + user.name, + site_id, + variant, ) - ) - continue - - # Add addon to the result - + elif project_name: + # Project level settings (no site overrides) + settings = await addon.get_project_settings(project_name, variant) + else: + # Just studio level settings (no project, no site) + settings = await addon.get_studio_settings(variant) + + except Exception: + log_traceback(f"Unable to load {addon_name} {addon_version} settings") addons_settings.append( AddonSettingsItemModel( name=addon_name, - title=addon.title if addon.title else addon_name, + title=addon_name, version=addon_version, - # Has settings means that addon has settings model - has_settings=has_settings, - has_project_settings=has_project_settings, - has_project_site_settings=has_project_site_settings, - has_site_settings=has_site_settings, - # Has overrides means that addon has overrides for the requested - # project/site - has_studio_overrides=settings._has_studio_overrides - if settings - else None, - has_project_overrides=settings._has_project_overrides - if settings - else None, - has_site_overrides=settings._has_site_overrides if settings else None, - settings=settings.dict() if (settings and not summary) else {}, - site_settings=site_settings, + settings={}, + site_settings=None, + is_broken=True, + reason={ + "error": "Unable to load settings", + "traceback": traceback.format_exc(), + }, ) ) + continue - addons_settings.sort(key=lambda x: x.title.lower()) - return AllSettingsResponseModel( - bundle_name=bundle, - addons=addons_settings, + # Add addon to the result + + addons_settings.append( + AddonSettingsItemModel( + name=addon_name, + title=addon.title if addon.title else addon_name, + version=addon_version, + # Has settings means that addon has settings model + has_settings=has_settings, + has_project_settings=has_project_settings, + has_project_site_settings=has_project_site_settings, + has_site_settings=has_site_settings, + # Has overrides means that addon has overrides for the requested + # project/site + has_studio_overrides=settings._has_studio_overrides + if settings + else None, + has_project_overrides=settings._has_project_overrides + if settings + else None, + has_site_overrides=settings._has_site_overrides if settings else None, + settings=settings.dict() if (settings and not summary) else {}, + site_settings=site_settings, + ) ) + addons_settings.sort(key=lambda x: x.title.lower()) + return AllSettingsResponseModel( + bundle_name=bundle_name, + addons=addons_settings, + ) +