def _determine_active_settings(self, slug, plugin): """Determines which settings are active. Returns a tuple in following order: ``form``, ``old_settings``, ``plugin_obj``, ``active_nav`` """ # Any ideas how to do this better? slug = slug if slug else 'general' active_nav = {} # used to build the navigation plugin_obj = None if plugin is not None: plugin_obj = PluginRegistry.query.filter_by(name=plugin ).first_or_404() active_nav.update( { 'key': plugin_obj.name, 'title': plugin_obj.name.title() } ) form = plugin_obj.get_settings_form() old_settings = plugin_obj.settings elif slug is not None: group_obj = SettingsGroup.query.filter_by(key=slug).first_or_404() active_nav.update({'key': group_obj.key, 'title': group_obj.name}) form = Setting.get_form(group_obj)() old_settings = Setting.get_settings(group_obj) return form, old_settings, plugin_obj, active_nav
def create_settings_from_fixture(fixture): """ Inserts the settings from a fixture into the database. """ for settingsgroup in fixture: group = SettingsGroup( key=settingsgroup[0], name=settingsgroup[1]['name'], description=settingsgroup[1]['description'] ) group.save() for settings in settingsgroup[1]['settings']: setting = Setting( key=settings[0], value=settings[1]['value'], value_type=settings[1]['value_type'], name=settings[1]['name'], description=settings[1]['description'], extra=settings[1].get('extra', ""), # Optional field settingsgroup=group.key ) setting.save()
def post(self, slug=None, plugin=None): form, old_settings, plugin_obj, active_nav = \ self._determine_active_settings(slug, plugin) all_groups = SettingsGroup.query.all() all_plugins = PluginRegistry.query.filter(db.and_( PluginRegistry.values != None, PluginRegistry.enabled == True )).all() if form.validate_on_submit(): new_settings = populate_settings_dict(form, old_settings) if plugin_obj is not None: plugin_obj.update_settings(new_settings) else: Setting.update(settings=new_settings, app=current_app) flash(_("Settings saved."), "success") return render_template( "management/settings.html", form=form, all_groups=all_groups, all_plugins=all_plugins, active_nav=active_nav )
def update_settings_from_fixture(fixture, overwrite_group=False, overwrite_setting=False): """Updates the database settings from a fixture. Returns the updated groups and settings. :param fixture: The fixture which should be inserted/updated. :param overwrite_group: Set this to ``True`` if you want to overwrite the group if it already exists. Defaults to ``False``. :param overwrite_setting: Set this to ``True`` if you want to overwrite the setting if it already exists. Defaults to ``False``. """ updated_settings = {} for settingsgroup in fixture: group = SettingsGroup.query.filter_by(key=settingsgroup[0]).first() if (group is not None and overwrite_group) or group is None: if group is not None: group.name = settingsgroup[1]["name"] group.description = settingsgroup[1]["description"] else: group = SettingsGroup( key=settingsgroup[0], name=settingsgroup[1]["name"], description=settingsgroup[1]["description"]) group.save() updated_settings[group] = [] for settings in settingsgroup[1]["settings"]: setting = Setting.query.filter_by(key=settings[0]).first() if (setting is not None and overwrite_setting) or setting is None: if setting is not None: setting.value = settings[1]["value"] setting.value_type = settings[1]["value_type"] setting.name = settings[1]["name"] setting.description = settings[1]["description"] setting.extra = settings[1].get("extra", "") setting.settingsgroup = group.key else: setting = Setting(key=settings[0], value=settings[1]["value"], value_type=settings[1]["value_type"], name=settings[1]["name"], description=settings[1]["description"], extra=settings[1].get("extra", ""), settingsgroup=group.key) setting.save() updated_settings[group].append(setting) return updated_settings
def post(self, slug=None, plugin=None): form, old_settings, plugin_obj, active_nav = \ self._determine_active_settings(slug, plugin) all_groups = SettingsGroup.query.all() all_plugins = PluginRegistry.query.filter(db.and_( PluginRegistry.values != None, PluginRegistry.enabled == True )).all() if form.validate_on_submit(): new_settings = populate_settings_dict(form, old_settings) if plugin_obj is not None: plugin_obj.update_settings(new_settings) else: Setting.update(settings=new_settings) flash(_("Settings saved."), "success") return render_template( "management/settings.html", form=form, all_groups=all_groups, all_plugins=all_plugins, active_nav=active_nav )
def get(self, slug=None): slug = slug if slug else 'general' # get the currently active group active_group = SettingsGroup.query.filter_by(key=slug).first_or_404() # get all groups - used to build the navigation all_groups = SettingsGroup.query.all() SettingsForm = Setting.get_form(active_group) old_settings = Setting.get_settings(active_group) form = SettingsForm() for key, values in iteritems(old_settings): try: form[key].data = values['value'] except (KeyError, ValueError): pass return render_template( 'management/settings.html', form=form, all_groups=all_groups, active_group=active_group )
def create_settings_from_fixture(fixture): """Inserts the settings from a fixture into the database. Returns the created groups and settings. :param fixture: The fixture which should inserted. """ created_settings = {} for settingsgroup in fixture: group = SettingsGroup( key=settingsgroup[0], name=settingsgroup[1]["name"], description=settingsgroup[1]["description"] ) group.save() created_settings[group] = [] for settings in settingsgroup[1]["settings"]: setting = Setting( key=settings[0], value=settings[1]["value"], value_type=settings[1]["value_type"], name=settings[1]["name"], description=settings[1]["description"], extra=settings[1].get("extra", ""), # Optional field settingsgroup=group.key ) if setting: setting.save() created_settings[group].append(setting) return created_settings
def uninstall_plugin(plugin): plugin = get_plugin_from_all(plugin) if plugin.uninstallable: plugin.uninstall() Setting.invalidate_cache() flash("Plugin {} has been uninstalled.".format(plugin.name), "success") else: flash("Cannot uninstall Plugin {}".format(plugin.name), "danger") return redirect(url_for("management.plugins"))
def post(self, plugin): plugin = get_plugin_from_all(plugin) if not plugin.installed: plugin.install() Setting.invalidate_cache() flash(_("Plugin has been installed."), "success") else: flash(_("Cannot install plugin."), "danger") return redirect(url_for("management.plugins"))
def install_plugin(plugin): plugin = get_plugin_from_all(plugin) if plugin.installable and not plugin.uninstallable: plugin.install() Setting.invalidate_cache() flash(_("Plugin has been installed."), "success") else: flash(_("Cannot install Plugin."), "danger") return redirect(url_for("management.plugins"))
def post(self, slug=None): slug = slug if slug else 'general' # get the currently active group active_group = SettingsGroup.query.filter_by(key=slug).first_or_404() # get all groups - used to build the navigation all_groups = SettingsGroup.query.all() SettingsForm = Setting.get_form(active_group) old_settings = Setting.get_settings(active_group) new_settings = {} form = SettingsForm() if form.validate_on_submit(): for key, values in iteritems(old_settings): try: # check if the value has changed if values['value'] == form[key].data: continue else: new_settings[key] = form[key].data except KeyError: pass Setting.update(settings=new_settings, app=current_app) flash(_('Settings saved.'), 'success') else: for key, values in iteritems(old_settings): try: form[key].data = values['value'] except (KeyError, ValueError): pass return render_template( 'management/settings.html', form=form, all_groups=all_groups, active_group=active_group )
def update_settings_from_fixture(fixture, overwrite_group=False, overwrite_setting=False): """ Updates the database settings from a fixture. Returns the number of updated groups and settings. """ groups_count = 0 settings_count = 0 for settingsgroup in fixture: group = SettingsGroup.query.filter_by(key=settingsgroup[0]).first() if group is not None and overwrite_group or group is None: groups_count += 1 group = SettingsGroup( key=settingsgroup[0], name=settingsgroup[1]['name'], description=settingsgroup[1]['description'] ) group.save() for settings in settingsgroup[1]['settings']: setting = Setting.query.filter_by(key=settings[0]).first() if setting is not None and overwrite_setting or setting is None: settings_count += 1 setting = Setting( key=settings[0], value=settings[1]['value'], value_type=settings[1]['value_type'], name=settings[1]['name'], description=settings[1]['description'], extra=settings[1].get('extra', ""), settingsgroup=group.key ) setting.save() return groups_count, settings_count
def create_settings_from_fixture(fixture): """Inserts the settings from a fixture into the database. :param fixture: The fixture which should inserted. """ for settingsgroup in fixture: group = SettingsGroup(key=settingsgroup[0], name=settingsgroup[1]["name"], description=settingsgroup[1]["description"]) group.save() for settings in settingsgroup[1]["settings"]: setting = Setting( key=settings[0], value=settings[1]["value"], value_type=settings[1]["value_type"], name=settings[1]["name"], description=settings[1]["description"], extra=settings[1].get("extra", ""), # Optional field settingsgroup=group.key) setting.save()
def settings(slug=None): slug = slug if slug else "general" # get the currently active group active_group = SettingsGroup.query.filter_by(key=slug).first_or_404() # get all groups - used to build the navigation all_groups = SettingsGroup.query.all() SettingsForm = Setting.get_form(active_group) old_settings = Setting.get_settings(active_group) new_settings = {} form = SettingsForm() if form.validate_on_submit(): for key, values in iteritems(old_settings): try: # check if the value has changed if values['value'] == form[key].data: continue else: new_settings[key] = form[key].data except KeyError: pass Setting.update(settings=new_settings, app=current_app) flash(_("Settings saved."), "success") else: for key, values in iteritems(old_settings): try: form[key].data = values['value'] except (KeyError, ValueError): pass return render_template("management/settings.html", form=form, all_groups=all_groups, active_group=active_group)
def __len__(self): return len(Setting.as_dict())
def __iter__(self): return iter(Setting.as_dict())
def __setitem__(self, key, value): Setting.update({key.lower(): value})
def __getitem__(self, key): return Setting.as_dict()[key]
def update_settings_from_fixture(fixture, overwrite_group=False, overwrite_setting=False): """Updates the database settings from a fixture. Returns the updated groups and settings. :param fixture: The fixture which should be inserted/updated. :param overwrite_group: Set this to ``True`` if you want to overwrite the group if it already exists. Defaults to ``False``. :param overwrite_setting: Set this to ``True`` if you want to overwrite the setting if it already exists. Defaults to ``False``. """ updated_settings = {} for settingsgroup in fixture: group = SettingsGroup.query.filter_by(key=settingsgroup[0]).first() if (group is not None and overwrite_group) or group is None: if group is not None: group.name = settingsgroup[1]["name"] group.description = settingsgroup[1]["description"] else: group = SettingsGroup( key=settingsgroup[0], name=settingsgroup[1]["name"], description=settingsgroup[1]["description"] ) group.save() for settings in settingsgroup[1]["settings"]: setting = Setting.query.filter_by(key=settings[0]).first() if (setting is not None and overwrite_setting) or setting is None: if setting is not None: setting.value = settings[1]["value"] setting.value_type = settings[1]["value_type"] setting.name = settings[1]["name"] setting.description = settings[1]["description"] setting.extra = settings[1].get("extra", "") setting.settingsgroup = group.key else: setting = Setting( key=settings[0], value=settings[1]["value"], value_type=settings[1]["value_type"], name=settings[1]["name"], description=settings[1]["description"], extra=settings[1].get("extra", ""), settingsgroup=group.key ) setting.save() updated_settings[group] = [] updated_settings[group].append(setting) return updated_settings
def __getitem__(self, key): try: return Setting.as_dict()[key] except KeyError: logger.info(f"Couldn't find setting for key ${key}") return None