Inherited the test, about & help module from PgAdminModule class to load

them automatically by PgAdmin.
This commit is contained in:
Ronan Dunklau
2015-06-29 13:24:05 +05:30
committed by Ashesh Vashi
parent eb6580b43a
commit b626eec0fd
15 changed files with 280 additions and 267 deletions

View File

@@ -9,7 +9,7 @@
"""The main pgAdmin module. This handles the application initialisation tasks,
such as setup of logging, dynamic loading of modules etc."""
from collections import defaultdict
from flask import Flask, abort, request, current_app
from flask.ext.babel import Babel
from flask.ext.security import Security, SQLAlchemyUserDatastore
@@ -42,6 +42,32 @@ class PgAdmin(Flask):
if isinstance(value, PgAdminModule):
yield value
@property
def submodules(self):
for blueprint in self.blueprints.values():
if isinstance(blueprint, PgAdminModule):
yield blueprint
@property
def stylesheets(self):
stylesheets = []
for module in self.submodules:
stylesheets.extend(getattr(module, "stylesheets", []))
return stylesheets
@property
def javascripts(self):
stylesheets = []
for module in self.submodules:
stylesheets.extend(getattr(module, "javascripts", []))
return stylesheets
@property
def panels(self):
panels = []
for module in self.submodules:
panels.extend(module.get_panels())
return panels
def _find_blueprint():
if request.blueprint:
@@ -169,9 +195,13 @@ def create_app(app_name=config.APP_NAME):
@app.context_processor
def inject_blueprint():
"""Inject a reference to the current blueprint, if any."""
menu_items = defaultdict(list)
for blueprint in app.submodules:
menu_items.update(getattr(blueprint, "menu_items", {}))
return {
'current_app': current_app,
'current_blueprint': current_blueprint,
'menu_items': getattr(current_blueprint, "menu_items", {})}
'menu_items': menu_items }
##########################################################################
# All done!

View File

@@ -0,0 +1,67 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""A blueprint module implementing the about box."""
MODULE_NAME = 'about'
from pgadmin.utils import PgAdminModule
from pgadmin.utils.menu import MenuItem
from flask import Response, current_app, render_template, __version__, url_for
from flask.ext.babel import gettext
from flask.ext.security import current_user, login_required
import sys
import config
class AboutModule(PgAdminModule):
def get_own_menuitems(self):
return {
'help_items': [
MenuItem(name='mnu_about',
priority=999,
url='#',
onclick='about_show()',
label=gettext('About %(appname)s', appname=config.APP_NAME))
]
}
def get_own_javascripts(self):
return [url_for('about.script')]
blueprint = AboutModule(MODULE_NAME, __name__,
static_url_path='')
##########################################################################
# A test page
##########################################################################
@blueprint.route("/")
@login_required
def index():
"""Render the about box."""
info = { }
info['python_version'] = sys.version
info['flask_version'] = __version__
if config.SERVER_MODE == True:
info['app_mode'] = gettext('Server')
else:
info['app_mode'] = gettext('Desktop')
info['current_user'] = current_user.email
return render_template(MODULE_NAME + '/index.html', info=info)
@blueprint.route("/about.js")
@login_required
def script():
"""Render the required Javascript"""
return Response(response=render_template("about/about.js"),
status=200,
mimetype="application/javascript")

View File

@@ -1,28 +0,0 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""Browser integration functions for the About module."""
from flask import render_template, url_for
from flask.ext.babel import gettext
import config
def get_help_menu_items():
"""Return a (set) of dicts of help menu items, with name, priority, URL,
target and onclick code."""
return [{'name': 'mnu_about',
'label': gettext('About %(appname)s', appname=config.APP_NAME),
'priority': 999,
'url': "#",
'onclick': "about_show()"}]
def get_scripts():
"""Return a list of script URLs to include in the rendered page header"""
return [ url_for('about.script') ]

View File

@@ -1,48 +0,0 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""A blueprint module implementing the about box."""
MODULE_NAME = 'about'
from flask import Blueprint, Response, current_app, render_template, __version__
from flask.ext.babel import gettext
from flask.ext.security import current_user, login_required
import sys
import config
# 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."""
info = { }
info['python_version'] = sys.version
info['flask_version'] = __version__
if config.SERVER_MODE == True:
info['app_mode'] = gettext('Server')
else:
info['app_mode'] = gettext('Desktop')
info['current_user'] = current_user.email
return render_template(MODULE_NAME + '/index.html', info=info)
@blueprint.route("/about.js")
@login_required
def script():
"""Render the required Javascript"""
return Response(response=render_template("about/about.js"),
status=200,
mimetype="application/javascript")

