Example #1
0
 def load_config(self):
     config = dict([
         (key, value) for key, value in iteritems(self.options)
         if key in self.cfg.settings and value is not None
     ])
     for key, value in iteritems(config):
         self.cfg.set(key.lower(), value)
Example #2
0
 def load_config(self):
     config = dict([
         (key, value) for key, value in iteritems(self.options)
         if key in self.cfg.settings and value is not None
     ])
     for key, value in iteritems(config):
         self.cfg.set(key.lower(), value)
Example #3
0
def load_plugins(app):
    app.pluggy.add_hookspecs(spec)

    # have to find all the flaskbb modules that are loaded this way
    # otherwise sys.modules might change while we're iterating it
    # because of imports and that makes Python very unhappy
    # we are not interested in duplicated plugins or invalid ones
    # ('None' - appears on py2) and thus using a set
    flaskbb_modules = set(
        module for name, module in iteritems(sys.modules)
        if name.startswith('flaskbb')
    )
    for module in flaskbb_modules:
        app.pluggy.register(module, internal=True)

    try:
        with app.app_context():
            plugins = PluginRegistry.query.all()

    except (OperationalError, ProgrammingError) as exc:
        logger.debug("Database is not setup correctly or has not been "
                     "setup yet.", exc_info=exc)
        return

    for plugin in plugins:
        if not plugin.enabled:
            app.pluggy.set_blocked(plugin.name)

    app.pluggy.load_setuptools_entrypoints('flaskbb_plugins')
    app.pluggy.hook.flaskbb_extensions(app=app)

    loaded_names = set([p[0] for p in app.pluggy.list_name_plugin()])
    registered_names = set([p.name for p in plugins])
    unregistered = [
        PluginRegistry(name=name) for name in loaded_names - registered_names
        # ignore internal FlaskBB modules
        if not name.startswith('flaskbb.') and name != 'flaskbb'
    ]
    with app.app_context():
        db.session.add_all(unregistered)
        db.session.commit()

        removed = 0
        if app.config["REMOVE_DEAD_PLUGINS"]:
            removed = remove_zombie_plugins_from_db()
            logger.info("Removed Plugins: {}".format(removed))

    # we need a copy of it because of
    # RuntimeError: dictionary changed size during iteration
    tasks = celery.tasks.copy()
    disabled_plugins = [
        p.__package__ for p in app.pluggy.get_disabled_plugins()
    ]
    for task_name, task in iteritems(tasks):
        if task.__module__.split(".")[0] in disabled_plugins:
            logger.debug("Unregistering task: '{}'".format(task))
            celery.tasks.unregister(task_name)
Example #4
0
    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
        )
Example #5
0
def populate_settings_form(form, settings):
    for key, value in iteritems(settings):
        try:
            form[key].data = value
        except (KeyError, ValueError):
            pass

    return form
Example #6
0
def populate_settings_form(form, settings):
    for key, value in iteritems(settings):
        try:
            form[key].data = value
        except (KeyError, ValueError):
            pass

    return form
Example #7
0
    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
        )
Example #8
0
def populate_settings_dict(form, settings):
    new_settings = {}
    for key, value in iteritems(settings):
        try:
            # check if the value has changed
            if value == form[key].data:
                continue
            else:
                new_settings[key] = form[key].data
        except KeyError:
            pass

    return new_settings
Example #9
0
def populate_settings_dict(form, settings):
    new_settings = {}
    for key, value in iteritems(settings):
        try:
            # check if the value has changed
            if value == form[key].data:
                continue
            else:
                new_settings[key] = form[key].data
        except KeyError:
            pass

    return new_settings
Example #10
0
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)
Example #11
0
    def update(cls, settings, app=None):
        """Updates the cache and stores the changes in the
        database.

        :param settings: A dictionary with setting items.
        """
        # update the database
        for key, value in iteritems(settings):
            setting = cls.query.filter(Setting.key == key.lower()).first()

            setting.value = value

            db.session.add(setting)
            db.session.commit()

        cls.invalidate_cache()
