def update_plugin_settings(plugin_id, settings, library_id=None): """ Updates the settings for the requested plugin_id :param plugin_id: :param settings: :param library_id: :return: """ # Fetch plugin info (and settings if any) plugin_data = prepare_plugin_info_and_settings(plugin_id, library_id=library_id) # If no plugin data was found for the posted plugin table ID, then return a failure response if not plugin_data: return False # Loop over all plugin settings in order to find matches in the posted params settings_to_save = {} for s in settings: key = s.get('key') key_id = s.get('key_id') input_type = s.get('input_type') # Check if setting is in params value = s.get('value') # Check if value should be boolean if input_type == 'checkbox': if isinstance(value, str): value = True if value.lower() == 'true' else False elif isinstance(value, int): value = True if value > 0 else False # Add that to our dictionary of settings to save settings_to_save[key] = value # If we found settings that need to be saved, save them... if settings_to_save: plugin_executor = PluginExecutor() saved_all_settings = plugin_executor.save_plugin_settings( plugin_data.get('plugin_id'), settings_to_save, library_id=library_id) # If the save function was successful if saved_all_settings: # Update settings in plugin data that will be returned return True return False
def update_plugin_settings(self, *args, **kwargs): return_data = {"success": False} # Fetch ID of plugin to get Info for plugin_id = self.get_argument('plugin_id') # Fetch plugin info (and settings if any) plugin_data = self.__get_plugin_info_and_settings(plugin_id) # If no plugin data was found for the posted plugin table ID, then return a failure response if not plugin_data: return return_data # Create a dictionary of all posted arguments post_params = {} for k, v in self.request.arguments.items(): post_params[k] = v[0].decode("utf-8") # Loop over all plugin settings in order to find matches in the posted params settings_to_save = {} for setting in plugin_data.get('settings'): key = setting.get('key') key_id = setting.get('key_id') input_type = setting.get('input_type') # Check if setting is in params if key_id in post_params: post_value = post_params.get(key_id, '') # Check if value should be boolean if input_type == 'checkbox': post_value = True if post_value.lower( ) == 'true' else False # Add that to our dictionary of settings to save settings_to_save[key] = post_value # If we found settings in the post params that need to be saved, save them... result = False if settings_to_save: plugin_executor = PluginExecutor() saved_all_settings = plugin_executor.save_plugin_settings( plugin_data.get('plugin_id'), settings_to_save) # If the save function was successful if saved_all_settings: # Update settings in plugin data that will be returned plugin_data['settings'] = settings_to_save result = True self.write(json.dumps({"success": result, "plugin_info": plugin_data}))
def save_library_config(library_id, library_config=None, plugin_config=None): """ Save a complete library configuration :param library_id: :param library_config: :param plugin_config: :return: """ # Parse library config if plugin_config is None: plugin_config = {} if library_config is None: library_config = {} # Check if this save requires a new library entry if int(library_id) > 0: # Fetch existing library by ID new_library = False library = Library(library_id) else: # Create a new library with required data new_library = True library = Library.create({ 'name': library_config.get('name'), 'path': library_config.get('path'), }) library_id = library.get_id() # Update library config (if the data was given) if library_config: library.set_name(library_config.get('name', library.get_name())) library.set_path(library_config.get('path', library.get_path())) library.set_locked(library_config.get('locked', library.get_locked())) library.set_enable_remote_only( library_config.get('enable_remote_only', library.get_enable_remote_only())) library.set_enable_scanner( library_config.get('enable_scanner', library.get_enable_scanner())) library.set_enable_inotify( library_config.get('enable_inotify', library.get_enable_inotify())) library.set_priority_score( library_config.get('priority_score', library.get_priority_score())) library.set_tags(library_config.get('tags', library.get_tags())) # Update enabled plugins (if the data was given) enabled_plugins = plugin_config.get('enabled_plugins') if enabled_plugins is not None: # Ensure plugins are installed (install them if they are not) repo_refreshed = False for ep in enabled_plugins: if not plugins.check_if_plugin_is_installed(ep.get('plugin_id')): # Trigger plugin repo refresh if this is the first install if not repo_refreshed: plugins.reload_plugin_repos_data() repo_refreshed = True # Install the plugin if not plugins.install_plugin_by_id(ep.get('plugin_id')): if new_library: library.delete() raise Exception( "Failed to install plugin by plugin ID '{}'".format( ep.get('plugin_id'))) # Enable the plugins against this library library.set_enabled_plugins(enabled_plugins) # Import settings plugin_executor = PluginExecutor() for ep in enabled_plugins: if ep.get('has_config'): plugin_executor.save_plugin_settings(ep.get('plugin_id'), ep.get('settings', {}), library_id=library_id) # Update plugin flow (if the data was given) plugin_flow = plugin_config.get('plugin_flow') if plugin_flow is not None: for plugin_type in plugins.get_plugin_types_with_flows(): flow = [] for plugin_id in plugin_flow.get(plugin_type, []): flow.append({'plugin_id': plugin_id}) plugins.save_enabled_plugin_flows_for_plugin_type( plugin_type, library_id, flow) # Save config return library.save()