View File

@@ -31,39 +31,22 @@
<li><a id="mnu_drop_object" href="#" onclick="drop_object()">{{ _('Drop object') }}</a></li>
<li><a id="mnu_rename_object" href="#" onclick="rename_object()">{{ _('Rename object') }}</a></li>
<li class="divider"></li>
{% if file_items is defined and file_items|count > 0 %}{% for file_item in file_items %}
<li><a id="{{ file_item.name }}" href="{{ file_item.url }}"{% if file_item.target %} target="{{ file_item.target }}"{% endif %}{% if file_item.onclick %} onclick="{{ file_item.onclick|safe }}"{% endif %}>{{ file_item.label }}</a></li>{% endfor %}{% endif %}
{% for file_item in menu_items.file_items %}
<li><a id="{{ file_item.name }}" href="{{ file_item.url }}"{% if file_item.target %} target="{{ file_item.target }}"{% endif %}{% if file_item.onclick %} onclick="{{ file_item.onclick|safe }}"{% endif %}>{{ file_item.label }}</a></li>{% endfor %}
</ul>
</li>
{% if edit_items is defined and edit_items|count > 0 %}<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{{ _('Edit') }} <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">{% for edit_item in edit_items %}
<li><a id="{{ edit_item.name }}" href="{{ edit_item.url }}"{% if edit_item.target %} target="{{ edit_item.target }}"{% endif %}{% if edit_item.onclick %} onclick="{{ edit_item.onclick|safe }}"{% endif %}>{{ edit_item.label }}</a></li>{% endfor %}
</ul>
</li>{% endif %}
{% if tools_items is defined and tools_items|count > 0 %}<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{{ _('Tools') }} <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">{% for tools_item in tools_items %}
<li><a id="{{ tools_item.name }}" href="{{ tools_item.url }}"{% if tools_item.target %} target="{{ tools_item.target }}"{% endif %}{% if tools_item.onclick %} onclick="{{ tools_item.onclick|safe }}"{% endif %}>{{ tools_item.label }}</a></li>{% endfor %}
</ul>
</li>{% endif %}
{% if management_items is defined and management_items|count > 0 %}<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{{ _('Management') }} <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">{% for management_item in management_items %}
<li><a id="{{ management_item.name }}" href="{{ management_item.url }}"{% if management_item.target %} target="{{ management_item.target }}"{% endif %}{% if management_item.onclick %} onclick="{{ management_item.onclick|safe }}"{% endif %}>{{ management_item.label }}</a></li>{% endfor %}
</ul>
</li>{% endif %}
{% if help_items is defined and help_items|count > 0 %}<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{{ _('Help') }} <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">{% for help_item in help_items %}
<li><a id="{{ help_item.name }}" href="{{ help_item.url }}"{% if help_item.target %} target="{{ help_item.target }}"{% endif %}{% if help_item.onclick %} onclick="{{ help_item.onclick|safe }}"{% endif %}>{{ help_item.label }}</a></li>{% endfor %}
</ul>
</li>{% endif %}
{% for key in ('Edit', 'Tools', 'Management', 'Help') %}
{% if menu_items['%s_items' % key.lower()] %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"
role="button" aria-expanded="false">{{ _(key) }} <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
{% for item in menu_items['%s_items' % key.lower()] %}
<li><a id="{{ item.name }}" href="{{ item.url }}"{% if item.target %} target="{{ item.target }}"{% endif %}{% if item.onclick %} onclick="{{ item.onclick|safe }}"{% endif %}>{{ item.label }}</a></li>
{% endfor %}
</ul>
</li>{%endif%}
{% endfor %}
</ul>
{% if config.SERVER_MODE %}
<ul class="nav navbar-nav navbar-right">

View File

@@ -254,7 +254,7 @@ ALTER TABLE tickets_detail \n\
'<p>Dependents pane</p>')
// Add hooked-in panels
{% for panel_item in menu_items.panel_items %}{% if panel_item.isIframe %}
{% for panel_item in current_app.panels %}{% if panel_item.isIframe %}
buildIFramePanel(docker, '{{ panel_item.name }}', '{{ panel_item.title }}',
{{ panel_item.width }}, {{ panel_item.height }},
{{ panel_item.showTitle|lower }}, {{ panel_item.isCloseable|lower }},

View File

@@ -0,0 +1,66 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""A blueprint module implementing the pgAdmin help system."""
MODULE_NAME = 'help'
from pgadmin.utils import PgAdminModule
from pgadmin.utils.menu import MenuItem, Panel
from flask.ext.babel import gettext
from flask import url_for
import config
class HelpModule(PgAdminModule):
def get_own_menuitems(self):
"""Return a (set) of dicts of help menu items, with name, priority, URL,
target and onclick code."""
return { 'help_items': [
MenuItem(name='mnu_online_help',
label=gettext('Online Help'),
priority=100,
target='_new',
url=url_for('help.static', filename='index.html')),
MenuItem(name='mnu_pgadmin_website',
label= gettext('pgAdmin Website'),
priority= 200,
target= '_new',
url= 'http://www.pgadmin.org/' ),
MenuItem(name= 'mnu_postgresql_website',
label= gettext('PostgreSQL Website'),
priority= 300,
target= '_new',
url= 'http://www.postgresql.org/' )]}
def get_panels(self):
return [
Panel(
name='pnl_online_help',
priority=100,
title=gettext('Online Help'),
content=url_for('help.static', filename='index.html')),
Panel(name='pnl_pgadmin_website',
priority=200,
title=gettext('pgAdmin Website'),
content='http://www.pgadmin.org/'),
Panel(name='pnl_postgresql_website',
priority=300,
title=gettext('PostgreSQL Website'),
content='http://www.postgresql.org/')]
# Initialise the module
blueprint = HelpModule(MODULE_NAME, __name__, static_url_path='/help',
static_folder=config.HELP_PATH)

View File

@@ -1,73 +0,0 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""Browser integration functions for the Help module."""
from flask import url_for
from flask.ext.babel import gettext
import config
def get_help_menu_items():
"""Return a (set) of dicts of help menu items, with name, priority, URL,
target and onclick code."""
return [{'name': 'mnu_online_help',
'label': gettext('Online Help'),
'priority': 100,
'target': '_new',
'url': url_for('help.static', filename='index.html') },
{'name': 'mnu_pgadmin_website',
'label': gettext('pgAdmin Website'),
'priority': 200,
'target': '_new',
'url': 'http://www.pgadmin.org/' },
{'name': 'mnu_postgresql_website',
'label': gettext('PostgreSQL Website'),
'priority': 300,
'target': '_new',
'url': 'http://www.postgresql.org/' }]
def get_panels():
"""Return a (set) of dicts describing panels to create in the browser. Fields
are name, priority, title, width, height, isIframe, showTitle, isCloseable,
isPrivate and content"""
return [{'name': 'pnl_online_help',
'priority': 100,
'title': gettext('Online Help'),
'width': 500,
'height': 600,
'isIframe': True,
'showTitle': True,
'isCloseable': True,
'isPrivate': False,
'content': url_for('help.static', filename='index.html') },
{'name': 'pnl_pgadmin_website',
'priority': 200,
'title': gettext('pgAdmin Website'),
'width': 500,
'height': 600,
'isIframe': True,
'showTitle': True,
'isCloseable': True,
'isPrivate': False,
'content': 'http://www.pgadmin.org/' },
{'name': 'pnl_postgresql_website',
'priority': 300,
'title': gettext('PostgreSQL Website'),
'width': 500,
'height': 600,
'isIframe': True,
'showTitle': True,
'isCloseable': True,
'isPrivate': False,
'content': 'http://www.postgresql.org/' }]

View File

@@ -1,18 +0,0 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""A blueprint module implementing the pgAdmin help system."""
MODULE_NAME = 'help'
from flask import Blueprint
import config
# Initialise the module
blueprint = Blueprint(MODULE_NAME, __name__, static_url_path='/help', static_folder=config.HELP_PATH)

View File

@@ -21,7 +21,7 @@
{% if config.DEBUG %}<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap-theme.min.css') }}">{% else %}<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap-theme.min.css') }}">{% endif %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/overrides.css') }}">
<!-- View specified stylesheets -->
{% for stylesheet in current_blueprint.stylesheets %}
{% for stylesheet in current_app.stylesheets %}
<link rel="stylesheet" href="{{ stylesheet }}">
{% endfor %}
<!-- Base template scripts -->
@@ -32,7 +32,7 @@
<script src="{{ url_for('static', filename='js/alertifyjs/pgadmin.defaults.js') }}"></script>
<!-- View specified scripts -->
{% for script in current_blueprint.javascripts %}
{% for script in current_app.javascripts %}
<script src="{{ script }}"></script>
{% endfor %}
</head>

View File

@@ -0,0 +1,70 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""Browser integration functions for the Test module."""
MODULE_NAME = 'test'
from flask.ext.security import login_required
from flask import render_template, url_for
from flask.ext.babel import gettext
from pgadmin.utils import PgAdminModule
from pgadmin.utils.menu import MenuItem
from time import time, ctime
class TestModule(PgAdminModule):
def get_own_menuitems(self):
return {'file_items': [
MenuItem(name='mnu_generate_test_html',
label=gettext('Generated Test HTML'),
priority=100,
url=url_for('test.generated')),
MenuItem(name='mnu_test_alert',
label=gettext('Test Alert'),
priority=200,
url='#',
onclick='test_alert()'),
MenuItem(name='mnu_test_confirm',
label=gettext('Test Confirm'),
priority=300,
url='#',
onclick='test_confirm()'),
MenuItem(name='mnu_test_dialog',
label=gettext('Test Dialog'),
priority=400,
url='#',
onclick='test_dialog()'),
MenuItem(name='mnu_test_prompt',
label=gettext('Test Prompt'),
priority=500,
url='#',
onclick='test_prompt()'),
MenuItem(name='mnu_test_notifier',
label=gettext('Test Notifier'),
priority=600,
url='#',
onclick='test_notifier()')
]}
def get_own_javascripts(self):
return [ url_for('test.static', filename='js/test.js') ]
# Initialise the module
blueprint = TestModule(MODULE_NAME, __name__)
@blueprint.route("/generated")
@login_required
def generated():
"""Generate a simple test page to demonstrate that output can be rendered."""
output = """
Today is <b>%s</b>
<br />
<i>This is Flask-generated HTML.</i>
<br /><br />
<a href="http://www.pgadmin.org/">%s v%s</a>""" % (ctime(time()), config.APP_NAME, config.APP_VERSION)
return output

View File

@@ -1,29 +0,0 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""Browser integration functions for the Test module."""
from flask import render_template, url_for
from flask.ext.babel import gettext
def get_file_menu_items():
"""Return a (set) of dicts of file menu items, with name, priority, URL,
target and onclick code."""
return [
{'name': 'mnu_generate_test_html', 'label': gettext('Generated Test HTML'), 'priority': 100, 'url': url_for('test.generated')},
{'name': 'mnu_test_alert', 'label': gettext('Test Alert'), 'priority': 200, 'url': '#', 'onclick': 'test_alert()'},
{'name': 'mnu_test_confirm', 'label': gettext('Test Confirm'), 'priority': 300, 'url': '#', 'onclick': 'test_confirm()'},
{'name': 'mnu_test_dialog', 'label': gettext('Test Dialog'), 'priority': 400, 'url': '#', 'onclick': 'test_dialog()'},
{'name': 'mnu_test_prompt', 'label': gettext('Test Prompt'), 'priority': 500, 'url': '#', 'onclick': 'test_prompt()'},
{'name': 'mnu_test_notifier', 'label': gettext('Test Notifier'), 'priority': 600, 'url': '#', 'onclick': 'test_notifier()'},
]
def get_scripts():
"""Return a list of script URLs to include in the rendered page header"""
return [ url_for('test.static', filename='js/test.js') ]

View File

@@ -1,35 +0,0 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""A blueprint module providing utility functions for the application."""
MODULE_NAME = 'test'
import config
from flask import Blueprint, render_template
from flask.ext.security import login_required
from time import time, ctime
# Initialise the module
blueprint = Blueprint(MODULE_NAME, __name__, static_folder='static', template_folder='templates', url_prefix='/' + MODULE_NAME)
##########################################################################
# A test page
##########################################################################
@blueprint.route("/generated")
@login_required
def generated():
"""Generate a simple test page to demonstrate that output can be rendered."""
output = """
Today is <b>%s</b>
<br />
<i>This is Flask-generated HTML.</i>
<br /><br />
<a href="http://www.pgadmin.org/">%s v%s</a>""" % (ctime(time()), config.APP_NAME, config.APP_VERSION)
return output

View File

@@ -55,6 +55,13 @@ class PgAdminModule(Blueprint):
"""
return defaultdict(list)
def get_panels(self):
"""
Returns:
list: a list of panel objects to add
"""
return []
@property
def stylesheets(self):
stylesheets = self.get_own_stylesheets()

View File

@@ -1,6 +1,27 @@
from collections import namedtuple
PRIORITY = 100
class MenuItem(object):
def __init__(self, **kwargs):
self.__dict__.update(**kwargs)
class Panel(object):
def __init__(self, name, title, content, width=500, height=600, isIframe=True,
showTitle=True, isCloseable=True, isPrivate=False, priority=None):
self.name = name
self.title = title
self.content = content
self.width = width
self.height = height
self.isIfframe = isIframe
self.showTitle = showTitle
self.isCloseable = isCloseable
self.isPrivate = isPrivate
if priority is None:
global PRIORITY
PRIORITY += 100
priority = PRIORITY
self.priority = priority