mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Initial cut at saving/retrieving the main browser layout.
Still needs some work to: - Minimise AJAX calls by writing multiple settings at once. - Move the settings storage/retrieval JS code into a global file - Avoid using synchronous AJAX calls in the main thread
This commit is contained in:
37
web/pgadmin/settings/__init__.py
Normal file
37
web/pgadmin/settings/__init__.py
Normal file
@@ -0,0 +1,37 @@
|
||||
##########################################################################
|
||||
#
|
||||
# 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 store_setting(setting, value):
|
||||
"""Set a configuration setting for the current user."""
|
||||
db.init_app(current_app)
|
||||
|
||||
data = Setting(user_id=current_user.id, setting=setting, value=value)
|
||||
|
||||
db.session.merge(data)
|
||||
db.session.commit()
|
||||
|
||||
def get_setting(setting, default=None):
|
||||
"""Retrieve a 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.value is None:
|
||||
return default
|
||||
else:
|
||||
return data.value
|
||||
47
web/pgadmin/settings/settings_model.py
Normal file
47
web/pgadmin/settings/settings_model.py
Normal file
@@ -0,0 +1,47 @@
|
||||
##########################################################################
|
||||
#
|
||||
# pgAdmin 4 - PostgreSQL Tools
|
||||
#
|
||||
# Copyright (C) 2013 - 2014, The pgAdmin Development Team
|
||||
# This software is released under the PostgreSQL Licence
|
||||
#
|
||||
##########################################################################
|
||||
|
||||
"""Defines the models for the configuration database."""
|
||||
|
||||
from flask.ext.sqlalchemy import SQLAlchemy
|
||||
from flask.ext.security import UserMixin, RoleMixin
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
# Define models
|
||||
roles_users = db.Table('roles_users',
|
||||
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
|
||||
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
|
||||
|
||||
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))
|
||||
active = db.Column(db.Boolean())
|
||||
active = db.Column(db.Boolean())
|
||||
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)
|
||||
value = db.Column(db.String(1024))
|
||||
|
||||
50
web/pgadmin/settings/views.py
Normal file
50
web/pgadmin/settings/views.py
Normal file
@@ -0,0 +1,50 @@
|
||||
##########################################################################
|
||||
#
|
||||
# pgAdmin 4 - PostgreSQL Tools
|
||||
#
|
||||
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
|
||||
# This software is released under the PostgreSQL Licence
|
||||
#
|
||||
##########################################################################
|
||||
|
||||
"""Views for setting and storing configuration options."""
|
||||
MODULE_NAME = 'settings'
|
||||
|
||||
import config
|
||||
from flask import Blueprint, abort, request
|
||||
from flask.ext.security import login_required
|
||||
|
||||
from . import get_setting, store_setting
|
||||
|
||||
# Initialise the module
|
||||
blueprint = Blueprint(MODULE_NAME, __name__, url_prefix='/' + MODULE_NAME)
|
||||
|
||||
@blueprint.route("/store", methods=['POST'])
|
||||
@blueprint.route("/store/<setting>/<value>", methods=['GET'])
|
||||
@login_required
|
||||
def store(setting=None, value=None):
|
||||
"""Store a configuration setting."""
|
||||
if request.method == 'POST':
|
||||
setting = request.form['setting']
|
||||
value = request.form['value']
|
||||
|
||||
store_setting(setting, value)
|
||||
|
||||
return ''
|
||||
|
||||
@blueprint.route("/get", methods=['POST'])
|
||||
@blueprint.route("/get/<setting>", methods=['GET'])
|
||||
@blueprint.route("/get/<setting>/<default>", methods=['GET'])
|
||||
@login_required
|
||||
def get(setting=None, default=None):
|
||||
"""Get a configuration setting."""
|
||||
if request.method == 'POST':
|
||||
setting = request.form['setting']
|
||||
default = request.form['default']
|
||||
|
||||
try:
|
||||
value = get_setting(setting, default)
|
||||
except:
|
||||
return ''
|
||||
|
||||
return value
|
||||
Reference in New Issue
Block a user