from os.path import basename

from website import settings

def serialize_addon_config(config, user):
    lookup = config.template_lookup

    return {
        'addon_short_name': config.short_name,
        'addon_full_name': config.full_name,
        'node_settings_template': lookup.get_template(basename(config.node_settings_template)),
        'user_settings_template': lookup.get_template(basename(config.user_settings_template)),
        'is_enabled': user.get_addon(config.short_name) is not None,
    }

def get_addons_by_config_type(config_type, user):
    addons = [addon for addon in settings.ADDONS_AVAILABLE if config_type in addon.configs]
    addon_settings = []
    for addon_config in sorted(addons, key=lambda cfg: cfg.full_name.lower()):
        # short_name = addon_config.short_name
        config = serialize_addon_config(addon_config, user)
        '''
        user_settings = user.get_addon(short_name)
        if user_settings:
            user_settings = user_settings.to_json(user)
        config.update({
            'user_settings': user_settings or {}
        })
        '''
        addon_settings.append(config)
    return addon_settings

from os.path import basename

from website import settings

def serialize_addon_config(config, user):
    lookup = config.template_lookup

    user_addon = user.get_addon(config.short_name)
    ret = {
        'addon_short_name': config.short_name,
        'addon_full_name': config.full_name,
        'node_settings_template': lookup.get_template(basename(config.node_settings_template)),
        'user_settings_template': lookup.get_template(basename(config.user_settings_template)),
        'is_enabled': user_addon is not None,
    }
    ret.update(user_addon.to_json(user) if user_addon else {})
    return ret

def get_addons_by_config_type(config_type, user):
    addons = [addon for addon in settings.ADDONS_AVAILABLE if config_type in addon.configs]
    return [serialize_addon_config(addon_config, user) for addon_config in sorted(addons, key=lambda cfg: cfg.full_name.lower())]

Add user_settings to serialized addon settings
Add user_settings to serialized addon settings

  from os.path import basename
  
  from website import settings
  
  def serialize_addon_config(config, user):
      lookup = config.template_lookup
  
+     user_addon = user.get_addon(config.short_name)
-     return {
+     ret = {
          'addon_short_name': config.short_name,
          'addon_full_name': config.full_name,
          'node_settings_template': lookup.get_template(basename(config.node_settings_template)),
          'user_settings_template': lookup.get_template(basename(config.user_settings_template)),
-         'is_enabled': user.get_addon(config.short_name) is not None,
+         'is_enabled': user_addon is not None,
      }
+     ret.update(user_addon.to_json(user) if user_addon else {})
+     return ret
  
  def get_addons_by_config_type(config_type, user):
      addons = [addon for addon in settings.ADDONS_AVAILABLE if config_type in addon.configs]
+     return [serialize_addon_config(addon_config, user) for addon_config in sorted(addons, key=lambda cfg: cfg.full_name.lower())]
-     addon_settings = []
-     for addon_config in sorted(addons, key=lambda cfg: cfg.full_name.lower()):
-         # short_name = addon_config.short_name
-         config = serialize_addon_config(addon_config, user)
-         '''
-         user_settings = user.get_addon(short_name)
-         if user_settings:
-             user_settings = user_settings.to_json(user)
-         config.update({
-             'user_settings': user_settings or {}
-         })
-         '''
-         addon_settings.append(config)
-     return addon_settings
  
Add user_settings to serialized addon settings
## Code Before:
from os.path import basename

from website import settings

def serialize_addon_config(config, user):
    lookup = config.template_lookup

    return {
        'addon_short_name': config.short_name,
        'addon_full_name': config.full_name,
        'node_settings_template': lookup.get_template(basename(config.node_settings_template)),
        'user_settings_template': lookup.get_template(basename(config.user_settings_template)),
        'is_enabled': user.get_addon(config.short_name) is not None,
    }

def get_addons_by_config_type(config_type, user):
    addons = [addon for addon in settings.ADDONS_AVAILABLE if config_type in addon.configs]
    addon_settings = []
    for addon_config in sorted(addons, key=lambda cfg: cfg.full_name.lower()):
        # short_name = addon_config.short_name
        config = serialize_addon_config(addon_config, user)
        '''
        user_settings = user.get_addon(short_name)
        if user_settings:
            user_settings = user_settings.to_json(user)
        config.update({
            'user_settings': user_settings or {}
        })
        '''
        addon_settings.append(config)
    return addon_settings

## Instruction:
Add user_settings to serialized addon settings
## Code After:
from os.path import basename

from website import settings

def serialize_addon_config(config, user):
    lookup = config.template_lookup

    user_addon = user.get_addon(config.short_name)
    ret = {
        'addon_short_name': config.short_name,
        'addon_full_name': config.full_name,
        'node_settings_template': lookup.get_template(basename(config.node_settings_template)),
        'user_settings_template': lookup.get_template(basename(config.user_settings_template)),
        'is_enabled': user_addon is not None,
    }
    ret.update(user_addon.to_json(user) if user_addon else {})
    return ret

def get_addons_by_config_type(config_type, user):
    addons = [addon for addon in settings.ADDONS_AVAILABLE if config_type in addon.configs]
    return [serialize_addon_config(addon_config, user) for addon_config in sorted(addons, key=lambda cfg: cfg.full_name.lower())]
