def set_config_value(field, value): config_field = ConfigurationField.query.filter_by(field_name=field).first() if config_field is None: config_field = ConfigurationField(field_name=field, field_value=value) db.session.add(config_field) db.session.commit() else: config_field.field_value = value db.session.commit() app.logger.debug("Set configuration field %r to %r" % (field, value))
def set_config_value(field, value): """Sets the configuration value specified in ``field`` to the value given in ``value``. ``value`` should be able to be converted to a string, as it is stored that way internally and transformed on the way out (see :func:load_config_from_db). :param field: The configuration field to change :type field: string :param value: The new value of ``field`` :type value: string """ config_field = ConfigurationField.query.filter_by(field_name=field).first() if config_field is None: config_field = ConfigurationField(field_name=field, field_value=value) db.session.add(config_field) db.session.commit() else: config_field.field_value = value db.session.commit()
def load_config_from_db(): """Loads configuration fields from the database and throws them in the Partify config dict. This should be used to reload configuration values after they have changed. If a configuration field is not in the database that field is automatically populated from a list of defaults.""" default_configuration = { 'DEBUG': True, 'LASTFM_API_KEY': '', 'LASTFM_API_SECRET': '', 'MPD_SERVER_HOSTNAME': 'localhost', 'MPD_SERVER_PORT': 6600, 'PROFILE': False, 'SECRET_KEY': _produce_random_data(), 'SELECTION_SCHEME': 'ROUND_ROBIN', 'SERVER': 'tornado', 'SERVER_HOST': '0.0.0.0', 'SERVER_PORT': 5000, 'SESSION_SALT': _produce_random_data(), 'TESTING': False } # Transformations to be performed on the key in the DB in case it shouldn't be just a string transformations = { 'DEBUG': lambda x: bool(int(x)), 'MPD_SERVER_PORT': int, 'PROFILE': lambda x: bool(int(x)), 'SERVER_PORT': int, 'TESTING': lambda x: bool(int(x)) } all_config_fields = ConfigurationField.query.all() for cfg_field in all_config_fields: field = cfg_field.field_name value = cfg_field.field_value transform = transformations.get(field, lambda x: x) app.config[field] = transform(value) for field, value in default_configuration.iteritems(): if field not in [f.field_name for f in all_config_fields]: app.config[field] = value new_cfg_field = ConfigurationField(field_name=field, field_value=value) db.session.add(new_cfg_field) db.session.commit()