mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Introduced a PgAdmin class inherited from the Flask, which looks for
submodules inherited from the PgAdminModule instead of regular Blueprint. This allows us to load the module automatically from the under the pgadmin directory, and will work to extend the pgAdmin extension module. PgAdminModule is inherited from the Blueprint, and bring several methods: - get_own_stylesheets, which returns the stylesheets used by the module (excluding its submodules stylesheets) - get_own_javascripts - menu_items, which returns a dictionray mapping the old hook names (context_items etc) to a list of MenuItem instances For more specialized modules (as for now, any module that should be part of the browser tree construction), one can define an abstract base class defining additional methods. For example, the BrowserPluginModule abstract base class defines the following methods: - jssnippets - csssnipeets - node_type - get_nodes
This commit is contained in:
committed by
Ashesh Vashi
parent
9e0b011ec8
commit
eb6580b43a
@@ -14,20 +14,95 @@ from flask.ext.login import current_user
|
||||
from flask.ext.sqlalchemy import SQLAlchemy
|
||||
|
||||
from settings_model import db, Setting
|
||||
import traceback
|
||||
from flask import Blueprint, Response, abort, request, render_template
|
||||
from flask.ext.security import login_required
|
||||
|
||||
import config
|
||||
from pgadmin.utils.ajax import make_json_response
|
||||
from pgadmin.utils import PgAdminModule
|
||||
|
||||
MODULE_NAME = 'settings'
|
||||
|
||||
def store_setting(setting, value):
|
||||
"""Set a configuration setting for the current user."""
|
||||
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
|
||||
"""Retrieve a configuration setting for the current user, or return the
|
||||
default value specified by the caller."""
|
||||
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
|
||||
return data.value
|
||||
|
||||
# Initialise the module
|
||||
blueprint = PgAdminModule(MODULE_NAME, __name__, template_folder='templates', url_prefix='/' + MODULE_NAME)
|
||||
|
||||
@blueprint.route("/settings.js")
|
||||
@login_required
|
||||
def script():
|
||||
"""Render the required Javascript"""
|
||||
return Response(response=render_template("settings/settings.js"),
|
||||
status=200,
|
||||
mimetype="application/javascript")
|
||||
|
||||
@blueprint.route("/store", methods=['POST'])
|
||||
@blueprint.route("/store/<setting>/<value>", methods=['GET'])
|
||||
@login_required
|
||||
def store(setting=None, value=None):
|
||||
"""Store a configuration setting, or if this is a POST request and a
|
||||
count value is present, store multiple settings at once."""
|
||||
success = 1
|
||||
errorcode = 0
|
||||
errormsg = ''
|
||||
|
||||
try:
|
||||
if request.method == 'POST':
|
||||
if 'count' in request.form:
|
||||
for x in range(int(request.form['count'])):
|
||||
store_setting(request.form['setting%d' % (x+1)], request.form['value%d' % (x+1)])
|
||||
else:
|
||||
store_setting(request.form['setting'], request.form['value'])
|
||||
else:
|
||||
store_setting(setting, value)
|
||||
except Exception as e:
|
||||
success = 0
|
||||
errormsg = e.message
|
||||
|
||||
return make_json_response(success=success,
|
||||
errormsg=errormsg,
|
||||
info=traceback.format_exc(),
|
||||
result=request.form)
|
||||
|
||||
@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']
|
||||
|
||||
success = 1
|
||||
errorcode = 0
|
||||
errormsg = ''
|
||||
|
||||
try:
|
||||
value = get_setting(setting, default)
|
||||
except Exception as e:
|
||||
success = 0
|
||||
errormsg = e.message
|
||||
|
||||
return make_json_response(success=success,
|
||||
errormsg=errormsg,
|
||||
info=traceback.format_exc(),
|
||||
result=request.form)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user