From afd5626fe4ce8df9b4aa0ba38107ceeca443c11b Mon Sep 17 00:00:00 2001 From: Dave Page Date: Tue, 3 Feb 2015 11:48:15 +0000 Subject: [PATCH] Add a simple module for storing and retrieving settings from the config database. --- web/pgadmin/__init__.py | 2 +- web/settings/__init__.py | 79 ++++++++++++++++++++++++++++ web/{ => settings}/settings_model.py | 12 +++++ web/setup.py | 2 +- 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 web/settings/__init__.py rename web/{ => settings}/settings_model.py (75%) diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py index 74c355784..6dd711bf9 100644 --- a/web/pgadmin/__init__.py +++ b/web/pgadmin/__init__.py @@ -15,7 +15,7 @@ from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.security import Security, SQLAlchemyUserDatastore, login_required from flask_security.utils import login_user from flask_mail import Mail -from settings_model import db, Role, User +from settings.settings_model import db, Role, User import inspect, imp, logging, os diff --git a/web/settings/__init__.py b/web/settings/__init__.py new file mode 100644 index 000000000..1bb7cc5d2 --- /dev/null +++ b/web/settings/__init__.py @@ -0,0 +1,79 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2014, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +"""Utility functions for storing and retrieving user configuration settings.""" + +from flask import current_app +from flask.ext.login import current_user +from flask.ext.sqlalchemy import SQLAlchemy + +from settings_model import db, Setting + +def set_boolean(setting, value): + """Set a boolean configuration setting for the current user.""" + db.init_app(current_app) + + data = Setting(user_id=current_user.id, setting=setting, boolean_value=value) + + db.session.merge(data) + db.session.commit() + +def set_integer(setting, value): + """Set a string configuration setting for the current user.""" + db.init_app(current_app) + + data = Setting(user_id=current_user.id, setting=setting, integer_value=value) + + db.session.merge(data) + db.session.commit() + +def set_string(setting, value): + """Set a string configuration setting for the current user.""" + db.init_app(current_app) + + data = Setting(user_id=current_user.id, setting=setting, string_value=value) + + db.session.merge(data) + db.session.commit() + +def get_boolean(setting, default=None): + """Retrieve a boolean configuration setting for the current user, or return + the default value specified by the caller.""" + db.init_app(current_app) + + data = Setting.query.filter_by(user_id=current_user.id, setting=setting).first() + + if not data or data.boolean_value is None: + return default + else: + return data.boolean_value + +def get_integer(setting, default=None): + """Retrieve an integer configuration setting for the current user, or return + the default value specified by the caller.""" + db.init_app(current_app) + + data = Setting.query.filter_by(user_id=current_user.id, setting=setting).first() + + if not data or data.integer_value is None: + return default + else: + return data.integer_value + +def get_string(setting, default=None): + """Retrieve a string configuration setting for the current user, or return + the default value specified by the caller.""" + db.init_app(current_app) + + data = Setting.query.filter_by(user_id=current_user.id, setting=setting).first() + + if not data or data.string_value is None: + return default + else: + return data.string_value \ No newline at end of file diff --git a/web/settings_model.py b/web/settings/settings_model.py similarity index 75% rename from web/settings_model.py rename to web/settings/settings_model.py index 6ff9c5e99..05f821b8e 100644 --- a/web/settings_model.py +++ b/web/settings/settings_model.py @@ -21,12 +21,14 @@ roles_users = db.Table('roles_users', class Role(db.Model, RoleMixin): """Define a security role""" + __tablename__ = 'role' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique=True) description = db.Column(db.String(255)) class User(db.Model, UserMixin): """Define a user object""" + __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(255), unique=True) password = db.Column(db.String(255)) @@ -35,3 +37,13 @@ class User(db.Model, UserMixin): confirmed_at = db.Column(db.DateTime()) roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic')) + +class Setting(db.Model): + """Define a setting object""" + __tablename__ = 'setting' + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) + setting = db.Column(db.String(255), primary_key=True) + boolean_value = db.Column(db.Boolean()) + integer_value = db.Column(db.Integer()) + string_value = db.Column(db.String(1024)) + diff --git a/web/setup.py b/web/setup.py index 8603c1412..24f6db938 100644 --- a/web/setup.py +++ b/web/setup.py @@ -14,7 +14,7 @@ from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.security import Security, SQLAlchemyUserDatastore from flask.ext.security.utils import encrypt_password -from settings_model import db, Role, User +from settings.settings_model import db, Role, User import getpass, os, random, sys, string