diff --git a/web/pgadmin/about/static/js/about.js b/web/pgadmin/about/static/js/about.js new file mode 100644 index 00000000..a746d0f5 --- /dev/null +++ b/web/pgadmin/about/static/js/about.js @@ -0,0 +1,54 @@ +define( + ['jquery', 'alertify', 'pgadmin', 'underscore.string', 'sources/gettext', + 'sources/url_for' + ], + function($, alertify, pgAdmin, S, gettext, url_for) { + pgAdmin = pgAdmin || window.pgAdmin || {}; + + /* Return back, this has been called more than once */ + if (pgAdmin.About) + return; + + pgAdmin.About = { + about_show: function() { + if (!alertify.aboutDialog) { + alertify.dialog('aboutDialog', function factory() { + return { + main: function(title, message) { + this.set('title', title); + this.message = message; + }, + setup: function() { + return { + buttons:[{ text: gettext("OK"), key: 27, className: "btn btn-primary" }], + options: { + modal: false, + resizable: true, + maximizable: true, + pinnable: false, + closableByDimmer: false + } + }; + }, + build: function() { + alertify.pgDialogBuild.apply(this); + }, + prepare:function() { + this.setContent(this.message); + } + }; + }); + } + + var content = ''; + $.get(url_for('about.index'), + function(data) { + alertify.aboutDialog( + S(gettext("About %s")).sprintf(pgAdmin.Browser.utils.app_name).value(), data + ).resizeTo(800, 450); + }); + } + }; + + return pgAdmin.About; + }); diff --git a/web/pgadmin/browser/server_groups/__init__.py b/web/pgadmin/browser/server_groups/__init__.py index ab0a3ee2..fd2b4ac9 100644 --- a/web/pgadmin/browser/server_groups/__init__.py +++ b/web/pgadmin/browser/server_groups/__init__.py @@ -26,7 +26,7 @@ from pgadmin.model import db, ServerGroup class ServerGroupModule(BrowserPluginModule): - NODE_TYPE = "server-group" + NODE_TYPE = "server_group" def get_nodes(self, *arg, **kwargs): """Return a JSON document listing the server groups for the user""" diff --git a/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/__init__.py index 7b2da993..c2512c43 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/__init__.py @@ -86,6 +86,14 @@ class ForeignServerModule(CollectionNodeModule): """ return databases.DatabaseModule.NODE_TYPE + @property + def module_use_template_javascript(self): + """ + Returns whether Jinja2 template is used for generating the javascript + module. + """ + return False + blueprint = ForeignServerModule(__name__) diff --git a/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/static/js/foreign_server.js b/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/static/js/foreign_server.js new file mode 100644 index 00000000..52cf4e90 --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/static/js/foreign_server.js @@ -0,0 +1,184 @@ +define('pgadmin.node.foreign_server', [ + 'sources/gettext', 'jquery', 'underscore', 'underscore.string', 'pgadmin', + 'pgadmin.browser', 'alertify', 'pgadmin.browser.collection', + 'pgadmin.browser.server.privilege' +], function(gettext, $, _, S, pgAdmin, pgBrowser, alertify) { + + // Extend the browser's node model class to create a Options model + var OptionsModel = pgAdmin.Browser.Node.Model.extend({ + idAttribute: 'fsrvoption', + defaults: { + fsrvoption: undefined, + fsrvvalue: undefined + }, + + // Defining schema for the Options model + schema: [ + {id: 'fsrvoption', label:'Options', type:'text', cellHeaderClasses:'width_percent_50', group: null, editable: true}, + {id: 'fsrvvalue', label:'Value', type: 'text', cellHeaderClasses:'width_percent_50', group:null, editable: true}, + ], + + /* validate function is used to validate the input given by + * the user. In case of error, message will be displayed on + * the browser for the respective control. + */ + validate: function() { + // Validation for the option name + if (_.isUndefined(this.get('fsrvoption')) || + _.isNull(this.get('fsrvoption')) || + String(this.get('fsrvoption')).replace(/^\s+|\s+$/g, '') == '') { + var msg = 'Please enter an option name'; + this.errorModel.set('fsrvoption', msg); + return msg; + } else { + this.errorModel.unset('fsrvoption'); + } + return null; + } + }); + + // Extend the browser's collection class for foreign server collection + if (!pgBrowser.Nodes['coll-foreign_server']) { + var foreign_data_wrappers = pgAdmin.Browser.Nodes['coll-foreign_server'] = + pgAdmin.Browser.Collection.extend({ + node: 'foreign_server', + label: gettext('Foreign Servers'), + type: 'coll-foreign_server', + columns: ['name','fsrvowner','description'] + }); + }; + + // Extend the browser's node class for foreign server node + if (!pgBrowser.Nodes['foreign_server']) { + pgAdmin.Browser.Nodes['foreign_server'] = pgAdmin.Browser.Node.extend({ + parent_type: 'foreign_data_wrapper', + type: 'foreign_server', + sqlAlterHelp: 'sql-alterforeignserver.html', + sqlCreateHelp: 'sql-createforeignserver.html', + label: gettext('Foreign Server'), + hasSQL: true, + hasDepends: true, + canDrop: true, + canDropCascade: true, + Init: function() { + + // Avoid multiple registration of menus + if (this.initialized) + return; + + this.initialized = true; + + /* Create foreign server context menu at database, + * foreign server collections and foreign server node + */ + pgBrowser.add_menus([{ + name: 'create_foreign_server_on_coll', node: 'coll-foreign_server', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('Foreign Server...'), + icon: 'wcTabIcon icon-foreign_server', data: {action: 'create'} + },{ + name: 'create_foreign_server', node: 'foreign_server', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('Foreign Server...'), + icon: 'wcTabIcon icon-foreign_server', data: {action: 'create'} + },{ + name: 'create_foreign_server', node: 'foreign_data_wrapper', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('Foreign Server...'), + icon: 'wcTabIcon icon-foreign_server', data: {action: 'create'} + } + ]); + }, + + // Defining model for foreign server node + model: pgAdmin.Browser.Node.Model.extend({ + defaults: { + name: undefined, + fsrvtype: undefined, + fsrvversion: undefined, + fsrvvalue: undefined, + fsrvoptions: [], + fsrvowner: undefined, + description: undefined, + fsrvacl: [] + }, + + // Default values! + initialize: function(attrs, args) { + var isNew = (_.size(attrs) === 0); + + if (isNew) { + var userInfo = pgBrowser.serverInfo[args.node_info.server._id].user; + + this.set({'fsrvowner': userInfo.name}, {silent: true}); + } + pgAdmin.Browser.Node.Model.prototype.initialize.apply(this, arguments); + }, + + // Defining schema for the foreign server node + schema: [{ + id: 'name', label: gettext('Name'), cell: 'string', + type: 'text', disabled: function(m) { + if (this.mode == 'edit' && this.node_info.server.version < 90200) { + return true; + } + else + return false; + } + },{ + id: 'fsrvid', label: gettext('OID'), cell: 'string', + type: 'text', disabled: true, mode: ['properties'] + },{ + id: 'fsrvowner', label: gettext('Owner'), type: 'text', + control: Backform.NodeListByNameControl, node: 'role', + mode: ['edit', 'create', 'properties'], select2: { allowClear: false } + },{ + id: 'fsrvtype', label: gettext('Type'), cell: 'string', + group: gettext('Definition'), type: 'text', mode: ['edit','create','properties'], disabled: function(m) { + return !m.isNew(); + } + },{ + id: 'fsrvversion', label: gettext('Version'), cell: 'string', + group: gettext('Definition'), type: 'text' + },{ + id: 'description', label: gettext('Comment'), cell: 'string', + type: 'multiline' + },{ + id: 'fsrvoptions', label: 'Options', type: 'collection', group: gettext("Options"), + model: OptionsModel, control: 'unique-col-collection', mode: ['edit', 'create'], + canAdd: true, canDelete: true, uniqueCol : ['fsrvoption'], + columns: ['fsrvoption','fsrvvalue'] + }, pgBrowser.SecurityGroupUnderSchema, { + id: 'fsrvacl', label: 'Privileges', type: 'collection', group: 'security', + model: pgAdmin.Browser.Node.PrivilegeRoleModel.extend({privileges: ['U']}), control: 'unique-col-collection', + mode: ['edit', 'create'], canAdd: true, canDelete: true, uniqueCol : ['grantee'] + },{ + id: 'acl', label: gettext('Privileges'), type: 'text', + group: gettext('Security'), mode: ['properties'], disabled: true + } + ], + + /* validate function is used to validate the input given by + * the user. In case of error, message will be displayed on + * the browser for the respective control. + */ + validate: function() { + var name = this.get('name'); + + if (_.isUndefined(name) || _.isNull(name) || + String(name).replace(/^\s+|\s+$/g, '') == '') { + var msg = gettext('Name cannot be empty.'); + this.errorModel.set('name', msg); + return msg; + } else { + this.errorModel.unset('name'); + } + return null; + } + }) + }); + + } + + return pgBrowser.Nodes['coll-foreign_server']; +}); diff --git a/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/user_mapping/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/user_mapping/__init__.py index c6a131df..263a4ca7 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/user_mapping/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/user_mapping/__init__.py @@ -101,6 +101,14 @@ class UserMappingModule(CollectionNodeModule): return servers.ServerModule.NODE_TYPE + @property + def module_use_template_javascript(self): + """ + Returns whether Jinja2 template is used for generating the javascript + module. + """ + return False + blueprint = UserMappingModule(__name__) diff --git a/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/user_mapping/static/js/user_mapping.js b/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/user_mapping/static/js/user_mapping.js new file mode 100644 index 00000000..7895ce17 --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/foreign_data_wrappers/foreign_servers/user_mapping/static/js/user_mapping.js @@ -0,0 +1,165 @@ +define('pgadmin.node.user_mapping', [ + 'sources/gettext', 'sources/url_for', 'jquery', 'underscore', 'underscore.string', 'pgadmin', + 'pgadmin.browser', 'alertify', 'pgadmin.browser.collection' +], function(gettext, url_for, $, _, S, pgAdmin, pgBrowser, alertify) { + + // Extend the browser's node model class to create a Options model + var OptionsModel = pgAdmin.Browser.Node.Model.extend({ + idAttribute: 'umoption', + defaults: { + umoption: undefined, + umvalue: undefined + }, + + // Defining schema for the Options model + schema: [{ + id: 'umoption', label: gettext('Options'), type:'text', + cellHeaderClasses:'width_percent_50', group: null, editable: true + }, { + id: 'umvalue', label: gettext('Value'), type: 'text', + cellHeaderClasses:'width_percent_50', group:null, editable: true + }], + + /* validate function is used to validate the input given by + * the user. In case of error, message will be displayed on + * the browser for the respective control. + */ + validate: function() { + // Validation for the option value + if (_.isUndefined(this.get('umoption')) || + _.isNull(this.get('umoption')) || + String(this.get('umoption')).replace(/^\s+|\s+$/g, '') == '') { + var msg = 'Please enter an option name'; + this.errorModel.set('umoption', msg); + return msg; + } else { + this.errorModel.unset('umoption'); + } + return null; + } + }); + + // Extend the browser's collection class for user mapping collection + if (!pgBrowser.Nodes['coll-user_mapping']) { + var foreign_data_wrappers = pgAdmin.Browser.Nodes['coll-user_mapping'] = + pgAdmin.Browser.Collection.extend({ + node: 'user_mapping', + label: gettext('User Mappings'), + type: 'coll-user_mapping', + columns: ['name'] + }); + }; + + // Extend the browser's node class for user mapping node + if (!pgBrowser.Nodes['user_mapping']) { + pgAdmin.Browser.Nodes['user_mapping'] = pgAdmin.Browser.Node.extend({ + parent_type: 'foreign_server', + type: 'user_mapping', + sqlAlterHelp: 'sql-alterusermapping.html', + sqlCreateHelp: 'sql-createusermapping.html', + dialogHelp: url_for('help.static', {'filename': 'user_mapping_dialog.html'}), + label: gettext('User Mapping'), + hasSQL: true, + hasDepends: true, + canDrop: true, + canDropCascade: true, + Init: function() { + + // Avoid multiple registration of menus + if (this.initialized) + return; + + this.initialized = true; + + /* Create foreign server context menu at database, + * user mapping collections and user mapping node + */ + pgBrowser.add_menus([{ + name: 'create_user_mapping_on_coll', node: 'coll-user_mapping', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('User Mapping...'), + icon: 'wcTabIcon icon-user_mapping', data: {action: 'create'} + },{ + name: 'create_user_mapping', node: 'user_mapping', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('User Mapping...'), + icon: 'wcTabIcon icon-user_mapping', data: {action: 'create'} + },{ + name: 'create_user_mapping', node: 'foreign_server', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('User Mapping...'), + icon: 'wcTabIcon icon-user_mapping', data: {action: 'create'} + } + ]); + }, + + // Defining model for user mapping node + model: pgAdmin.Browser.Node.Model.extend({ + defaults: { + name: undefined, + um_options: [] + }, + + // Default values! + initialize: function(attrs, args) { + var isNew = (_.size(attrs) === 0); + + if (isNew) { + var userInfo = pgBrowser.serverInfo[args.node_info.server._id].user; + + this.set({'name': userInfo.name}, {silent: true}); + } + pgAdmin.Browser.Node.Model.prototype.initialize.apply(this, arguments); + }, + + // Defining schema for the user mapping node + schema: [{ + id: 'name', label: gettext('User'), type: 'text', + control: Backform.NodeListByNameControl, node: 'role', + mode: ['edit', 'create', 'properties'], select2: { allowClear: false }, + disabled: function(m) { return !m.isNew(); }, + transform: function(data) { + var self = this; + node = self.field.get('schema_node'); + var res = + Backform.NodeListByNameControl.prototype.defaults.transform.apply( + this, arguments + ); + res.unshift({label: 'CURRENT_USER', value: 'CURRENT_USER', image: 'icon-' + node.type}, + {label: 'PUBLIC', value: 'PUBLIC', image: 'icon-' + node.type}); + return res; + } + },{ + id: 'um_oid', label: gettext('OID'), cell: 'string', + type: 'text', disabled: true, mode: ['properties'], + },{ + id: 'umoptions', label: 'Options', type: 'collection', group: gettext("Options"), + model: OptionsModel, control: 'unique-col-collection', mode: ['create', 'edit'], + canAdd: true, canDelete: true, uniqueCol : ['umoption'] + } + ], + + /* validate function is used to validate the input given by + * the user. In case of error, message will be displayed on + * the browser for the respective control. + */ + validate: function() { + var name = this.get('name'); + + if (_.isUndefined(name) || _.isNull(name) || + String(name).replace(/^\s+|\s+$/g, '') == '') { + var msg = gettext('Name cannot be empty.'); + this.errorModel.set('name', msg); + return msg; + } else { + this.errorModel.unset('name'); + } + return null; + } + }) + }); + + } + + return pgBrowser.Nodes['coll-user_mapping']; +}); diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/catalog_objects/columns/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/catalog_objects/columns/__init__.py index ea2254d8..eebf05b4 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/catalog_objects/columns/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/catalog_objects/columns/__init__.py @@ -94,6 +94,14 @@ class CatalogObjectColumnsModule(CollectionNodeModule): 'show_system_objects' ) + @property + def module_use_template_javascript(self): + """ + Returns whether Jinja2 template is used for generating the javascript + module. + """ + return False + blueprint = CatalogObjectColumnsModule(__name__) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/catalog_objects/columns/static/js/catalog_object_column.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/catalog_objects/columns/static/js/catalog_object_column.js new file mode 100644 index 00000000..b5138576 --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/catalog_objects/columns/static/js/catalog_object_column.js @@ -0,0 +1,71 @@ +define('pgadmin.node.catalog_object_column', [ + 'sources/gettext', 'jquery', 'underscore', 'underscore.string', 'pgadmin', + 'pgadmin.browser', 'alertify', 'pgadmin.browser.collection' +], function(gettext, $, _, S, pgAdmin, pgBrowser, alertify) { + + if (!pgBrowser.Nodes['coll-catalog_object_column']) { + var databases = pgAdmin.Browser.Nodes['coll-catalog_object_column'] = + pgAdmin.Browser.Collection.extend({ + node: 'catalog_object_column', + label: gettext('catalog_object_column'), + type: 'coll-catalog_object_column', + columns: ['attname', 'attnum', 'cltype', 'description'] + }); + }; + + if (!pgBrowser.Nodes['catalog_object_column']) { + pgAdmin.Browser.Nodes['catalog_object_column'] = + pgAdmin.Browser.Node.extend({ + parent_type: 'catalog_object', + type: 'catalog_object_column', + label: gettext('catalog_object_column'), + hasSQL: false, + hasScriptTypes: [], + hasDepends: true, + Init: function() { + /* Avoid mulitple registration of menus */ + if (this.initialized) + return; + + this.initialized = true; + + }, + model: pgAdmin.Browser.Node.Model.extend({ + defaults: { + attname: undefined, + attowner: undefined, + atttypid: undefined, + attnum: undefined, + cltype: undefined, + collspcname: undefined, + attacl: undefined, + description: undefined + }, + schema: [{ + id: 'attname', label: gettext('Column'), cell: 'string', + type: 'text', disabled: true + },{ + id: 'attowner', label: gettext('Owner'), cell: 'string', + type: 'text', disabled: true + },{ + id: 'attnum', label: gettext('Position'), cell: 'string', + type: 'text', disabled: true + },{ + id: 'cltype', label: gettext('Data type'), cell: 'string', + group: gettext('Definition'), type: 'text', disabled: true + },{ + id: 'collspcname', label: gettext('Collation'), cell: 'string', + group: gettext('Definition'), type: 'text', disabled: true + },{ + id: 'attacl', label: gettext('Privileges'), cell: 'string', + group: gettext('Security'), type: 'text', disabled: true + },{ + id: 'description', label: gettext('Comment'), cell: 'string', + type: 'multiline', disabled: true + }] + }) + }); + } + + return pgBrowser.Nodes['catalog_object_column']; +}); diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py index 2ee6a430..ed383942 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/__init__.py @@ -56,7 +56,7 @@ class ForeignTableModule(SchemaChildModule): - Load the module script for Foreign Table, when schema node is initialized. """ - NODE_TYPE = 'foreign-table' + NODE_TYPE = 'foreign_table' COLLECTION_LABEL = gettext("Foreign Tables") def __init__(self, *args, **kwargs): @@ -396,7 +396,7 @@ class ForeignTableView(PGChildNodeView, DataTypeReader): row['oid'], scid, row['name'], - icon="icon-foreign-table" + icon="icon-foreign_table" )) return make_json_response( @@ -430,7 +430,7 @@ class ForeignTableView(PGChildNodeView, DataTypeReader): row['oid'], scid, row['name'], - icon="icon-foreign-table" + icon="icon-foreign_table" ), status=200 ) @@ -689,7 +689,7 @@ class ForeignTableView(PGChildNodeView, DataTypeReader): foid, scid, self.request['name'], - icon="icon-foreign-table" + icon="icon-foreign_table" ) ) except Exception as e: diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/img/coll-foreign_table.png b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/img/coll-foreign_table.png new file mode 100644 index 00000000..dc1f742b Binary files /dev/null and b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/img/coll-foreign_table.png differ diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/img/foreign_table.png b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/img/foreign_table.png new file mode 100644 index 00000000..53c13388 Binary files /dev/null and b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/img/foreign_table.png differ diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js new file mode 100644 index 00000000..32d5072c --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/static/js/foreign_table.js @@ -0,0 +1,719 @@ +/* Create and Register Foreign Table Collection and Node. */ +define('pgadmin.node.foreign_table', [ + 'sources/gettext', 'sources/url_for', 'jquery', 'underscore', + 'underscore.string', 'pgadmin', 'pgadmin.browser', 'alertify', + 'pgadmin.browser.collection' +], function(gettext, url_for, $, _, S, pgAdmin, pgBrowser, alertify) { + + if (!pgBrowser.Nodes['coll-foreign_table']) { + var foreigntable = pgBrowser.Nodes['coll-foreign_table'] = + pgBrowser.Collection.extend({ + node: 'foreign_table', + label: gettext('Foreign Tables'), + type: 'coll-foreign_table', + columns: ['name', 'owner', 'description'] + }); + }; + + // Integer Cell for Columns Length and Precision + var IntegerDepCell = Backgrid.IntegerCell.extend({ + initialize: function() { + Backgrid.NumberCell.prototype.initialize.apply(this, arguments); + Backgrid.Extension.DependentCell.prototype.initialize.apply(this, arguments); + }, + dependentChanged: function () { + this.$el.empty(); + var model = this.model; + var column = this.column; + var editable = this.column.get("editable"); + var is_editable = _.isFunction(editable) ? !!editable.apply(column, [model]) : !!editable; + + if (is_editable){ this.$el.addClass("editable"); } + else { this.$el.removeClass("editable"); } + + this.delegateEvents(); + return this; + }, + remove: Backgrid.Extension.DependentCell.prototype.remove + }); + + + // Options Model + var ColumnOptionsModel = pgBrowser.Node.Model.extend({ + idAttribute: 'option', + defaults: { + option: undefined, + value: undefined + }, + schema: [ + {id: 'option', label:'Option', type:'text', editable: true, cellHeaderClasses: 'width_percent_30'}, + { + id: 'value', label:'Value', type: 'text', editable: true, cellHeaderClasses: 'width_percent_50' + } + ], + validate: function() { + if (_.isUndefined(this.get('value')) || + _.isNull(this.get('value')) || + String(this.get('value')).replace(/^\s+|\s+$/g, '') == '') { + var msg = 'Please enter a value.'; + + this.errorModel.set('value', msg); + + return msg; + } else { + this.errorModel.unset('value'); + } + + return null; + } + }); + + // Columns Model + var ColumnsModel = pgBrowser.Node.Model.extend({ + idAttribute: 'attnum', + defaults: { + attname: undefined, + datatype: undefined, + typlen: undefined, + precision: undefined, + typdefault: undefined, + attnotnull: undefined, + collname: undefined, + attnum: undefined, + inheritedfrom: undefined, + inheritedid: undefined, + attstattarget: undefined, + coloptions: [] + }, + type_options: undefined, + schema: [{ + id: 'attname', label: gettext('Name'), cell: 'string', type: 'text', + editable: 'is_editable_column', cellHeaderClasses: 'width_percent_40' + },{ + id: 'datatype', label: gettext('Data Type'), cell: 'node-ajax-options', + control: 'node-ajax-options', type: 'text', url: 'get_types', + editable: 'is_editable_column', cellHeaderClasses: 'width_percent_0', + group: gettext('Definition'), + transform: function(d, self){ + self.model.type_options = d; + return d; + } + },{ + id: 'typlen', label: gettext('Length'), + cell: 'string', group: gettext('Definition'), + type: 'int', deps: ['datatype'], + disabled: function(m) { + var val = m.get('typlen'); + // We will store type from selected from combobox + if(!(_.isUndefined(m.get('inheritedid')) + || _.isNull(m.get('inheritedid')) + || _.isUndefined(m.get('inheritedfrom')) + || _.isNull(m.get('inheritedfrom')))) { + + if (!_.isUndefined(val)) { + setTimeout(function() { + m.set('typlen', undefined); + }, 10); + } + return true; + } + + var of_type = m.get('datatype'), + has_length = false; + if(m.type_options) { + m.set('is_tlength', false, {silent: true}); + + // iterating over all the types + _.each(m.type_options, function(o) { + // if type from selected from combobox matches in options + if ( of_type == o.value ) { + // if length is allowed for selected type + if(o.length) + { + // set the values in model + has_length = true; + m.set('is_tlength', true, {silent: true}); + m.set('min_val', o.min_val, {silent: true}); + m.set('max_val', o.max_val, {silent: true}); + } + } + }); + + if (!has_length && !_.isUndefined(val)) { + setTimeout(function() { + m.set('typlen', undefined); + }, 10); + } + + return !(m.get('is_tlength')); + } + if (!has_length && !_.isUndefined(val)) { + setTimeout(function() { + m.set('typlen', undefined); + }, 10); + } + return true; + }, + cellHeaderClasses: 'width_percent_10' + },{ + id: 'precision', label: gettext('Precision'), + type: 'int', deps: ['datatype'], + cell: 'string', group: gettext('Definition'), + disabled: function(m) { + var val = m.get('precision'); + if(!(_.isUndefined(m.get('inheritedid')) + || _.isNull(m.get('inheritedid')) + || _.isUndefined(m.get('inheritedfrom')) + || _.isNull(m.get('inheritedfrom')))) { + + if (!_.isUndefined(val)) { + setTimeout(function() { + m.set('precision', undefined); + }, 10); + } + return true; + } + + var of_type = m.get('datatype'), + has_precision = false; + + if(m.type_options) { + m.set('is_precision', false, {silent: true}); + // iterating over all the types + _.each(m.type_options, function(o) { + // if type from selected from combobox matches in options + if ( of_type == o.value ) { + // if precession is allowed for selected type + if(o.precision) + { + has_precision = true; + // set the values in model + m.set('is_precision', true, {silent: true}); + m.set('min_val', o.min_val, {silent: true}); + m.set('max_val', o.max_val, {silent: true}); + } + } + }); + if (!has_precision && !_.isUndefined(val)) { + setTimeout(function() { + m.set('precision', undefined); + }, 10); + } + return !(m.get('is_precision')); + } + if (!has_precision && !_.isUndefined(val)) { + setTimeout(function() { + m.set('precision', undefined); + }, 10); + } + return true; + }, cellHeaderClasses: 'width_percent_10' + },{ + id: 'typdefault', label: gettext('Default'), type: 'text', + cell: 'string', min_version: 90300, group: gettext('Definition'), + placeholder: "Enter an expression or a value.", + cellHeaderClasses: 'width_percent_10', + editable: function(m) { + if(!(_.isUndefined(m.get('inheritedid')) + || _.isNull(m.get('inheritedid')) + || _.isUndefined(m.get('inheritedfrom')) + || _.isNull(m.get('inheritedfrom')))) { return false; } + if (this.get('node_info').server.version < 90300){ + return false; + } + return true; + } + },{ + id: 'attnotnull', label: gettext('Not Null'), + cell: 'boolean',type: 'switch', editable: 'is_editable_column', + cellHeaderClasses: 'width_percent_10', group: gettext('Definition') + },{ + id: 'attstattarget', label: gettext('Statistics'), min_version: 90200, + cell: 'integer', type: 'int', group: gettext('Definition'), + editable: function(m) { + if (_.isUndefined(m.isNew) || m.isNew()) { return false; } + if (this.get('node_info').server.version < 90200){ + return false; + } + return (_.isUndefined(m.get('inheritedid')) || _.isNull(m.get('inheritedid')) + || _.isUndefined(m.get('inheritedfrom')) || _.isNull(m.get('inheritedfrom'))) ? true : false + }, cellHeaderClasses: 'width_percent_10' + },{ + id: 'collname', label: gettext('Collation'), cell: 'node-ajax-options', + control: 'node-ajax-options', type: 'text', url: 'get_collations', + min_version: 90300, editable: function(m) { + if (!(_.isUndefined(m.isNew)) && !m.isNew()) { return false; } + return (_.isUndefined(m.get('inheritedid')) || _.isNull(m.get('inheritedid')) + || _.isUndefined(m.get('inheritedfrom')) || _.isNull(m.get('inheritedfrom'))) ? true : false + }, + cellHeaderClasses: 'width_percent_20', group: gettext('Definition') + },{ + id: 'attnum', cell: 'string',type: 'text', visible: false + },{ + id: 'inheritedfrom', label: gettext('Inherited From'), cell: 'string', + type: 'text', visible: false, mode: ['properties', 'edit'], + cellHeaderClasses: 'width_percent_10' + },{ + id: 'coloptions', label: gettext('Options'), cell: 'string', + type: 'collection', group: gettext('Options'), mode: ['edit', 'create'], + model: ColumnOptionsModel, canAdd: true, canDelete: true, canEdit: false, + control: Backform.UniqueColCollectionControl, uniqueCol : ['option'], + min_version: 90200 + }], + validate: function() { + var errmsg = null; + + if (_.isUndefined(this.get('attname')) || String(this.get('attname')).replace(/^\s+|\s+$/g, '') == '') { + errmsg = gettext('Column Name cannot be empty!'); + this.errorModel.set('attname', errmsg); + } else { + this.errorModel.unset('attname'); + } + + if (_.isUndefined(this.get('datatype')) || String(this.get('datatype')) + .replace(/^\s+|\s+$/g, '') == '') { + errmsg = gettext('Column Datatype cannot be empty!'); + this.errorModel.set('datatype', errmsg); + } else { + this.errorModel.unset('datatype'); + } + + return errmsg; + }, + is_editable_column: function(m) { + return (_.isUndefined(m.get('inheritedid')) || _.isNull(m.get('inheritedid')) + || _.isUndefined(m.get('inheritedfrom')) || _.isNull(m.get('inheritedfrom'))) ? true : false + }, + toJSON: Backbone.Model.prototype.toJSON + }); + + + /* NodeAjaxOptionsMultipleControl is for multiple selection of Combobox. + * This control is used to select Multiple Parent Tables to be inherited. + * It also populates/vacates Columns on selection/deselection of the option (i.e. table name). + * To populates the column, it calls the server and fetch the columns data + * for the selected table. + */ + var NodeAjaxOptionsMultipleControl = Backform.NodeAjaxOptionsControl.extend({ + onChange: function(e) { + var model = this.model, + $el = $(e.target), + attrArr = this.field.get("name").split('.'), + name = attrArr.shift(), + path = attrArr.join('.'), + value = this.getValueFromDOM(), + changes = {}, + columns = model.get('columns'), + inherits = model.get(name); + + if (this.model.errorModel instanceof Backbone.Model) { + if (_.isEmpty(path)) { + this.model.errorModel.unset(name); + } else { + var nestedError = this.model.errorModel.get(name); + if (nestedError) { + this.keyPathSetter(nestedError, path, null); + this.model.errorModel.set(name, nestedError); + } + } + } + + var self = this; + + if (typeof(inherits) == "string"){ inherits = JSON.parse(inherits); } + + // Remove Columns if inherit option is deselected from the combobox + if(_.size(value) < _.size(inherits)) { + var dif = _.difference(inherits, value); + var rmv_columns = columns.where({inheritedid: parseInt(dif[0])}); + columns.remove(rmv_columns); + } + else + { + _.each(value, function(i) { + // Fetch Columns from server + var fnd_columns = columns.where({inheritedid: parseInt(i)}); + if (fnd_columns && fnd_columns.length <= 0) { + inhted_columns = self.fetchColumns(i); + columns.add(inhted_columns); + } + }); + } + + changes[name] = _.isEmpty(path) ? value : _.clone(model.get(name)) || {}; + this.stopListening(this.model, "change:" + name, this.render); + model.set(changes); + this.listenTo(this.model, "change:" + name, this.render); + }, + fetchColumns: function(table_id){ + var self = this, + url = 'get_columns', + m = self.model.top || self.model; + + if (url) { + var node = this.field.get('schema_node'), + node_info = this.field.get('node_info'), + full_url = node.generate_url.apply( + node, [ + null, url, this.field.get('node_data'), + this.field.get('url_with_id') || false, node_info + ]), + cache_level = this.field.get('cache_level') || node.type, + cache_node = this.field.get('cache_node'); + + cache_node = (cache_node && pgBrowser.Nodes['cache_node']) || node; + + m.trigger('pgadmin:view:fetching', m, self.field); + var data = {attrelid: table_id} + + // Fetching Columns data for the selected table. + $.ajax({ + async: false, + url: full_url, + data: data, + success: function(res) { + /* + * We will cache this data for short period of time for avoiding + * same calls. + */ + data = cache_node.cache(url, node_info, cache_level, res.data); + + }, + error: function() { + m.trigger('pgadmin:view:fetch:error', m, self.field); + } + }); + m.trigger('pgadmin:view:fetched', m, self.field); + + // To fetch only options from cache, we do not need time from 'at' + // attribute but only options. + // + // It is feasible that the data may not have been fetched. + data = (data && data.data) || []; + return data; + } + }, + }); + + + // Constraints Model + var ConstraintModel = pgBrowser.Node.Model.extend({ + idAttribute: 'conoid', + initialize: function(attrs, args) { + var isNew = (_.size(attrs) === 0); + if (!isNew) { + this.convalidated_default = this.get('convalidated') + } + pgBrowser.Node.Model.prototype.initialize.apply(this, arguments); + }, + defaults: { + conoid: undefined, + conname: undefined, + consrc: undefined, + connoinherit: undefined, + convalidated: true, + conislocal: undefined + }, + convalidated_default: true, + schema: [{ + id: 'conoid', type: 'text', cell: 'string', visible: false + },{ + id: 'conname', label: gettext('Name'), type: 'text', cell: 'string', + editable: 'is_editable', cellHeaderClasses: 'width_percent_30' + },{ + id: 'consrc', label: gettext('Check'), type: 'multiline', + editable: 'is_editable', cell: Backgrid.Extension.TextareaCell, + cellHeaderClasses: 'width_percent_30' + },{ + id: 'connoinherit', label: gettext('No Inherit'), type: 'switch', + cell: 'boolean', editable: 'is_editable', + cellHeaderClasses: 'width_percent_20' + },{ + id: 'convalidated', label: gettext('Validate?'), type: 'switch', + cell: 'boolean', cellHeaderClasses: 'width_percent_20', + editable: function(m) { + var server = this.get('node_info').server; + if (_.isUndefined(m.isNew)) { return true; } + if (!m.isNew()) { + if(m.get('convalidated') && m.convalidated_default) { + return false; + } + return true; + } + return true; + } + } + ], + validate: function() { + var err = {}, + errmsg; + + if (_.isUndefined(this.get('conname')) || String(this.get('conname')).replace(/^\s+|\s+$/g, '') == '') { + err['conname'] = gettext('Constraint Name cannot be empty!'); + errmsg = errmsg || err['conname']; + } + + if (_.isUndefined(this.get('consrc')) || String(this.get('consrc')) + .replace(/^\s+|\s+$/g, '') == '') { + err['consrc'] = gettext('Constraint Check cannot be empty!'); + errmsg = errmsg || err['consrc']; + } + + this.errorModel.clear().set(err); + + return errmsg; + }, + is_editable: function(m) { + return _.isUndefined(m.isNew) ? true : m.isNew(); + }, + toJSON: Backbone.Model.prototype.toJSON + }); + + + // Options Model + var OptionsModel = pgBrowser.Node.Model.extend({ + defaults: { + option: undefined, + value: undefined + }, + schema: [{ + id: 'option', label: gettext('Option'), cell: 'string', type: 'text', + editable: true, cellHeaderClasses:'width_percent_50' + },{ + id: 'value', label: gettext('Value'), cell: 'string',type: 'text', + editable: true, cellHeaderClasses:'width_percent_50' + } + ], + validate: function() { + // TODO: Add validation here + }, + toJSON: Backbone.Model.prototype.toJSON + }); + + + if (!pgBrowser.Nodes['foreign_table']) { + pgBrowser.Nodes['foreign_table'] = pgBrowser.Node.extend({ + type: 'foreign_table', + sqlAlterHelp: 'sql-alterforeigntable.html', + sqlCreateHelp: 'sql-createforeigntable.html', + dialogHelp: url_for('help.static', {'filename': 'foreign_table_dialog.html'}), + label: gettext('Foreign Table'), + collection_type: 'coll-foreign_table', + hasSQL: true, + hasDepends: true, + hasScriptTypes: ['create', 'select', 'insert', 'update', 'delete'], + parent_type: ['schema'], + Init: function() { + /* Avoid multiple registration of menus */ + if (this.initialized) + return; + + this.initialized = true; + + pgBrowser.add_menus([{ + name: 'create_foreign-table_on_coll', node: 'coll-foreign_table', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('Foreign Table...'), + icon: 'wcTabIcon icon-foreign-table', data: {action: 'create', check: true}, + enable: 'canCreate' + },{ + name: 'create_foreign-table', node: 'foreign_table', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('Foreign Table...'), + icon: 'wcTabIcon icon-foreign-table', data: {action: 'create', check: true}, + enable: 'canCreate' + },{ + name: 'create_foreign-table', node: 'schema', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('Foreign Table...'), + icon: 'wcTabIcon icon-foreign_table', data: {action: 'create', check: false}, + enable: 'canCreate' + } + ]); + + }, + canDrop: pgBrowser.Nodes['schema'].canChildDrop, + canDropCascade: pgBrowser.Nodes['schema'].canChildDrop, + model: pgBrowser.Node.Model.extend({ + initialize: function(attrs, args) { + var isNew = (_.size(attrs) === 0); + if (isNew) { + var schema = args.node_info.schema._label, + userInfo = pgBrowser.serverInfo[args.node_info.server._id].user; + + // Set Selected Schema and Current User + this.set({ + 'basensp': schema, 'owner': userInfo.name + }, {silent: true}); + } + pgBrowser.Node.Model.prototype.initialize.apply(this, arguments); + }, + defaults: { + name: undefined, + oid: undefined, + owner: undefined, + basensp: undefined, + description: undefined, + ftsrvname: undefined, + strftoptions: undefined, + inherits: [], + columns: [], + constraints: [], + ftoptions: [], + relacl: [], + stracl: [], + seclabels: [] + }, + schema: [{ + id: 'name', label: gettext('Name'), cell: 'string', + type: 'text', mode: ['properties', 'create', 'edit'] + },{ + id: 'oid', label: gettext('OID'), cell: 'string', + type: 'text' , mode: ['properties'] + },{ + id: 'owner', label: gettext('Owner'), cell: 'string', + control: Backform.NodeListByNameControl, + node: 'role', type: 'text', select2: { allowClear: false } + },{ + id: 'basensp', label: gettext('Schema'), cell: 'node-list-by-name', + control: 'node-list-by-name', cache_level: 'database', type: 'text', + node: 'schema', mode:['create', 'edit'] + },{ + id: 'description', label: gettext('Comment'), cell: 'string', + type: 'multiline' + },{ + id: 'ftsrvname', label: gettext('Foreign server'), cell: 'string', control: 'node-ajax-options', + type: 'text', group: gettext('Definition'), url: 'get_foreign_servers', disabled: function(m) { return !m.isNew(); } + },{ + id: 'inherits', label: gettext('Inherits'), group: gettext('Definition'), + type: 'array', min_version: 90500, control: NodeAjaxOptionsMultipleControl, + url: 'get_tables', select2: {multiple: true}, + 'cache_level': 'database', + transform: function(d, self){ + if (this.field.get('mode') == 'edit') { + var oid = this.model.get('oid'); + var s = _.findWhere(d, {'id': oid}); + if (s) { + d = _.reject(d, s); + } + } + return d; + } + },{ + id: 'columns', label: gettext('Columns'), cell: 'string', + type: 'collection', group: gettext('Columns'), visible: false, mode: ['edit', 'create'], + model: ColumnsModel, canAdd: true, canDelete: true, canEdit: true, + columns: ['attname', 'datatype', 'inheritedfrom'], + canDeleteRow: function(m) { + return (_.isUndefined(m.get('inheritedid')) || _.isNull(m.get('inheritedid')) + || _.isUndefined(m.get('inheritedfrom')) || _.isNull(m.get('inheritedfrom'))) ? true : false + }, + canEditRow: function(m) { + return (_.isUndefined(m.get('inheritedid')) || _.isNull(m.get('inheritedid')) + || _.isUndefined(m.get('inheritedfrom')) || _.isNull(m.get('inheritedfrom'))) ? true : false + } + }, + { + id: 'constraints', label: gettext('Constraints'), cell: 'string', + type: 'collection', group: gettext('Constraints'), visible: false, mode: ['edit', 'create'], + model: ConstraintModel, canAdd: true, canDelete: true, columns: ['conname','consrc', 'connoinherit', 'convalidated'], + canEdit: function(o) { + if (o instanceof Backbone.Model) { + if (o instanceof ConstraintModel) { + return o.isNew(); + } + } + return true; + }, min_version: 90500, canDeleteRow: function(m) { + return (m.get('conislocal') == true || _.isUndefined(m.get('conislocal'))) ? true : false + } + },{ + id: 'strftoptions', label: gettext('Options'), cell: 'string', + type: 'text', group: gettext('Definition'), mode: ['properties'] + },{ + id: 'ftoptions', label: gettext('Options'), cell: 'string', + type: 'collection', group: gettext('Options'), mode: ['edit', 'create'], + model: OptionsModel, canAdd: true, canDelete: true, canEdit: false, + control: 'unique-col-collection', uniqueCol : ['option'] + },{ + id: 'relacl', label: gettext('Privileges'), cell: 'string', + type: 'text', group: gettext('Security'), + mode: ['properties'], min_version: 90200 + }, pgBrowser.SecurityGroupUnderSchema, { + id: 'acl', label: gettext('Privileges'), model: pgAdmin + .Browser.Node.PrivilegeRoleModel.extend( + {privileges: ['a','r','w','x']}), uniqueCol : ['grantee', 'grantor'], + editable: false, type: 'collection', group: 'security', + mode: ['edit', 'create'], + canAdd: true, canDelete: true, control: 'unique-col-collection', + min_version: 90200 + },{ + id: 'seclabels', label: gettext('Security Labels'), + model: pgBrowser.SecLabelModel, type: 'collection', + group: 'security', mode: ['edit', 'create'], + min_version: 90100, canAdd: true, + canEdit: false, canDelete: true, + control: 'unique-col-collection', uniqueCol : ['provider'] + } + ], + validate: function() + { + var err = {}, + errmsg, + seclabels = this.get('seclabels'); + + if (_.isUndefined(this.get('name')) || String(this.get('name')).replace(/^\s+|\s+$/g, '') == '') { + err['name'] = gettext('Name cannot be empty.'); + errmsg = errmsg || err['name']; + } + + if (_.isUndefined(this.get('basensp')) || String(this.get('basensp')) + .replace(/^\s+|\s+$/g, '') == '') { + err['basensp'] = gettext('Schema cannot be empty.'); + errmsg = errmsg || err['basensp']; + } + + if (_.isUndefined(this.get('ftsrvname')) || String(this.get('ftsrvname')).replace(/^\s+|\s+$/g, '') == '') { + err['ftsrvname'] = gettext('Foreign server cannot be empty.'); + errmsg = errmsg || err['ftsrvname']; + } + + this.errorModel.clear().set(err); + + return null; + } + }), + canCreate: function(itemData, item, data) { + //If check is false then , we will allow create menu + if (data && data.check == false) + return true; + + var t = pgBrowser.tree, i = item, d = itemData; + // To iterate over tree to check parent node + while (i) { + // If it is schema then allow user to create foreign table + if (_.indexOf(['schema'], d._type) > -1) + return true; + + if ('coll-foreign-table' == d._type) { + //Check if we are not child of catalog + var prev_i = t.hasParent(i) ? t.parent(i) : null, + prev_d = prev_i ? t.itemData(prev_i) : null; + if( prev_d._type == 'catalog') { + return false; + } else { + return true; + } + } + i = t.hasParent(i) ? t.parent(i) : null; + d = i ? t.itemData(i) : null; + } + // by default we do not want to allow create menu + return true; + } + }); + + } + + return pgBrowser.Nodes['foreign_table']; +}); diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_add.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_add.py index d11e11c2..3dcf456c 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_add.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_add.py @@ -30,7 +30,7 @@ class ForeignTableAddTestCase(BaseTestGenerator): scenarios = [ # Fetching default URL for foreign server node. - ('Check foreign table Node', dict(url='/browser/foreign-table/obj/')) + ('Check foreign table Node', dict(url='/browser/foreign_table/obj/')) ] def setUp(self): diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_delete.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_delete.py index 86ea0ae6..731e32b0 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_delete.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_delete.py @@ -30,7 +30,7 @@ class ForeignTableDeleteTestCase(BaseTestGenerator): scenarios = [ # Fetching default URL for foreign table node. - ('Check foreign table Node', dict(url='/browser/foreign-table/obj/')) + ('Check foreign table Node', dict(url='/browser/foreign_table/obj/')) ] def setUp(self): diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_get.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_get.py index 7c4faf1c..329bd34a 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_get.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_get.py @@ -29,7 +29,7 @@ class ForeignTableGetTestCase(BaseTestGenerator): """ scenarios = [ # Fetching default URL for foreign server node. - ('Check foreign table Node', dict(url='/browser/foreign-table/obj/')) + ('Check foreign table Node', dict(url='/browser/foreign_table/obj/')) ] def setUp(self): diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_put.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_put.py index b46e1844..ee219047 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_put.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/foreign_tables/tests/test_foreign_table_put.py @@ -30,7 +30,7 @@ class ForeignTablePutTestCase(BaseTestGenerator): """ scenarios = [ # Fetching default URL for foreign server node. - ('Check foreign table Node', dict(url='/browser/foreign-table/obj/')) + ('Check foreign table Node', dict(url='/browser/foreign_table/obj/')) ] def setUp(self): diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbfuncs/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbfuncs/__init__.py index 4d34f131..42187b0d 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbfuncs/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbfuncs/__init__.py @@ -109,6 +109,14 @@ class EdbFuncModule(CollectionNodeModule): self.SHOW_ON_BROWSER, category_label=gettext('Nodes') ) + @property + def module_use_template_javascript(self): + """ + Returns whether Jinja2 template is used for generating the javascript + module. + """ + return False + blueprint = EdbFuncModule(__name__) @@ -693,6 +701,14 @@ class EdbProcModule(CollectionNodeModule): self.SHOW_ON_BROWSER, category_label=gettext('Nodes') ) + @property + def module_use_template_javascript(self): + """ + Returns whether Jinja2 template is used for generating the javascript + module. + """ + return False + procedure_blueprint = EdbProcModule(__name__) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbfuncs/static/js/edbfunc.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbfuncs/static/js/edbfunc.js new file mode 100644 index 00000000..7ea2b397 --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbfuncs/static/js/edbfunc.js @@ -0,0 +1,112 @@ +/* Create and Register Function Collection and Node. */ +define('pgadmin.node.edbfunc', [ + 'sources/gettext', 'sources/url_for', 'jquery', 'underscore', + 'underscore.string', 'pgadmin', 'pgadmin.browser', 'alertify', + 'pgadmin.browser.collection', 'pgadmin.browser.server.privilege' +], function(gettext, url_for, $, _, S, pgAdmin, pgBrowser, alertify) { + + if (!pgBrowser.Nodes['coll-edbfunc']) { + pgBrowser.Nodes['coll-edbfunc'] = + pgBrowser.Collection.extend({ + node: 'edbfunc', + label: gettext('Functions'), + type: 'coll-edbfunc', + columns: ['name', 'funcowner', 'description'] + }); + }; + + if (!pgBrowser.Nodes['edbfunc']) { + pgBrowser.Nodes['edbfunc'] = pgBrowser.Node.extend({ + type: 'edbfunc', + dialogHelp: url_for('help.static', {'filename': 'edbfunc_dialog.html'}), + label: gettext('Function'), + collection_type: 'coll-edbfunc', + hasDepends: true, + canEdit: false, + hasSQL: true, + hasScriptTypes: [], + parent_type: ['package'], + Init: function(args) { + /* Avoid multiple registration of menus */ + if (this.initialized) + return; + + this.initialized = true; + + }, + canDrop: false, + canDropCascade: false, + model: pgBrowser.Node.Model.extend({ + defaults: { + name: undefined, + oid: undefined, + funcowner: undefined, + pronargs: undefined, /* Argument Count */ + proargs: undefined, /* Arguments */ + proargtypenames: undefined, /* Argument Signature */ + prorettypename: undefined, /* Return Type */ + lanname: 'sql', /* Language Name in which function is being written */ + prosrc: undefined, + proacl: undefined, + visibility: 'Unknown' + }, + schema: [{ + id: 'name', label: gettext('Name'), cell: 'string', + type: 'text', mode: ['properties'], + disabled: true + },{ + id: 'oid', label: gettext('OID'), cell: 'string', + type: 'text' , mode: ['properties'] + },{ + id: 'funcowner', label: gettext('Owner'), cell: 'string', + type: 'text', disabled: true + },{ + id: 'pronargs', label: gettext('Argument count'), cell: 'string', + type: 'text', group: gettext('Definition'), mode: ['properties'] + },{ + id: 'proargs', label: gettext('Arguments'), cell: 'string', + type: 'text', group: gettext('Definition'), mode: ['properties'], + disabled: true + },{ + id: 'proargtypenames', label: gettext('Signature arguments'), cell: + 'string', type: 'text', group: gettext('Definition'), mode: ['properties'], + disabled: true + },{ + id: 'prorettypename', label: gettext('Return type'), cell: 'string', + type: 'text', group: gettext('Definition'), disabled: true, + mode: ['properties'], visible: 'isVisible' + },{ + id: 'visibility', label: gettext('Visibility'), cell: 'string', + type: 'text', mode: ['properties'], + disabled: true + },{ + id: 'lanname', label: gettext('Language'), cell: 'string', + type: 'text', group: gettext('Definition'), disabled: true + },{ + id: 'prosrc', label: gettext('Code'), cell: 'string', + type: 'text', mode: ['properties'], + group: gettext('Definition'), + control: Backform.SqlFieldControl, + extraClasses:['custom_height_css_class'], + visible: function(m) { + if (m.get('lanname') == 'c') { + return false; + } + return true; + }, disabled: true + }], + validate: function() + { + return null; + }, + isVisible: function(m){ + if (this.name == 'sysproc') { return false; } + return true; + } + }) + }); + + } + + return pgBrowser.Nodes['edbfunc']; +}); diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbfuncs/static/js/edbproc.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbfuncs/static/js/edbproc.js new file mode 100644 index 00000000..c13b9267 --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbfuncs/static/js/edbproc.js @@ -0,0 +1,67 @@ +/* Create and Register Procedure Collection and Node. */ +define('pgadmin.node.edbproc', [ + 'sources/gettext', 'sources/url_for', 'jquery', 'underscore', + 'underscore.string', 'pgadmin', 'pgadmin.browser', 'alertify', + 'pgadmin.node.edbfunc', 'pgadmin.browser.collection', + 'pgadmin.browser.server.privilege' +], function( + gettext, url_for, $, _, S, pgAdmin, pgBrowser, alertify, EdbFunction +) { + + if (!pgBrowser.Nodes['coll-edbproc']) { + pgAdmin.Browser.Nodes['coll-edbproc'] = + pgAdmin.Browser.Collection.extend({ + node: 'edbproc', + label: gettext('Procedures'), + type: 'coll-edbproc', + columns: ['name', 'funcowner', 'description'], + hasStatistics: true + }); + }; + + // Inherit Functions Node + if (!pgBrowser.Nodes['edbproc']) { + pgAdmin.Browser.Nodes['edbproc'] = pgBrowser.Node.extend({ + type: 'edbproc', + dialogHelp: url_for('help.static', {'filename': 'edbproc_dialog.html'}), + label: gettext('Procedure'), + collection_type: 'coll-edbproc', + hasDepends: true, + canEdit: false, + hasSQL: true, + hasScriptTypes: [], + parent_type: ['package'], + Init: function() { + /* Avoid multiple registration of menus */ + if (this.proc_initialized) + return; + + this.proc_initialized = true; + + }, + canDrop: false, + canDropCascade: false, + model: EdbFunction.model.extend({ + defaults: _.extend({}, + EdbFunction.model.prototype.defaults, + { + lanname: 'edbspl' + } + ), + isVisible: function(m){ + if (this.name == 'sysfunc') { return false; } + else if (this.name == 'sysproc') { return true; } + return false; + }, + validate: function() + { + return null; + } + } + ) + }); + + } + + return pgBrowser.Nodes['edbproc']; +}); diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbvars/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbvars/__init__.py index 96d78918..7a41194c 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbvars/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbvars/__init__.py @@ -91,6 +91,14 @@ class EdbVarModule(CollectionNodeModule): """ return False + @property + def module_use_template_javascript(self): + """ + Returns whether Jinja2 template is used for generating the javascript + module. + """ + return False + blueprint = EdbVarModule(__name__) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbvars/static/js/edbvar.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbvars/static/js/edbvar.js new file mode 100644 index 00000000..1933326d --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/packages/edbvars/static/js/edbvar.js @@ -0,0 +1,70 @@ +/* Create and Register Function Collection and Node. */ +define('pgadmin.node.edbvar', [ + 'sources/gettext', 'sources/url_for', 'jquery', 'underscore', + 'underscore.string', 'pgadmin', 'pgadmin.browser', 'alertify', + 'pgadmin.browser.collection', 'pgadmin.browser.server.privilege' +], function(gettext, url_for, $, _, S, pgAdmin, pgBrowser, alertify) { + + if (!pgBrowser.Nodes['coll-edbvar']) { + pgBrowser.Nodes['coll-edbvar'] = + pgBrowser.Collection.extend({ + node: 'edbvar', + label: gettext('Variables'), + type: 'coll-edbvar', + columns: ['name', 'funcowner', 'description'] + }); + }; + + if (!pgBrowser.Nodes['edbvar']) { + pgBrowser.Nodes['edbvar'] = pgBrowser.Node.extend({ + type: 'edbvar', + dialogHelp: url_for('help.static', {'filename': 'edbvar_dialog.html'}), + label: gettext('Function'), + collection_type: 'coll-edbvar', + canEdit: false, + hasSQL: true, + hasScriptTypes: [], + parent_type: ['package'], + Init: function(args) { + /* Avoid mulitple registration of menus */ + if (this.initialized) + return; + + this.initialized = true; + + }, + canDrop: false, + canDropCascade: false, + model: pgBrowser.Node.Model.extend({ + defaults: { + name: undefined, + oid: undefined, + datatype: undefined, + visibility: 'Unknown' + }, + schema: [{ + id: 'name', label: gettext('Name'), cell: 'string', + type: 'text', mode: ['properties'], + disabled: true + },{ + id: 'oid', label: gettext('OID'), cell: 'string', + type: 'text' , mode: ['properties'] + },{ + id: 'datatype', label: gettext('Data type'), cell: 'string', + type: 'text', disabled: true + },{ + id: 'visibility', label: gettext('Visibility'), cell: 'string', + type: 'text', mode: ['properties'], + disabled: true + }], + validate: function() + { + return null; + } + }) + }); + + } + + return pgBrowser.Nodes['edbvar']; +}); diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/__init__.py index b5b88e90..a22703c9 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/__init__.py @@ -92,6 +92,14 @@ class ColumnsModule(CollectionNodeModule): """ return False + @property + def module_use_template_javascript(self): + """ + Returns whether Jinja2 template is used for generating the javascript + module. + """ + return False + blueprint = ColumnsModule(__name__) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/static/js/column.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/static/js/column.js new file mode 100644 index 00000000..10737e0a --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/column/static/js/column.js @@ -0,0 +1,734 @@ +define('pgadmin.node.column', [ + 'sources/gettext', 'sources/url_for', 'jquery', 'underscore', + 'underscore.string', 'pgadmin', 'pgadmin.browser', 'backform', 'alertify', + 'pgadmin.browser.collection' +], function(gettext, url_for, $, _, S, pgAdmin, pgBrowser, Backform, alertify) { + + if (!pgBrowser.Nodes['coll-column']) { + var databases = pgBrowser.Nodes['coll-column'] = + pgBrowser.Collection.extend({ + node: 'column', + label: gettext('Columns'), + type: 'coll-column', + columns: ['name', 'atttypid', 'description'] + }); + }; + + // This Node model will be used for variable control for column + var VariablesModel = Backform.VariablesModel = pgBrowser.Node.Model.extend({ + defaults: { + name: null, + value: null + }, + schema: [{ + id: 'name', label: gettext('Name'), cell: 'select2', + type: 'text', disabled: false, node: 'column', + options: [['n_distinct', 'n_distinct'], + ['n_distinct_inherited','n_distinct_inherited']], + select2: {placeholder: "Select variable"}, + cellHeaderClasses:'width_percent_50' + },{ + id: 'value', label: gettext('Value'), + type: 'text', disabled: false, + cellHeaderClasses:'width_percent_50' + }], + validate: function() { + var err = {}, + errmsg = null; + + if (_.isUndefined(this.get('value')) || + _.isNull(this.get('value')) || + String(this.get('value')).replace(/^\s+|\s+$/g, '') == '') { + errmsg = gettext('Please provide input for variable.'); + this.errorModel.set('value', errmsg); + return errmsg; + } else { + this.errorModel.unset('value'); + } + return null; + } + }); + + // Integer Cell for Columns Length and Precision + var IntegerDepCell = Backgrid.Extension.IntegerDepCell = + Backgrid.IntegerCell.extend({ + initialize: function() { + Backgrid.NumberCell.prototype.initialize.apply(this, arguments); + Backgrid.Extension.DependentCell.prototype.initialize.apply(this, arguments); + }, + dependentChanged: function () { + this.$el.empty(); + var model = this.model, + column = this.column, + editable = this.column.get("editable"), + is_editable = _.isFunction(editable) ? !!editable.apply(column, [model]) : !!editable; + + if (is_editable){ this.$el.addClass("editable"); } + else { this.$el.removeClass("editable"); } + + this.delegateEvents(); + return this; + }, + render: function() { + Backgrid.NumberCell.prototype.render.apply(this, arguments); + + var model = this.model, + column = this.column, + editable = this.column.get("editable"), + is_editable = _.isFunction(editable) ? !!editable.apply(column, [model]) : !!editable; + + if (is_editable){ this.$el.addClass("editable"); } + else { this.$el.removeClass("editable"); } + return this; + }, + remove: Backgrid.Extension.DependentCell.prototype.remove + }); + + if (!pgBrowser.Nodes['column']) { + pgBrowser.Nodes['column'] = pgBrowser.Node.extend({ + getTreeNodeHierarchy: pgBrowser.tableChildTreeNodeHierarchy, + parent_type: ['table', 'view', 'mview'], + collection_type: ['coll-table', 'coll-view', 'coll-mview'], + type: 'column', + label: gettext('Column'), + hasSQL: true, + sqlAlterHelp: 'sql-altertable.html', + sqlCreateHelp: 'sql-altertable.html', + dialogHelp: url_for('help.static', {'filename': 'column_dialog.html'}), + canDrop: function(itemData, item, data){ + if (pgBrowser.Nodes['schema'].canChildDrop.apply(this, [itemData, item, data])) { + var t = pgBrowser.tree, i = item, d = itemData, parents = []; + // To iterate over tree to check parent node + while (i) { + parents.push(d._type); + i = t.hasParent(i) ? t.parent(i) : null; + d = i ? t.itemData(i) : null; + } + + // Check if menu is allowed ? + if(_.indexOf(parents, 'catalog') > -1 || + _.indexOf(parents, 'view') > -1 || + _.indexOf(parents, 'mview') > -1) { + return false; + } else if(_.indexOf(parents, 'table') > -1) { + return true; + } + } else { + return false; + } + }, + hasDepends: true, + hasStatistics: true, + Init: function() { + /* Avoid mulitple registration of menus */ + if (this.initialized) + return; + + this.initialized = true; + + pgBrowser.add_menus([{ + name: 'create_column_on_coll', node: 'coll-column', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('Column...'), + icon: 'wcTabIcon icon-column', data: {action: 'create', check: true}, + enable: 'canCreate' + },{ + name: 'create_column', node: 'column', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('Column...'), + icon: 'wcTabIcon icon-column', data: {action: 'create', check: true}, + enable: 'canCreate' + },{ + name: 'create_column_onTable', node: 'table', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('Column...'), + icon: 'wcTabIcon icon-column', data: {action: 'create', check: true}, + enable: 'canCreate' + },{ + name: 'create_column_onView', node: 'view', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 4, label: gettext('Column...'), + icon: 'wcTabIcon icon-column', data: {action: 'create', check: true}, + enable: 'canCreate' + } + ]); + }, + model: pgBrowser.Node.Model.extend({ + idAttribute: 'attnum', + + defaults: { + name: undefined, + attowner: undefined, + atttypid: undefined, + attnum: undefined, + cltype: undefined, + collspcname: undefined, + attacl: undefined, + description: undefined, + parent_tbl: undefined, + min_val: undefined, + max_val: undefined, + edit_types: undefined, + is_primary_key: false, + inheritedfrom: undefined, + attstattarget:undefined, + attnotnull: false, + attlen: null, + attprecision: null + }, + schema: [{ + id: 'name', label: gettext('Name'), cell: 'string', + type: 'text', disabled: 'inSchemaWithColumnCheck', + cellHeaderClasses:'width_percent_30', + editable: 'editable_check_for_table' + },{ + // Need to show this field only when creating new table + // [in SubNode control] + id: 'is_primary_key', label: gettext('Primary key?'), + cell: Backgrid.Extension.TableChildSwitchCell, type: 'switch', + deps:['name'], cellHeaderClasses:'width_percent_5', + options: { + onText: gettext('Yes'), offText: gettext('No'), + onColor: 'success', offColor: 'primary' + }, + visible: function(m) { + return _.isUndefined( + m.top.node_info['table'] || m.top.node_info['view'] || + m.top.node_info['mview'] + ); + }, + disabled: function(m){ + // Disable it, when one of this: + // - Primary key already exist + // - Table is a partitioned table + if ( + m.top && (( + !_.isUndefined(m.top.get('oid')) && + m.top.get('primary_key').length > 0 && + !_.isUndefined(m.top.get('primary_key').first().get('oid')) + ) || ( + m.top.has('is_partitioned') && m.top.get('is_partitioned') + )) + ) { + return true; + } + + var name = m.get('name'); + + if(!m.inSchemaWithColumnCheck.apply(this, [m]) && + (_.isUndefined(name) || _.isNull(name) || name == '')) { + return true; + } + return false; + }, + editable: function(m){ + var name = m.get('name'); + // If HeaderCell then allow True + if(m instanceof Backbone.Collection) { + return true; + } + // If primary key already exist then disable. + if (m.top && !_.isUndefined(m.top.get('oid')) && + m.top.get('primary_key').length > 0 && + !_.isUndefined(m.top.get('primary_key').first().get('oid'))) { + + return false; + } + + // If table is partitioned table then disable + if (m.top && !_.isUndefined(m.top.get('is_partitioned')) && + m.top.get('is_partitioned')) + { + setTimeout(function () { + m.set('is_primary_key', false); + }, 10); + + return false; + } + + if(!m.inSchemaWithColumnCheck.apply(this, [m]) && + !_.isUndefined(name) && !_.isNull(name) && name !== '') { + return true; + } + return false; + // Set to false if no condition is met + m.set('is_primary_key', false); + } + },{ + id: 'attnum', label: gettext('Position'), cell: 'string', + type: 'text', disabled: 'notInSchema', mode: ['properties'] + },{ + id: 'cltype', label: gettext('Data type'), + cell: Backgrid.Extension.NodeAjaxOptionsCell.extend({ + exitEditMode: function(e) { + var self = this; + this.$select.off('blur', this.exitEditMode); + this.$select.select2('close'); + this.$el.removeClass('editor'); + // Once user have selected a value + // we can shift to next cell if it is editable + var next_cell, length_cell = this.$el.next(), + not_null_cell = this.$el.next().next().next(); + + // Add delay so that Select2 cell tab event is captured + // first before triggerring backgrid:edited event. + setTimeout(function() { + // First check Length column if it is disable then goto + // Not Null column + if(length_cell && length_cell.hasClass('editable') && e) { + next_cell = length_cell; + } else if(not_null_cell && not_null_cell.hasClass('editable') && e) { + next_cell = not_null_cell; + } + + if(next_cell) { + e.preventDefault(); + e.stopPropagation(); + var command = new Backgrid.Command({key: "Tab", keyCode: 9, which: 9}); + self.model.trigger("backgrid:edited", self.model, self.column, + command); + next_cell.focus(); + } + }, 20); + } + }), + type: 'text', disabled: 'inSchemaWithColumnCheck', + control: 'node-ajax-options', url: 'get_types', node: 'table', + cellHeaderClasses:'width_percent_30', first_empty: true, + select2: { allowClear: false }, group: gettext('Definition'), + transform: function(data, cell) { + /* 'transform' function will be called by control, and cell both. + * The way, we use the transform in cell, and control is different. + * Because - options are shared using 'column' object in backgrid, + * hence - the cell is passed as second parameter, while the control + * uses (this) as a object. + */ + var control = cell || this, + m = control.model; + + /* We need different data in create mode & in edit mode + * if we are in create mode then return data as it is + * if we are in edit mode then we need to filter data + */ + control.model.datatypes = data; + var edit_types = m.get('edit_types'), + result = []; + + // If called from Table, We will check if in edit mode + // then send edit_types only + if( !_.isUndefined(m.top) && !m.top.isNew() ) { + _.each(data, function(t) { + if (_.indexOf(edit_types, t.value) != -1) { + result.push(t); + } + }); + // There may be case that user adds new column in existing collection + // we will not have edit types then + return result.length > 0 ? result : data; + } + + // If called from Column + if(m.isNew()) { + return data; + } else { + //edit mode + _.each(data, function(t) { + if (_.indexOf(edit_types, t.value) != -1) { + result.push(t); + } + }); + + return result; + } + }, + editable: 'editable_check_for_table' + },{ + // Need to show this field only when creating new table [in SubNode control] + id: 'inheritedfrom', label: gettext('Inherited from table'), + type: 'text', disabled: true, editable: false, + cellHeaderClasses:'width_percent_10', + visible: function(m) { + return _.isUndefined(m.top.node_info['table'] || m.top.node_info['view'] || m.top.node_info['mview']); + } + },{ + id: 'attlen', label: gettext('Length'), cell: IntegerDepCell, + deps: ['cltype'], type: 'int', group: gettext('Definition'), cellHeaderClasses:'width_percent_20', + disabled: function(m) { + var of_type = m.get('cltype'), + flag = true; + _.each(m.datatypes, function(o) { + if ( of_type == o.value ) { + if(o.length) + { + m.set('min_val', o.min_val, {silent: true}); + m.set('max_val', o.max_val, {silent: true}); + flag = false; + } + } + }); + + flag && setTimeout(function() { + if(m.get('attlen')) { + m.set('attlen', null); + } + },10); + + return flag; + }, + editable: function(m) { + // inheritedfrom has value then we should disable it + if(!_.isUndefined(m.get('inheritedfrom'))) { + return false; + } + + if (!m.datatypes) { + // datatypes not loaded, may be this call is from CallByNeed from backgrid cell initialize. + return true; + } + var of_type = m.get('cltype'), + flag = false; + + _.each(m.datatypes, function(o) { + if ( of_type == o.value ) { + if(o.length) { + m.set('min_val', o.min_val, {silent: true}); + m.set('max_val', o.max_val, {silent: true}); + flag = true; + } + } + }); + + !flag && setTimeout(function() { + if(m.get('attlen')) { + m.set('attlen', null, {silent: true}); + } + },10); + + return flag; + } + },{ + id: 'attprecision', label: gettext('Precision'), cell: IntegerDepCell, + deps: ['cltype'], type: 'int', group: gettext('Definition'), cellHeaderClasses:'width_percent_20', + disabled: function(m) { + var of_type = m.get('cltype'), + flag = true; + _.each(m.datatypes, function(o) { + if ( of_type == o.value ) { + if(o.precision) { + m.set('min_val', o.min_val, {silent: true}); + m.set('max_val', o.max_val, {silent: true}); + flag = false; + } + } + }); + + flag && setTimeout(function() { + if(m.get('attprecision')) { + m.set('attprecision', null); + } + },10); + return flag; + }, + editable: function(m) { + // inheritedfrom has value then we should disable it + if(!_.isUndefined(m.get('inheritedfrom'))) { + return false; + } + + if (!m.datatypes) { + // datatypes not loaded yet, may be this call is from CallByNeed from backgrid cell initialize. + return true; + } + + var of_type = m.get('cltype'), + flag = false; + _.each(m.datatypes, function(o) { + if ( of_type == o.value ) { + if(o.precision) { + m.set('min_val', o.min_val, {silent: true}); + m.set('max_val', o.max_val, {silent: true}); + flag = true; + } + } + }); + + !flag && setTimeout(function() { + if(m.get('attprecision')) { + m.set('attprecision', null); + } + },10); + + return flag; + } + },{ + id: 'collspcname', label: gettext('Collation'), cell: 'string', + type: 'text', control: 'node-ajax-options', url: 'get_collations', + group: gettext('Definition'), node: 'collation', + deps: ['cltype'], disabled: function(m) { + var of_type = m.get('cltype'), + flag = true; + _.each(m.datatypes, function(o) { + if ( of_type == o.value ) { + if(o.is_collatable) + { + flag = false; + } + } + }); + if (flag) { + setTimeout(function(){ + if(m.get('collspcname') && m.get('collspcname') !== '') { + m.set('collspcname', ""); + } + }, 10); + } + return flag; + } + },{ + id: 'defval', label: gettext('Default Value'), cell: 'string', + type: 'text', group: gettext('Definition'), deps: ['cltype'], + disabled: function(m) { + if(!m.inSchemaWithModelCheck.apply(this, [m])) { + var type = m.get('cltype'); + return type == 'serial' || type == 'bigserial' + || type == 'smallserial'; + } + } + },{ + id: 'attnotnull', label: gettext('Not NULL?'), cell: 'switch', + type: 'switch', disabled: 'inSchemaWithColumnCheck', cellHeaderClasses:'width_percent_20', + group: gettext('Definition'), editable: 'editable_check_for_table', + options: { onText: 'Yes', offText: 'No', onColor: 'success', offColor: 'primary' } + },{ + id: 'attstattarget', label: gettext('Statistics'), cell: 'string', + type: 'text', disabled: 'inSchemaWithColumnCheck', mode: ['properties', 'edit'], + group: gettext('Definition') + },{ + id: 'attstorage', label: gettext('Storage'), group: gettext('Definition'), + type: 'text', mode: ['properties', 'edit'], + cell: 'string', disabled: 'inSchemaWithColumnCheck', first_empty: true, + control: 'select2', select2: { placeholder: "Select storage", + allowClear: false, + width: "100%" + }, + options: [ + {label: "PLAIN", value: "p"}, + {label: "MAIN", value: "m"}, + {label: "EXTERNAL", value: "e"}, + {label: "EXTENDED", value: "x"}, + ] + },{ + id: 'is_pk', label: gettext('Primary key?'), + type: 'switch', disabled: true, mode: ['properties'], + group: gettext('Definition') + },{ + id: 'is_fk', label: gettext('Foreign key?'), + type: 'switch', disabled: true, mode: ['properties'], + group: gettext('Definition') + },{ + id: 'is_inherited', label: gettext('Inherited?'), + type: 'switch', disabled: true, mode: ['properties'], + group: gettext('Definition') + },{ + id: 'tbls_inherited', label: gettext('Inherited from table(s)'), + type: 'text', disabled: true, mode: ['properties'], deps: ['is_inherited'], + group: gettext('Definition'), + visible: function(m) { + if (!_.isUndefined(m.get('is_inherited')) && m.get('is_inherited')) { + return true; + } else { + return false; + } + } + },{ + id: 'is_sys_column', label: gettext('System column?'), cell: 'string', + type: 'switch', disabled: true, mode: ['properties'] + },{ + id: 'description', label: gettext('Comment'), cell: 'string', + type: 'multiline', mode: ['properties', 'create', 'edit'], + disabled: 'notInSchema' + },{ + id: 'attoptions', label: 'Variables', type: 'collection', + group: gettext('Variables'), control: 'unique-col-collection', + model: VariablesModel, uniqueCol : ['name'], + mode: ['edit', 'create'], canAdd: true, canEdit: false, + canDelete: true + }, pgBrowser.SecurityGroupUnderSchema, { + id: 'attacl', label: 'Privileges', type: 'collection', + group: 'security', control: 'unique-col-collection', + model: pgBrowser.Node.PrivilegeRoleModel.extend({ + privileges: ['a','r','w','x']}), + mode: ['edit'], canAdd: true, canDelete: true, + uniqueCol : ['grantee'] + },{ + id: 'seclabels', label: gettext('Security Labels'), canAdd: true, + model: pgBrowser.SecLabelModel, group: 'security', + mode: ['edit', 'create'], editable: false, type: 'collection', + min_version: 90100, canEdit: false, canDelete: true, + control: 'unique-col-collection' + }], + validate: function(keys) { + var err = {}, + changedAttrs = this.changed, + msg = undefined; + + // Nothing to validate + if (keys && keys.length == 0) { + this.errorModel.clear(); + return null; + } else { + this.errorModel.clear(); + } + + if (_.isUndefined(this.get('name')) + || String(this.get('name')).replace(/^\s+|\s+$/g, '') == '') { + msg = gettext('Column name cannot be empty.'); + this.errorModel.set('name', msg); + return msg; + } + + if (_.isUndefined(this.get('cltype')) + || String(this.get('cltype')).replace(/^\s+|\s+$/g, '') == '') { + msg = gettext('Column type cannot be empty.'); + this.errorModel.set('cltype', msg); + return msg; + } + + if (!_.isUndefined(this.get('cltype')) + && !_.isUndefined(this.get('attlen')) + && !_.isNull(this.get('attlen')) + && this.get('attlen') !== '') { + // Validation for Length field + if (this.get('attlen') < this.get('min_val')) + msg = gettext('Length should not be less than: ') + this.get('min_val'); + if (this.get('attlen') > this.get('max_val')) + msg = gettext('Length should not be greater than: ') + this.get('max_val'); + // If we have any error set then throw it to user + if(msg) { + this.errorModel.set('attlen', msg) + return msg; + } + } + + if (!_.isUndefined(this.get('cltype')) + && !_.isUndefined(this.get('attprecision')) + && !_.isNull(this.get('attprecision')) + && this.get('attprecision') !== '') { + // Validation for precision field + if (this.get('attprecision') < this.get('min_val')) + msg = gettext('Precision should not be less than: ') + this.get('min_val'); + if (this.get('attprecision') > this.get('max_val')) + msg = gettext('Precision should not be greater than: ') + this.get('max_val'); + // If we have any error set then throw it to user + if(msg) { + this.errorModel.set('attprecision', msg) + return msg; + } + } + + return null; + }, + // We will check if we are under schema node & in 'create' mode + notInSchema: function() { + if(this.node_info && 'catalog' in this.node_info) + { + return true; + } + return false; + }, + // We will check if we are under schema node & in 'create' mode + inSchemaWithModelCheck: function(m) { + if(this.node_info && 'schema' in this.node_info) + { + // We will disable control if it's in 'edit' mode + if (m.isNew()) { + return false; + } else { + return true; + } + } + return true; + }, + // Checks weather to enable/disable control + inSchemaWithColumnCheck: function(m) { + var node_info = this.node_info || m.node_info || m.top.node_info; + + // disable all fields if column is listed under view or mview + if (node_info && ('view' in node_info || 'mview' in node_info)) { + if (this && _.has(this, 'name') && (this.name != 'defval')) { + return true; + } + } + + if(node_info && 'schema' in node_info) + { + // We will disable control if it's system columns + // inheritedfrom check is useful when we use this schema in table node + // inheritedfrom has value then we should disable it + if(!_.isUndefined(m.get('inheritedfrom'))) { + return true; + } + // ie: it's position is less then 1 + if (m.isNew()) { + return false; + } + // if we are in edit mode + if (!_.isUndefined(m.get('attnum')) && m.get('attnum') > 0 ) { + return false; + } else { + return true; + } + } + return true; + }, + editable_check_for_table: function(arg) { + if (arg instanceof Backbone.Collection) { + return !arg.model.prototype.inSchemaWithColumnCheck.apply( + this, [arg.top] + ); + } else { + return !arg.inSchemaWithColumnCheck.apply( + this, [arg] + ); + } + } + }), + // Below function will enable right click menu for creating column + canCreate: function(itemData, item, data) { + // If check is false then , we will allow create menu + if (data && data.check == false) + return true; + + var t = pgBrowser.tree, i = item, d = itemData, parents = []; + // To iterate over tree to check parent node + while (i) { + // If it is schema then allow user to create table + if (_.indexOf(['schema'], d._type) > -1) { + return true; + } + else if (_.indexOf(['view', 'coll-view', + 'mview', + 'coll-mview'], d._type) > -1) { + parents.push(d._type); + break; + } + parents.push(d._type); + i = t.hasParent(i) ? t.parent(i) : null; + d = i ? t.itemData(i) : null; + } + // If node is under catalog then do not allow 'create' menu + if (_.indexOf(parents, 'catalog') > -1 || + _.indexOf(parents, 'coll-view') > -1 || + _.indexOf(parents, 'coll-mview') > -1 || + _.indexOf(parents, 'mview') > -1 || + _.indexOf(parents, 'view') > -1) { + return false; + } else { + return true; + } + } + }); + } + + return pgBrowser.Nodes['column']; +}); diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/__init__.py index 4fda9d76..88cbe1d6 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/__init__.py @@ -72,6 +72,14 @@ class ConstraintsModule(CollectionNodeModule): """ return database.DatabaseModule.NODE_TYPE + @property + def module_use_template_javascript(self): + """ + Returns whether Jinja2 template is used for generating the javascript + module. + """ + return False + blueprint = ConstraintsModule(__name__) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/__init__.py index e3022aba..9de9d41e 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/__init__.py @@ -46,7 +46,7 @@ class CheckConstraintModule(CollectionNodeModule): - Load the module script for the Check Constraint, when any of the Check node is initialized. """ - NODE_TYPE = 'check_constraints' + NODE_TYPE = 'check_constraint' COLLECTION_LABEL = _("Check Constraints") def __init__(self, *args, **kwargs): @@ -75,6 +75,16 @@ class CheckConstraintModule(CollectionNodeModule): """ return database.DatabaseModule.NODE_TYPE + + @property + def module_use_template_javascript(self): + """ + Returns whether Jinja2 template is used for generating the javascript + module. + """ + return False + + @property def csssnippets(self): """ diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js new file mode 100644 index 00000000..52a197ea --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/static/js/check_constraint.js @@ -0,0 +1,235 @@ +// Check Constraint Module: Node +define('pgadmin.node.check_constraint', [ + 'sources/gettext', 'sources/url_for', 'jquery', 'underscore', + 'underscore.string', 'pgadmin', 'pgadmin.browser', 'alertify', + 'sources/alerts/alertify_wrapper', + + 'pgadmin.browser.collection' +], function(gettext, url_for, $, _, S, pgAdmin, pgBrowser, Alertify, AlertifyWrapper) { + + // Check Constraint Node + if (!pgBrowser.Nodes['check_constraints']) { + pgAdmin.Browser.Nodes['check_constraints'] = pgBrowser.Node.extend({ + getTreeNodeHierarchy: pgBrowser.tableChildTreeNodeHierarchy, + type: 'check_constraints', + label: gettext('Check'), + collection_type: 'coll-constraints', + sqlAlterHelp: 'ddl-alter.html', + sqlCreateHelp: 'ddl-constraints.html', + dialogHelp: url_for('help.static', {'filename': 'check_dialog.html'}), + hasSQL: true, + hasDepends: true, + parent_type: ['table','partition'], + Init: function() { + // Avoid mulitple registration of menus + if (this.initialized) + return; + + this.initialized = true; + + pgBrowser.add_menus([{ + name: 'create_check_constraints_on_coll', node: 'coll-constraints', module: this, + applies: ['object', 'context'], callback: 'show_obj_properties', + category: 'create', priority: 5, label: gettext('Check...'), + icon: 'wcTabIcon icon-check_constraints', data: {action: 'create', check: true}, + enable: 'canCreate' + },{ + name: 'validate_check_constraint', node: 'check_constraints', module: this, + applies: ['object', 'context'], callback: 'validate_check_constraint', + category: 'validate', priority: 4, label: gettext('Validate check constraint'), + icon: 'fa fa-link', enable : 'is_not_valid', data: {action: 'edit', check: true} + } + ]); + + }, + is_not_valid: function(itemData, item, data) { + if (this.canCreate(itemData, item, data)) { + return (itemData && !itemData.valid); + } else { + return false; + } + }, + callbacks: { + validate_check_constraint: function(args) { + var input = args || {}, + obj = this, + t = pgBrowser.tree, + i = input.item || t.selected(), + d = i && i.length == 1 ? t.itemData(i) : undefined; + + if (!d) { + return false; + } + var data = d; + $.ajax({ + url: obj.generate_url(i, 'validate', d, true), + type:'GET', + success: function(res) { + if (res.success == 1) { + var alertifyWrapper = new AlertifyWrapper(); + alertifyWrapper.success(res.info); + t.removeIcon(i); + data.valid = true; + data.icon = 'icon-check_constraints'; + t.addIcon(i, {icon: data.icon}); + setTimeout(function() {t.deselect(i);}, 10); + setTimeout(function() {t.select(i);}, 100); + } + }, + error: function(xhr, status, error) { + try { + var err = $.parseJSON(xhr.responseText); + if (err.success == 0) { + var alertifyWrapper = new AlertifyWrapper(); + alertifyWrapper.error(err.errormsg); + } + } catch (e) {} + t.unload(i); + } + }); + + return false; + } + }, + canDrop: pgBrowser.Nodes['schema'].canChildDrop, + model: pgAdmin.Browser.Node.Model.extend({ + idAttribute: 'oid', + + defaults: { + name: undefined, + oid: undefined, + description: undefined, + consrc: undefined, + connoinherit: undefined, + convalidated: true + }, + // Check Constraint Schema + schema: [{ + id: 'name', label: gettext('Name'), type:'text', cell:'string', + disabled: 'isDisabled' + },{ + id: 'oid', label: gettext('OID'), cell: 'string', + type: 'text' , mode: ['properties'] + },{ + id: 'comment', label: gettext('Comment'), type: 'multiline', cell: + 'string', mode: ['properties', 'create', 'edit'], + deps:['name'], disabled:function(m) { + var name = m.get('name'); + if (!(name && name != '')) { + setTimeout(function(){ + if(m.get('comment') && m.get('comment') !== '') + m.set('comment', null); + },10); + return true; + } else { + return false; + } + } + },{ + id: 'consrc', label: gettext('Check'), type: 'multiline', cell: + 'string', group: gettext('Definition'), mode: ['properties', + 'create', 'edit'], disabled: function(m) { + return ((_.has(m, 'handler') && + !_.isUndefined(m.handler) && + !_.isUndefined(m.get('oid'))) || (_.isFunction(m.isNew) && !m.isNew())); + }, editable: false + },{ + id: 'connoinherit', label: gettext('No Inherit?'), type: + 'switch', cell: 'boolean', group: gettext('Definition'), mode: + ['properties', 'create', 'edit'], min_version: 90200, + disabled: function(m) { + // Disabled if table is a partitioned table. + if ((_.has(m , 'top') && !_.isUndefined(m.top) && m.top.get('is_partitioned')) || + (_.has(m, 'node_info') && _.has(m.node_info, 'table') && + _.has(m.node_info.table, 'is_partitioned') && m.node_info.table.is_partitioned) + ){ + setTimeout(function(){ + m.set('connoinherit', false); + },10); + + return true; + } + + return ((_.has(m, 'handler') && + !_.isUndefined(m.handler) && + !_.isUndefined(m.get('oid'))) || (_.isFunction(m.isNew) && !m.isNew())); + } + },{ + id: 'convalidated', label: gettext("Don't validate?"), type: 'switch', cell: + 'boolean', group: gettext('Definition'), min_version: 90200, + disabled: function(m) { + if ((_.isFunction(m.isNew) && !m.isNew()) || + (_.has(m, 'handler') && + !_.isUndefined(m.handler) && + !_.isUndefined(m.get('oid')))) { + + return !m.get("convalidated"); + } else { + return false; + } + }, + mode: ['properties', 'create', 'edit'] + }], + // Client Side Validation + validate: function() { + var err = {}, + errmsg; + + if (_.isUndefined(this.get('consrc')) || String(this.get('consrc')).replace(/^\s+|\s+$/g, '') == '') { + err['consrc'] = gettext('Check cannot be empty!'); + errmsg = errmsg || err['consrc']; + } + + this.errorModel.clear().set(err); + + if (_.size(err)) { + this.trigger('on-status', {msg: errmsg}); + return errmsg; + } + + return null; + + }, + isDisabled: function(m){ + if ((_.has(m, 'handler') && + !_.isUndefined(m.handler) && + !_.isUndefined(m.get('oid'))) || + (_.isFunction(m.isNew) && !m.isNew())) { + var server = (this.node_info || m.top.node_info).server; + if (server.version < 90200) + { + return true; + } + } + return false; + } + }), + // Below function will enable right click menu for creating check constraint. + canCreate: function(itemData, item, data) { + // If check is false then , we will allow create menu + if (data && data.check == false) + return true; + + var t = pgBrowser.tree, i = item, d = itemData, parents = []; + // To iterate over tree to check parent node + while (i) { + // If it is schema then allow user to c reate table + if (_.indexOf(['schema'], d._type) > -1) + return true; + parents.push(d._type); + i = t.hasParent(i) ? t.parent(i) : null; + d = i ? t.itemData(i) : null; + } + // If node is under catalog then do not allow 'create' menu + if (_.indexOf(parents, 'catalog') > -1) { + return false; + } else { + return true; + } + } + }); + + } + + return pgBrowser.Nodes['check_constraints']; +}); diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_add.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_add.py index 1e28d51c..13a12a88 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_add.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_add.py @@ -25,7 +25,7 @@ class CheckConstraintAddTestCase(BaseTestGenerator): """This class will add check constraint to existing table""" scenarios = [ ('Add check constraint to table', - dict(url='/browser/check_constraints/obj/')) + dict(url='/browser/check_constraint/obj/')) ] def setUp(self): diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_delete.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_delete.py index 0dd361dc..ae58c9bb 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_delete.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_delete.py @@ -25,7 +25,7 @@ class CheckConstraintDeleteTestCase(BaseTestGenerator): """This class will delete check constraint to existing table""" scenarios = [ ('Delete check constraint to table', - dict(url='/browser/check_constraints/obj/')) + dict(url='/browser/check_constraint/obj/')) ] def setUp(self): diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_get.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_get.py index ee94bbc4..971570ae 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_get.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_get.py @@ -25,7 +25,7 @@ class CheckConstraintGetTestCase(BaseTestGenerator): """This class will fetch check constraint to existing table""" scenarios = [ ('Fetch check constraint to table', - dict(url='/browser/check_constraints/obj/')) + dict(url='/browser/check_constraint/obj/')) ] def setUp(self): diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_put.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_put.py index 6192ae5a..e016dc60 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_put.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/check_constraint/tests/test_check_constraint_put.py @@ -26,7 +26,7 @@ class CheckConstraintPutTestCase(BaseTestGenerator): """This class will update check constraint to existing table""" scenarios = [ ('Update check constraint to table', - dict(url='/browser/check_constraints/obj/')) + dict(url='/browser/check_constraint/obj/')) ] def setUp(self): diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/__init__.py index b8685030..24ec064b 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/__init__.py @@ -93,6 +93,14 @@ class ExclusionConstraintModule(ConstraintTypeModule): """ return database.DatabaseModule.NODE_TYPE + @property + def module_use_template_javascript(self): + """ + Returns whether Jinja2 template is used for generating the javascript + module. + """ + return False + blueprint = ExclusionConstraintModule(__name__) diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.js new file mode 100644 index 00000000..065b6096 --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/exclusion_constraint/static/js/exclusion_constraint.js @@ -0,0 +1,951 @@ +define('pgadmin.node.exclusion_constraint', [ + 'sources/gettext', 'sources/url_for', 'jquery', 'underscore', + 'underscore.string', 'pgadmin', 'pgadmin.browser', 'alertify', + 'pgadmin.browser.collection' +], function(gettext, url_for, $, _, S, pgAdmin, pgBrowser, Alertify) { + + var ExclusionConstraintColumnModel = pgBrowser.Node.Model.extend({ + defaults: { + column: undefined, + oper_class: undefined, + order: false, + nulls_order: false, + operator:undefined, + col_type:undefined, + is_sort_nulls_applicable: true + }, + toJSON: function () { + var d = pgBrowser.Node.Model.prototype.toJSON.apply(this, arguments); + delete d.col_type; + return d; + }, + schema: [{ + id: 'column', label: gettext('Column'), type:'text', editable: false, + cell:'string' + },{ + id: 'oper_class', label: gettext('Operator class'), type:'text', + node: 'table', url: 'get_oper_class', first_empty: true, + editable: function(m) { + if (m instanceof Backbone.Collection) { + return true; + } else if ((_.has(m.collection, 'handler') && + !_.isUndefined(m.collection.handler) && + !_.isUndefined(m.collection.handler.get('oid')))) { + return false; + } else if (_.has(m.collection, 'handler') && + !_.isUndefined(m.collection.handler) && + !_.isUndefined(m.collection.handler.get('amname')) && + m.collection.handler.get('amname') != 'btree') { + // Disable if access method is not btree + return false; + } + return true; + }, + select2: { + allowClear: true, width: 'style', tags: true, + placeholder: gettext('Select the operator class') + }, cell: Backgrid.Extension.Select2Cell.extend({ + initialize: function () { + Backgrid.Extension.Select2Cell.prototype.initialize.apply(this, arguments); + + var self = this, + url = self.column.get('url') || self.defaults.url, + m = self.model, + indextype = self.model.collection.handler.get('amname'); + + if (url && (indextype == 'btree' || _.isUndefined(indextype) || + _.isNull(indextype) || indextype == '')) { + // Set sort_order and nulls to true if access method is btree + setTimeout(function() { + m.set('order', true); + m.set('nulls_order', true); + }, 10); + + var node = this.column.get('schema_node'), + eventHandler = m.top || m, + node_info = this.column.get('node_info'), + full_url = node.generate_url.apply( + node, [ + null, url, this.column.get('node_data'), + this.column.get('url_with_id') || false, node_info + ]), + data = []; + + indextype = 'btree'; + + if (this.column.get('version_compatible')) { + eventHandler.trigger('pgadmin:view:fetching', m, self.column); + $.ajax({ + async: false, + data : {indextype:indextype}, + url: full_url, + success: function(res) { + data = res.data; + self.column.set('options', data); + }, + error: function() { + eventHandler.trigger('pgadmin:view:fetch:error', m, self.column); + } + }); + eventHandler.trigger('pgadmin:view:fetched', m, self.column); + } + } else { + self.column.set('options', []); + } + } + }) + },{ + id: 'order', label: gettext('DESC'), type: 'switch', + options: { + onText: 'ASC', + offText: 'DESC', + }, + editable: function(m) { + if (m instanceof Backbone.Collection) { + return true; + } else if ((_.has(m.collection, 'handler') && + !_.isUndefined(m.collection.handler) && + !_.isUndefined(m.collection.handler.get('oid')))) { + return false; + } else if (m.top.get('amname') === 'btree') { + m.set('is_sort_nulls_applicable', true); + return true; + } else { + m.set('is_sort_nulls_applicable', false); + return false; + } + } + },{ + id: 'nulls_order', label: gettext('NULLs order'), type:"switch", + options: { + onText: 'FIRST', + offText: 'LAST', + }, + editable: function(m) { + if (m instanceof Backbone.Collection) { + return true; + } else if ((_.has(m.collection, 'handler') && + !_.isUndefined(m.collection.handler) && + !_.isUndefined(m.collection.handler.get('oid')))) { + return false; + } else if (m.top.get('amname') === 'btree') { + m.set('is_sort_nulls_applicable', true); + return true; + } else { + m.set('is_sort_nulls_applicable', false); + return false; + } + } + },{ + id: 'operator', label: gettext('Operator'), type: 'text', + node: 'table', url: 'get_operator', + editable: function(m) { + if (m instanceof Backbone.Collection) { + return true; + } + if ((_.has(m.collection, 'handler') && + !_.isUndefined(m.collection.handler) && + !_.isUndefined(m.collection.handler.get('oid')))) { + return false; + } + return true; + }, + select2: { + allowClear: false, width: 'style', + }, cell: Backgrid.Extension.Select2Cell.extend({ + initialize: function () { + Backgrid.Extension.Select2Cell.prototype.initialize.apply(this, arguments); + + var self = this, + url = self.column.get('url') || self.defaults.url, + m = self.model, + col_type = self.model.get('col_type'); + + self.column.set('options', []); + + if (url && !_.isUndefined(col_type) && !_.isNull(col_type) && col_type != '') { + var node = this.column.get('schema_node'), + eventHandler = m.top || m, + node_info = this.column.get('node_info'), + full_url = node.generate_url.apply( + node, [ + null, url, this.column.get('node_data'), + this.column.get('url_with_id') || false, node_info + ]), + data = []; + + if (this.column.get('version_compatible')) { + eventHandler.trigger('pgadmin:view:fetching', m, self.column); + $.ajax({ + async: false, + data : {col_type:col_type}, + url: full_url, + success: function(res) { + data = res.data; + self.column.set('options', data); + }, + error: function() { + eventHandler.trigger('pgadmin:view:fetch:error', m, self.column); + } + }); + eventHandler.trigger('pgadmin:view:fetched', m, self.column); + } + } + } + }) + } + ], + validate: function() { + this.errorModel.clear(); + var operator = this.get('operator'), + column_name = this.get('column'); + if (_.isUndefined(operator) || _.isNull(operator)) { + var msg = gettext('Please specify operator for column: ') + column_name; + this.errorModel.set('operator', msg); + return msg; + } + return null; + } + }); + + var ExclusionConstraintColumnControl = Backform.ExclusionConstraintColumnControl = + Backform.UniqueColCollectionControl.extend({ + + initialize: function(opts) { + Backform.UniqueColCollectionControl.prototype.initialize.apply( + this, arguments + ); + + var self = this, + node = 'exclusion_constraint', + headerSchema = [{ + id: 'column', label:'', type:'text', + node: 'column', control: Backform.NodeListByNameControl.extend({ + initialize: function() { + // Here we will decide if we need to call URL + // Or fetch the data from parent columns collection + if(self.model.handler) { + Backform.Select2Control.prototype.initialize.apply(this, arguments); + // Do not listen for any event(s) for existing constraint. + if (_.isUndefined(self.model.get('oid'))) { + var tableCols = self.model.top.get('columns'); + this.listenTo(tableCols, 'remove' , this.removeColumn); + this.listenTo(tableCols, 'change:name', this.resetColOptions); + this.listenTo(tableCols, 'change:cltype', this.resetColOptions); + } + this.custom_options(); + } else { + Backform.NodeListByNameControl.prototype.initialize.apply(this, arguments); + } + }, + removeColumn: function (m) { + var that = this; + setTimeout(function () { + that.custom_options(); + that.render.apply(that); + }, 50); + }, + resetColOptions: function(m) { + var that = this; + + if (m.previous('name') == self.headerData.get('column')) { + /* + * Table column name has changed so update + * column name in exclusion constraint as well. + */ + self.headerData.set( + {"column": m.get('name')}); + self.headerDataChanged(); + } + + setTimeout(function () { + that.custom_options(); + that.render.apply(that); + }, 50); + }, + custom_options: function() { + // We will add all the columns entered by user in table model + var columns = self.model.top.get('columns'), + added_columns_from_tables = [], + col_types = []; + + if (columns.length > 0) { + _.each(columns.models, function(m) { + var col = m.get('name'); + if(!_.isUndefined(col) && !_.isNull(col)) { + added_columns_from_tables.push({ + label: col, value: col, image:'icon-column' + }); + col_types.push({name:col, type:m.get('cltype')}); + } + }); + } + // Set the values in to options so that user can select + this.field.set('options', added_columns_from_tables); + self.field.set('col_types', col_types); + }, + remove: function () { + if(self.model.handler) { + var tableCols = self.model.top.get('columns'); + this.stopListening(tableCols, 'remove' , this.removeColumn); + this.stopListening(tableCols, 'change:name' , this.resetColOptions); + this.stopListening(tableCols, 'change:cltype' , this.resetColOptions); + + Backform.Select2Control.prototype.remove.apply(this, arguments); + + } else { + Backform.NodeListByNameControl.prototype.remove.apply(this, arguments); + } + }, + template: _.template([ + '
' + + '' + fm_filename + + '
'; + } + if (props.Width && props.Width != '') { + result += ''; + } + if (props.Size && props.Size != '') { + result += ''; + } + if (props['Date Created'] && props['Date Created'] != '') { + result += ''; + } + if (props['Date Modified'] && props['Date Modified'] != '') { + result += ''; + } + result += '| '; + result += '' + lg.name +' | '; + result += '' + lg.size + ' | '; + result += '' + lg.modified + ' | ||
|---|---|---|---|---|
| '; + if ((data[key]).Protected == 1) { + result += + ''; + } + if (!has_capability(data[key], 'rename')) { + result += '' + + fm_filename + ' | '; + } else { + result += '' + + props.Size + ' | '; + } else { + result += ''; + } + + if (props['Date Modified'] && props['Date Modified'] != '') { + result += ' | ' + props['Date Modified'] + ' | '; + } else { + result += ''; + } + + result += ' |
| ' + + lg.name + ' | ' + lg.size + + ' | ' + lg.modified + + ' |
|---|---|---|
| " +
+ " " + + lg.new_folder + " " + + " | " + + " | " + + " |