Subject: patch for RM1177 [pgadmin4]

From: Harshal Dhumal <[email-masked]>
Date: Fri, 5 Aug 2016 20:55:03 +0530

Hi,

PFA patch for RM1177.

Issue fixed: Added a check to warn the user if the binary path is not set
before running external commands.

-- 
*Harshal Dhumal*
*Software Engineer*

EnterpriseDB India: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
diff --git a/web/pgadmin/browser/templates/browser/js/browser.js b/web/pgadmin/browser/templates/browser/js/browser.js
index 5ed582a..95528b0 100644
--- a/web/pgadmin/browser/templates/browser/js/browser.js
+++ b/web/pgadmin/browser/templates/browser/js/browser.js
@@ -692,7 +692,24 @@ function(require, $, _, S, Bootstrap, pgAdmin, alertify, CodeMirror) {
         pnlDialogHelp.focus();
         iframe.openURL(url);
       }
+    },
+
+    get_preference(module, preference_name) {
+      preference = null;
+      $.ajax({
+        async: false,
+        url: "{{ url_for('preferences.preferences') }}" +"/"+ module +"/"+ preference_name,
+        success: function(res) {
+          preference = res;
+        },
+        error: function(xhr, status, error) {
+
+        }
+      });
+
+      return preference;
     }
