From d492da3ca100ef082f7e2376c33e3b28a422194e Mon Sep 17 00:00:00 2001 From: Dave Page Date: Tue, 27 Jan 2015 14:18:27 +0000 Subject: [PATCH] Add automatic browser menu integration for modules. Modules may now include functions that return lists of menu items that will be included on the main browser window menu. While we're at it, move the test views into a separate module. --- web/pgadmin/__init__.py | 18 ++++++-- web/pgadmin/about/__init__.py | 0 web/pgadmin/about/browser.py | 18 ++++++++ web/pgadmin/about/templates/about/index.html | 1 + web/pgadmin/about/views.py | 27 ++++++++++++ .../browser/templates/browser/index.html | 28 +++++++++++-- web/pgadmin/browser/views.py | 42 ++++++++++++++++++- web/pgadmin/test/__init__.py | 0 web/pgadmin/test/browser.py | 16 +++++++ web/pgadmin/test/views.py | 35 ++++++++++++++++ web/pgadmin/utils/views.py | 16 ------- 11 files changed, 176 insertions(+), 25 deletions(-) create mode 100644 web/pgadmin/about/__init__.py create mode 100644 web/pgadmin/about/browser.py create mode 100644 web/pgadmin/about/templates/about/index.html create mode 100644 web/pgadmin/about/views.py create mode 100644 web/pgadmin/test/__init__.py create mode 100644 web/pgadmin/test/browser.py create mode 100644 web/pgadmin/test/views.py diff --git a/web/pgadmin/__init__.py b/web/pgadmin/__init__.py index 7b67a7644..74c355784 100644 --- a/web/pgadmin/__init__.py +++ b/web/pgadmin/__init__.py @@ -17,11 +17,14 @@ from flask_security.utils import login_user from flask_mail import Mail from settings_model import db, Role, User -import inspect, logging, os +import inspect, imp, logging, os # Configuration settings import config +# Global module list +modules = [ ] + def create_app(app_name=config.APP_NAME): """Create the Flask application, startup logging and dynamically load additional modules (blueprints) that are found in this directory.""" @@ -88,6 +91,7 @@ def create_app(app_name=config.APP_NAME): path = os.path.dirname(os.path.realpath(__file__)) files = os.listdir(path) + for f in files: d = os.path.join(path, f) if os.path.isdir(d) and os.path.isfile(os.path.join(d, '__init__.py')): @@ -98,10 +102,16 @@ def create_app(app_name=config.APP_NAME): # Looks like a module, so import it, and register the blueprint if present # We rely on the ordering of syspath to ensure we actually get the right - # module here. + # module here. Note that we also try to load the 'browser' module for + # the browser integration hooks. app.logger.info('Examining potential module: %s' % d) - module = __import__(f, globals(), locals(), ['views'], -1) - if hasattr(module.views, 'blueprint'): + module = __import__(f, globals(), locals(), ['browser', 'views'], -1) + + # Add the module to the global module list + modules.append(module) + + # Register the blueprint if present + if 'views' in dir(module) and 'blueprint' in dir(module.views): app.logger.info('Registering blueprint module: %s' % f) app.register_blueprint(module.views.blueprint) diff --git a/web/pgadmin/about/__init__.py b/web/pgadmin/about/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/web/pgadmin/about/browser.py b/web/pgadmin/about/browser.py new file mode 100644 index 000000000..5f26ea2d9 --- /dev/null +++ b/web/pgadmin/about/browser.py @@ -0,0 +1,18 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2014, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +"""Browser integration functions for the About module.""" + +from flask import url_for + +import config + +def get_help_menu_items(): + """Return a (set) of dicts of help menu items, with name, priority and URL.""" + return [{'name': 'About %s' % (config.APP_NAME), 'priority': 999, 'url': url_for('about.index')}] \ No newline at end of file diff --git a/web/pgadmin/about/templates/about/index.html b/web/pgadmin/about/templates/about/index.html new file mode 100644 index 000000000..f23c4e2ad --- /dev/null +++ b/web/pgadmin/about/templates/about/index.html @@ -0,0 +1 @@ +About {{ config.APP_NAME }} diff --git a/web/pgadmin/about/views.py b/web/pgadmin/about/views.py new file mode 100644 index 000000000..1e57672c7 --- /dev/null +++ b/web/pgadmin/about/views.py @@ -0,0 +1,27 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2014, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +"""A blueprint module implementing the about box.""" +MODULE_NAME = 'about' + +import config +from flask import Blueprint, current_app, render_template +from flask.ext.security import login_required + +# Initialise the module +blueprint = Blueprint(MODULE_NAME, __name__, static_folder='static', static_url_path='', template_folder='templates', url_prefix='/' + MODULE_NAME) + +########################################################################## +# A test page +########################################################################## +@blueprint.route("/") +@login_required +def index(): + """Render the about box.""" + return render_template(MODULE_NAME + '/index.html') diff --git a/web/pgadmin/browser/templates/browser/index.html b/web/pgadmin/browser/templates/browser/index.html index 7df1d3c57..43a12dc07 100644 --- a/web/pgadmin/browser/templates/browser/index.html +++ b/web/pgadmin/browser/templates/browser/index.html @@ -16,18 +16,40 @@