Example #12
0
    def update(cls, settings, app=None):
        """Updates the cache and stores the changes in the
        database.

        :param settings: A dictionary with setting items.
        """
        # update the database
        for key, value in iteritems(settings):
            setting = cls.query.filter(Setting.key == key.lower()).first()

            setting.value = value

            db.session.add(setting)
            db.session.commit()

        cls.invalidate_cache()
Example #13
0
def app_config_from_env(app, prefix="FLASKBB_"):
    """Retrieves the configuration variables from the environment.
    Set your environment variables like this::

        export FLASKBB_SECRET_KEY="your-secret-key"

    and based on the prefix, it will set the actual config variable
    on the ``app.config`` object.

    :param app: The application object.
    :param prefix: The prefix of the environment variables.
    """
    for key, value in iteritems(os.environ):
        if key.startswith(prefix):
            key = key[len(prefix):]
            try:
                value = ast.literal_eval(value)
            except (ValueError, SyntaxError):
                pass
            app.config[key] = value
    return app
Example #14
0
def app_config_from_env(app, prefix="FLASKBB_"):
    """Retrieves the configuration variables from the environment.
    Set your environment variables like this::

        export FLASKBB_SECRET_KEY="your-secret-key"

    and based on the prefix, it will set the actual config variable
    on the ``app.config`` object.

    :param app: The application object.
    :param prefix: The prefix of the environment variables.
    """
    for key, value in iteritems(os.environ):
        if key.startswith(prefix):
            key = key[len(prefix) :]
            try:
                value = ast.literal_eval(value)
            except (ValueError, SyntaxError):
                pass
            app.config[key] = value
    return app
Example #15
0
def load_plugins(app):
    app.pluggy.add_hookspecs(spec)

    # have to find all the flaskbb modules that are loaded this way
    # otherwise sys.modules might change while we're iterating it
    # because of imports and that makes Python very unhappy
    # we are not interested in duplicated plugins or invalid ones
    # ('None' - appears on py2) and thus using a set
    flaskbb_modules = set(
        module
        for name, module in iteritems(sys.modules)
        if name.startswith("flaskbb")
    )
    for module in flaskbb_modules:
        app.pluggy.register(module, internal=True)

    try:
        with app.app_context():
            plugins = PluginRegistry.query.all()

    except (OperationalError, ProgrammingError) as exc:
        logger.debug(
            "Database is not setup correctly or has not been " "setup yet.",
            exc_info=exc,
        )
        # load plugins even though the database isn't setup correctly
        # i.e. when creating the initial database and wanting to install
        # the plugins migration as well
        app.pluggy.load_setuptools_entrypoints("flaskbb_plugins")
        return

    for plugin in plugins:
        if not plugin.enabled:
            app.pluggy.set_blocked(plugin.name)

    app.pluggy.load_setuptools_entrypoints("flaskbb_plugins")
    app.pluggy.hook.flaskbb_extensions(app=app)

    loaded_names = set([p[0] for p in app.pluggy.list_name_plugin()])
    registered_names = set([p.name for p in plugins])
    unregistered = [
        PluginRegistry(name=name)
        for name in loaded_names - registered_names
        # ignore internal FlaskBB modules
        if not name.startswith("flaskbb.") and name != "flaskbb"
    ]
    with app.app_context():
        db.session.add_all(unregistered)
        db.session.commit()

        removed = 0
        if app.config["REMOVE_DEAD_PLUGINS"]:
            removed = remove_zombie_plugins_from_db()
            logger.info("Removed Plugins: {}".format(removed))

    # we need a copy of it because of
    # RuntimeError: dictionary changed size during iteration
    tasks = celery.tasks.copy()
    disabled_plugins = [
        p.__package__ for p in app.pluggy.get_disabled_plugins()
    ]
    for task_name, task in iteritems(tasks):
        if task.__module__.split(".")[0] in disabled_plugins:
            logger.debug("Unregistering task: '{}'".format(task))
            celery.tasks.unregister(task_name)