From b0b964dd55b33b476d035cd9f386e25ec43614a5 Mon Sep 17 00:00:00 2001 From: Ashesh Vashi Date: Thu, 30 Jul 2015 16:17:13 +0530 Subject: [PATCH] Adding the module.js route for each in the NodeView class. --- web/pgadmin/browser/server_groups/__init__.py | 18 +++-- .../browser/server_groups/servers/__init__.py | 19 +++-- .../browser/templates/browser/js/browser.js | 1 + web/pgadmin/browser/utils.py | 75 ++++++++++++++----- 4 files changed, 77 insertions(+), 36 deletions(-) diff --git a/web/pgadmin/browser/server_groups/__init__.py b/web/pgadmin/browser/server_groups/__init__.py index 2939c9255..328ab3499 100644 --- a/web/pgadmin/browser/server_groups/__init__.py +++ b/web/pgadmin/browser/server_groups/__init__.py @@ -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) diff --git a/web/pgadmin/browser/server_groups/servers/__init__.py b/web/pgadmin/browser/server_groups/servers/__init__.py index a2a360ae1..416ed2a1c 100644 --- a/web/pgadmin/browser/server_groups/servers/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/__init__.py @@ -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) diff --git a/web/pgadmin/browser/templates/browser/js/browser.js b/web/pgadmin/browser/templates/browser/js/browser.js index e77e2c156..650d79cb7 100644 --- a/web/pgadmin/browser/templates/browser/js/browser.js +++ b/web/pgadmin/browser/templates/browser/js/browser.js @@ -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). diff --git a/web/pgadmin/browser/utils.py b/web/pgadmin/browser/utils.py index b49fb2039..1d50e33de 100644 --- a/web/pgadmin/browser/utils.py +++ b/web/pgadmin/browser/utils.py @@ -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'} + )