Add a simple module for storing and retrieving settings from the

config database.
This commit is contained in:
Dave Page 2015-02-03 11:48:15 +00:00
parent e4bec5ad1e
commit afd5626fe4
4 changed files with 93 additions and 2 deletions

View File

@ -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

79
web/settings/__init__.py Normal file
View File

@ -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

View File

@ -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))

View File

@ -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