pgadmin4/web/pgadmin/preferences/__init__.py
Ashesh Vashi ef7737c709 Added missing dependent javascript 'jquery.aciFragment' required by the
aciTree.

Also - fixed few CSS specifically for the preferences dialog.
Returning the sorted preferences from the server, so that - it lists
down in correct order.
2016-04-19 23:48:10 +05:30

142 lines
3.8 KiB
Python

##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2016, The pgAdmin Development Team
#
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""
Implements the routes for creating Preferences/Options Dialog on the client
side and for getting/setting preferences.
"""
from pgadmin.utils import PgAdminModule
from pgadmin.utils.ajax import success_return, \
make_response as ajax_response, internal_server_error
from flask import render_template, url_for, Response, request
from flask.ext.security import login_required
from flask.ext.login import current_user
from flask.ext.babel import gettext
from pgadmin.utils.menu import MenuItem
from pgadmin.utils.preferences import Preferences
import simplejson as json
MODULE_NAME = 'preferences'
class PreferencesModule(PgAdminModule):
"""
PreferenceModule represets the preferences of different modules to the
user in UI.
And, allows the user to modify (not add/remove) as per their requirement.
"""
def get_own_javascripts(self):
return [{
'name': 'pgadmin.preferences',
'path': url_for('preferences.index') + 'preferences',
'when': None
}]
def get_own_stylesheets(self):
return [url_for('preferences.static', filename='css/preferences.css')]
def get_own_menuitems(self):
return {
'file_items': [
MenuItem(name='mnu_preferences',
priority=999,
module="pgAdmin.Preferences",
callback='show',
icon='fa fa-cog',
label=gettext('Preferences'))
]
}
blueprint = PreferencesModule(MODULE_NAME, __name__)
@blueprint.route("/")
@login_required
def index():
"""Render the preferences dialog."""
return render_template(
MODULE_NAME + "/index.html",
username=current_user.email,
_=gettext
)
@blueprint.route("/preferences.js")
@login_required
def script():
"""render the required javascript"""
return Response(response=render_template("preferences/preferences.js", _=gettext),
status=200,
mimetype="application/javascript")
@blueprint.route("/preferences", methods=["GET"])
@login_required
def preferences():
"""Fetch all the preferences of pgAdmin IV."""
# Load Preferences
preferences = Preferences.preferences()
res = []
def label(p):
return p['label']
for m in preferences:
if len(m['categories']):
om = {
"id": m['id'],
"label": m['label'],
"inode": True,
"open": True,
"branch": []
}
for c in m['categories']:
oc = {
"id": c['id'],
"mid": m['id'],
"label": c['label'],
"inode": False,
"open": False,
"preferences": sorted(c['preferences'], key=label)
}
(om['branch']).append(oc)
om['branch'] = sorted(om['branch'], key=label)
res.append(om)
return ajax_response(
response=sorted(res, key=label),
status=200
)
@blueprint.route("/preferences/<int:pid>", methods=["PUT"])
@login_required
def save(pid):
"""
Save a specific preference.
"""
data = request.form if request.form else json.loads(request.data.decode())
res, msg = Preferences.save(data['mid'], data['cid'], data['id'], data['value'])
if not res:
return internal_server_error(errormsg=msg)
return success_return()