+
   });
 
   window.onbeforeunload = function(ev) {
diff --git a/web/pgadmin/preferences/__init__.py b/web/pgadmin/preferences/__init__.py
index 944542d..b282e43 100644
--- a/web/pgadmin/preferences/__init__.py
+++ b/web/pgadmin/preferences/__init__.py
@@ -82,18 +82,37 @@ def script():

 @blueprint.route("/preferences", methods=["GET"])
[email-masked]("/preferences/<module>/<preference>")
 @login_required
-def preferences():
-    """Fetch all the preferences of pgAdmin IV."""
+def preferences(module=None, preference=None):
+    """Fetch all/or requested preferences of pgAdmin IV."""
+
+    if module is not None and preference is not None:
+        try:
+            m = Preferences.module(module, create=False)
+            if m is None:
+                return Response(status=404)
+
+            p = m.preference(preference)
+            if p is None:
+                return Response(status=404)
+
+            return ajax_response(
+                response=p.to_json(),
+                status=200
+            )
+
+        except Exception as e:
+            return internal_server_error(errormsg=str(e))
 
     # Load Preferences
-    preferences = Preferences.preferences()
+    pref = Preferences.preferences()
     res = []
 
     def label(p):
         return p['label']
 
-    for m in preferences:
+    for m in pref:
         if len(m['categories']):
             om = {
                 "id": m['id'],
diff --git a/web/pgadmin/tools/backup/templates/backup/js/backup.js b/web/pgadmin/tools/backup/templates/backup/js/backup.js
index 9b4f679..5d4865a 100644
--- a/web/pgadmin/tools/backup/templates/backup/js/backup.js
+++ b/web/pgadmin/tools/backup/templates/backup/js/backup.js
@@ -361,6 +361,48 @@ TODO LIST FOR BACKUP:
 
       // Callback to draw Backup Dialog for globals/server
       start_backup_global_server: function(action, item, params) {
+        var i = item || pgBrowser.tree.selected(),
+          server_data = null;
+
+        while (i) {
+          var node_data = pgBrowser.tree.itemData(i);
+          if (node_data._type == 'server') {
+            server_data = node_data;
+            break;
+          }
+
+          if (pgBrowser.tree.hasParent(i)) {
+            i = $(pgBrowser.tree.parent(i));
+          } else {
+            alertify.alert("{{ _("Please select server or child node from tree.") }}");
+            break;
+          }
+        }
+
+        if (!server_data) {
+          return;
+        }
+
+        var module = 'paths',
+          preference_name = 'pg_bin_dir',
+          msg = '{{ _('Please set binary path for PostgreSQL Server from preferences.') }}';
+
+        if (server_data.server_type == 'ppas') {
+          preference_name = 'ppas_bin_dir';
+          msg = '{{ _('Please set binary path for EDB Advanced Server from preferences.') }}';
+        }
+
+        var preference = pgBrowser.get_preference(module, preference_name);
+
+        if(preference) {
+          if (!preference.value) {
+            alertify.alert(msg);
+            return;
+          }
+        } else {
+          alertify.alert(S('{{ _('Failed to load preference %s of module %s') }}').sprintf(preference_name, module).value());
+          return;
+        }
 
         var of_type = undefined;
 
@@ -533,6 +575,50 @@ TODO LIST FOR BACKUP:
 
       // Callback to draw Backup Dialog for objects
       backup_objects: function(action, treeItem) {
+
+        var i = treeItem || pgBrowser.tree.selected(),
+          server_data = null;
+
+        while (i) {
+          var node_data = pgBrowser.tree.itemData(i);
+          if (node_data._type == 'server') {
+            server_data = node_data;
+            break;
+          }
+
+          if (pgBrowser.tree.hasParent(i)) {
+            i = $(pgBrowser.tree.parent(i));
+          } else {
+            alertify.alert("{{ _("Please select server or child node from tree.") }}");
+            break;
+          }
+        }
+
+        if (!server_data) {
+          return;
+        }
+
+        var module = 'paths',
+          preference_name = 'pg_bin_dir',
+          msg = '{{ _('Please set binary path for PostgreSQL Server from preferences.') }}';
+
+        if (server_data.server_type == 'ppas') {
+          preference_name = 'ppas_bin_dir';
+          msg = '{{ _('Please set binary path for EDB Advanced Server from preferences.') }}';
+        }
+
+        var preference = pgBrowser.get_preference(module, preference_name);
+
+        if(preference) {
+          if (!preference.value) {
+            alertify.alert(msg);
+            return;
+          }
+        } else {
+          alertify.alert(S('{{ _('Failed to load preference %s of module %s') }}').sprintf(preference_name, module).value());
+          return;
+        }
+
         var title = S('{{ 'Backup (%s: %s)' }}'),
             tree = pgBrowser.tree,
             item = treeItem || tree.selected(),
diff --git a/web/pgadmin/tools/import_export/templates/import_export/js/import_export.js b/web/pgadmin/tools/import_export/templates/import_export/js/import_export.js
index 1188499..fceb193 100644
--- a/web/pgadmin/tools/import_export/templates/import_export/js/import_export.js
+++ b/web/pgadmin/tools/import_export/templates/import_export/js/import_export.js
@@ -298,6 +298,49 @@ define(
         Open the dialog for the import functionality
       */
       callback_import_export: function(args, item) {
+        var i = item || pgBrowser.tree.selected(),
+          server_data = null;
+
+        while (i) {
+          var node_data = pgBrowser.tree.itemData(i);
+          if (node_data._type == 'server') {
+            server_data = node_data;
+            break;
+          }
+
+          if (pgBrowser.tree.hasParent(i)) {
+            i = $(pgBrowser.tree.parent(i));
+          } else {
+            Alertify.alert("{{ _("Please select server or child node from tree.") }}");
+            break;
+          }
+        }
+
+        if (!server_data) {
+          return;
+        }
+
+        var module = 'paths',
+          preference_name = 'pg_bin_dir',
+          msg = '{{ _('Please set binary path for PostgreSQL Server from preferences.') }}';
+
+        if (server_data.server_type == 'ppas') {
+          preference_name = 'ppas_bin_dir';
+          msg = '{{ _('Please set binary path for EDB Advanced Server from preferences.') }}';
+        }
+
+        var preference = pgBrowser.get_preference(module, preference_name);
+
+        if(preference) {
+          if (!preference.value) {
+            Alertify.alert(msg);
+            return;
+          }
+        } else {
+          Alertify.alert(S('{{ _('Failed to load preference %s of module %s') }}').sprintf(preference_name, module).value());
+          return;
+        }
+
         var self = this;
         var input = args || {},
             t = pgBrowser.tree,
diff --git a/web/pgadmin/tools/maintenance/templates/maintenance/js/maintenance.js b/web/pgadmin/tools/maintenance/templates/maintenance/js/maintenance.js
index fcad538..6eb074e 100644
--- a/web/pgadmin/tools/maintenance/templates/maintenance/js/maintenance.js
+++ b/web/pgadmin/tools/maintenance/templates/maintenance/js/maintenance.js
@@ -187,8 +187,51 @@ define(
         Open the dialog for the maintenance functionality
       */
       callback_maintenace: function(args, item) {
-        var self = this;
-        var input = args || {},
+        var i = item || pgBrowser.tree.selected(),
+          server_data = null;
+
+        while (i) {
+          var node_data = pgBrowser.tree.itemData(i);
+          if (node_data._type == 'server') {
+            server_data = node_data;
+            break;
+          }
+
+          if (pgBrowser.tree.hasParent(i)) {
+            i = $(pgBrowser.tree.parent(i));
+          } else {
+            Alertify.alert("{{ _("Please select server or child node from tree.") }}");
+            break;
+          }
+        }
+
+        if (!server_data) {
+          return;
+        }
+
+        var module = 'paths',
+          preference_name = 'pg_bin_dir',
+          msg = '{{ _('Please set binary path for PostgreSQL Server from preferences.') }}';
+
+        if (server_data.server_type == 'ppas') {
+          preference_name = 'ppas_bin_dir';
+          msg = '{{ _('Please set binary path for EDB Advanced Server from preferences.') }}';
+        }
+
+        var preference = pgBrowser.get_preference(module, preference_name);
+
+        if(preference) {
+          if (!preference.value) {
+            Alertify.alert(msg);
+            return;
+          }
+        } else {
+          Alertify.alert(S('{{ _('Failed to load preference %s of module %s') }}').sprintf(preference_name, module).value());
+          return;
+        }
+
+        var self = this,
+          input = args || {},
           t = pgBrowser.tree,
           i = item || t.selected(),
           d = i && i.length == 1 ? t.itemData(i) : undefined,
diff --git a/web/pgadmin/tools/restore/templates/restore/js/restore.js b/web/pgadmin/tools/restore/templates/restore/js/restore.js
index ef731c6..d19cc18 100644
--- a/web/pgadmin/tools/restore/templates/restore/js/restore.js
+++ b/web/pgadmin/tools/restore/templates/restore/js/restore.js
@@ -286,6 +286,50 @@ define([
       },
       // Callback to draw Backup Dialog for objects
       restore_objects: function(action, treeItem) {
+
+        var i = treeItem || pgBrowser.tree.selected(),
+          server_data = null;
+
+        while (i) {
+          var node_data = pgBrowser.tree.itemData(i);
+          if (node_data._type == 'server') {
+            server_data = node_data;
+            break;
+          }
+
+          if (pgBrowser.tree.hasParent(i)) {
+            i = $(pgBrowser.tree.parent(i));
+          } else {
+            alertify.alert("{{ _("Please select server or child node from tree.") }}");
+            break;
+          }
+        }
+
+        if (!server_data) {
+          return;
+        }
+
+        var module = 'paths',
+          preference_name = 'pg_bin_dir',
+          msg = '{{ _('Please set binary path for PostgreSQL Server from preferences.') }}';
+
+        if (server_data.server_type == 'ppas') {
+          preference_name = 'ppas_bin_dir';
+          msg = '{{ _('Please set binary path for EDB Advanced Server from preferences.') }}';
+        }
+
+        var preference = pgBrowser.get_preference(module, preference_name);
+
+        if(preference) {
+          if (!preference.value) {
+            alertify.alert(msg);
+            return;
+          }
+        } else {
+          alertify.alert(S('{{ _('Failed to load preference %s of module %s') }}').sprintf(preference_name, module).value());
+          return;
+        }
+
         var title = S('{{ 'Restore (%s: %s)' }}'),
             tree = pgBrowser.tree,
             item = treeItem || tree.selected(),
diff --git a/web/pgadmin/utils/preferences.py b/web/pgadmin/utils/preferences.py
index 5d21d20..58e6eb5 100644
--- a/web/pgadmin/utils/preferences.py
+++ b/web/pgadmin/utils/preferences.py
@@ -394,8 +394,7 @@ class Preferences(object):
             if name in cat['preferences']:
                 return (cat['preferences'])[name]
 
-        assert False, """Couldn't find the preference in this preference!
-Did you forget to register it?"""
+        return None
 
     @classmethod
     def preferences(cls):
@@ -452,12 +451,13 @@ Did you forget to register it?"""
         )
 
     @classmethod
-    def module(cls, name):
+    def module(cls, name, create=True):
         """
         module (classmethod)
         Get the module preferences object
 
         :param name: Name of the module
+        :param create: Flag to create Preferences object
         :returns: a Preferences object representing for the module.
         """
         if name in Preferences.modules:
@@ -466,10 +466,11 @@ Did you forget to register it?"""
             if m.label is None:
                 m.label = name
             return m
-        else:
-            m = Preferences(name, None)
 
-        return m
+        if create:
+            return Preferences(name, None)
+
+        return None
 
     @classmethod
     def save(cls, mid, cid, pid, value):

-- 
Sent via pgadmin-hackers mailing list ([email-masked])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

---

From: Dave Page <[email-masked]>
Date: Mon, 8 Aug 2016 11:59:54 +0100

Thanks, applied with some minor wording tweaks.

On Fri, Aug 5, 2016 at 4:25 PM, Harshal Dhumal
<[email-masked]> wrote:
> Harshal Dhumal
> Software Engineer

-- 
Dave Page
Blog: http://pgsnake.blogspot.com
Twitter: @pgsnake

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company