Adding the module.js route for each in the NodeView class.

This commit is contained in:
Ashesh Vashi 2015-07-30 16:17:13 +05:30
parent 7d9224ba8b
commit b0b964dd55
4 changed files with 77 additions and 36 deletions

View File

@ -78,14 +78,6 @@ class ServerGroupPluginModule(BrowserPluginModule):
blueprint = ServerGroupModule( __name__, static_url_path='')
@blueprint.route('/module.js')
@login_required
def module():
return make_response(
render_template("server_groups/server_groups.js"),
200, {'Content-Type': 'application/x-javascript'})
# Initialise the module
from pgadmin.browser.utils import NodeView
@ -234,5 +226,15 @@ class ServerGroupView(NodeView):
def dependents(self, gid):
return make_json_response(status=422)
def module_js(self, **kwargs):
"""
This property defines (if javascript) exists for this node.
Override this property for your own logic.
"""
return make_response(
render_template("server_groups/server_groups.js"),
200, {'Content-Type': 'application/x-javascript'}
)
ServerGroupView.register_node_view(blueprint)

View File

@ -66,14 +66,6 @@ class ServerMenuItem(MenuItem):
blueprint = ServerModule(__name__)
@blueprint.route('/module.js')
@login_required
def module():
return make_response(
render_template("servers/servers.js"),
200, {'Content-Type': 'application/x-javascript'})
class ServerNode(NodeView):
node_type = ServerModule.NODE_TYPE
parent_ids = [{'type': 'int', 'id': 'gid'}]
@ -87,6 +79,7 @@ class ServerNode(NodeView):
'sql': [{'get': 'sql', 'post': 'modified_sql'}],
'stats': [{'get': 'statistics'}],
'deps': [{'get': 'dependencies', 'post': 'dependents'}],
'module.js': [{}, {}, {'get': 'module_js'}],
'connect': [{'get': 'connect_status', 'post': 'connect', 'delete': 'disconnect'}]
})
@ -300,5 +293,15 @@ class ServerNode(NodeView):
def dependents(self, gid, sid):
return make_json_response(data='')
def module_js(self, **kwargs):
"""
This property defines (if javascript) exists for this node.
Override this property for your own logic.
"""
return make_response(
render_template("servers/servers.js"),
200, {'Content-Type': 'application/x-javascript'}
)
ServerNode.register_node_view(blueprint)

View File

@ -519,6 +519,7 @@ OWNER TO helpdesk;\n';
},
// load the module right now
load_module: function(name, path, c) {
var obj = this;
require([name],function(m) {
try {
// initialze the module (if 'init' function present).

View File

@ -80,7 +80,8 @@ class NodeView(with_metaclass(MethodViewType, View)):
'nodes': [{'get': 'nodes'}],
'sql': [{'get': 'sql', 'post': 'modified_sql'}],
'stats': [{'get': 'statistics'}],
'deps': [{'get': 'dependencies', 'post': 'dependents'}]
'deps': [{'get': 'dependencies', 'post': 'dependents'}],
'module.js': [{}, {}, {'get': 'module_js'}]
})
@classmethod
@ -93,7 +94,7 @@ class NodeView(with_metaclass(MethodViewType, View)):
for meth in ops:
meths.append(meth.upper())
if len(meths) > 0:
cmds.append({'cmd': op, 'req': idx == 0, 'methods': meths})
cmds.append({'cmd': op, 'req': idx == 0, 'with_id': idx != 2, 'methods': meths})
idx += 1
return cmds
@ -146,22 +147,29 @@ class NodeView(with_metaclass(MethodViewType, View)):
meth = 'get'
assert self.cmd in self.operations, \
"Unimplemented Command ({0}) for {1}".format(
self.cmd,
str(self.__class__.__name__))
"Unimplemented Command ({0}) for {1}".format(
self.cmd,
str(self.__class__.__name__)
)
has_args, has_id = self.check_args(**kwargs)
assert ((has_id and len(self.operations[self.cmd]) >= 0 and meth in
self.operations[self.cmd][0]) or (
not has_id and len(
self.operations[self.cmd]) > 0 and meth in
self.operations[self.cmd][1])), \
"Unimplemented method ({0}) for command ({1}), which {2} an id".format(
meth, self.cmd,
'requires' if has_id else 'does not require')
assert self.cmd in self.operations and \
(has_id and len(self.operations[self.cmd]) > 0 and \
meth in self.operations[self.cmd][0]) or \
(not has_id and len(self.operations[self.cmd]) > 1 and \
meth in self.operations[self.cmd][1]) or \
(len(self.operations[self.cmd]) > 2 and \
meth in self.operations[self.cmd][2]), \
"Unimplemented method ({0}) for command ({1}), which {2} an id".format(
meth, self.cmd,
'requires' if has_id else 'does not require'
)
meth = self.operations[self.cmd][0][meth] if has_id else \
self.operations[self.cmd][1][meth]
self.operations[self.cmd][1][meth] if has_args and \
meth in self.operations[self.cmd][1] else \
self.operations[self.cmd][2][meth]
method = getattr(self, meth, None)
@ -184,9 +192,36 @@ class NodeView(with_metaclass(MethodViewType, View)):
commands = cls.generate_ops()
for c in commands:
blueprint.add_url_rule(
'/{0}{1}'.format(c['cmd'], id_url if c['req'] else url),
view_func=cls.as_view(
'%s%s' % (c['cmd'], '_id' if c['req'] else ''),
cmd=c['cmd']),
methods=c['methods'])
if c['with_id']:
blueprint.add_url_rule(
'/{0}{1}'.format(
c['cmd'], id_url if c['req'] else url
),
view_func=cls.as_view(
'{0}{1}'.format(
c['cmd'], '_id' if c['req'] else ''
),
cmd=c['cmd']
),
methods=c['methods']
)
else:
blueprint.add_url_rule(
'/{0}'.format(c['cmd']),
view_func=cls.as_view(
'{0}'.format(c['cmd']), cmd=c['cmd']
),
methods=c['methods']
)
def module_js(self, **kwargs):
"""
This property defines (if javascript) exists for this node.
Override this property for your own logic.
"""
return flask.make_response(
flask.render_template(
"{0}/{1}.js".format(self.node_type)
),
200, {'Content-Type': 'application/x-javascript'}
)