def get_config(): """ This function provides configuration dictionary to views. If config exists in cache returns it otherwise preforms query to db and sets the cache. If cache doesn't exist creates new config object. """ c = cache.get("configuration") if not c: try: config = ConfigModel.select().get() cache.set("configuration", config) return config except: logger.info("No configuration table found in db, creating new one") try: ConfigModel.create_config(**settings) config = ConfigModel.select().get() cache.set("configuration", config) return config except Exception as exc: raise Exception("Error when creating config file. Aborting\n Error: {0}".format(exc)) return c
def configure(): for field in app.config["SETTINGS_FIELDS"]: locals()[field] = request.form.get(field, "").strip().encode("utf-8") for field in app.config["SETTINGS_SWITCHES"]: locals()[field] = True if request.form.get(field) == "on" else False to_update = dict() for key, val in locals().items(): if key in ConfigModel._meta.get_field_names(): to_update[key] = val try: config = ConfigModel.select().get() config.save_settings(**to_update) with app.app_context(): cache.clear() flash("Settings has been updated") return redirect(url_for('account_settings', username=session['user'])) except: handle_errors("Error when saving configuration") return redirect(url_for("account_settings", username=session["user"]))