public inbox for [email protected]  
help / color / mirror / Atom feed
From: Pradip Parkale <[email protected]>
To: pgadmin-hackers <[email protected]>
Subject: [pgAdmin][RM6794]: Provide functionality to disable/enable rules
Date: Mon, 11 Oct 2021 19:37:09 +0530
Message-ID: <CAJ9T6Ssr2N-4M+_Xmd0Tv2=GoOx0B7zuLPupAqivzBKSKuSNPw@mail.gmail.com> (raw)

Hi Hackers,

Please find the updated patch for #6794 Provide functionality to
disable/enable rules.

This patch also includes the fix for #6870 Unable to delete server group if
first server group has shared server.

-- 
Thanks & Regards,
Pradip Parkale
Software Engineer | EnterpriseDB Corporation


Attachments:

  [application/octet-stream] RM6794.patch (12.4K, 3-RM6794.patch)
  download | inline diff:
diff --git a/web/pgadmin/browser/server_groups/__init__.py b/web/pgadmin/browser/server_groups/__init__.py
index 561772636..1cedc9c5d 100644
--- a/web/pgadmin/browser/server_groups/__init__.py
+++ b/web/pgadmin/browser/server_groups/__init__.py
@@ -177,7 +177,7 @@ class ServerGroupView(NodeView):
         # if server group id is 1 we won't delete it.
         sg = groups.first()
 
-        shared_servers = Server.query.filter_by(servergroup_id=sg.id,
+        shared_servers = Server.query.filter_by(servergroup_id=gid,
                                                 shared=True).all()
         if shared_servers:
             return make_json_response(
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/__init__.py
index 75a91c6b3..4c078043d 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/__init__.py
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/__init__.py
@@ -249,7 +249,7 @@ class RuleView(PGChildNodeView, SchemaDiffObjectCompare):
             rset['rows'][0]['oid'],
             tid,
             rset['rows'][0]['name'],
-            icon="icon-rule"
+            icon="icon-rule" if rset['rows'][0]['enabled'] else "icon-rule-bad"
         )
 
         return make_json_response(
@@ -276,7 +276,7 @@ class RuleView(PGChildNodeView, SchemaDiffObjectCompare):
                     row['oid'],
                     tid,
                     row['name'],
-                    icon="icon-rule"
+                    icon="icon-rule" if row['enabled'] else "icon-rule-bad"
                 ))
 
         return make_json_response(
@@ -385,7 +385,9 @@ class RuleView(PGChildNodeView, SchemaDiffObjectCompare):
                     rid,
                     tid,
                     name,
-                    icon="icon-%s" % self.node_type
+                    icon="icon-%s" % self.node_type if 'enabled' in data and
+                                                       data['enabled']
+                    else "icon-%s-bad" % self.node_type
                 )
             )
         except Exception as e:
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/img/rule-bad.svg b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/img/rule-bad.svg
new file mode 100644
index 000000000..a1b4aef87
--- /dev/null
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/img/rule-bad.svg
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 25.4.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 viewBox="0 0 16 16" style="enable-background:new 0 0 16 16;" xml:space="preserve">
+<style type="text/css">
+	.st0{fill:#FEE07B;stroke:#DDA91D;stroke-width:0.75;stroke-linecap:round;stroke-linejoin:round;}
+	.st1{fill:none;stroke:#DDA91D;stroke-width:0.75;stroke-linecap:round;stroke-linejoin:round;}
+	.st2{fill:#D0021B;}
+</style>
+<g>
+	<g id="_8">
+		<rect x="2.4" y="5" class="st0" width="11.1" height="6"/>
+		<line class="st1" x1="10.9" y1="5.4" x2="10.9" y2="8.9"/>
+		<line class="st1" x1="6.9" y1="5.4" x2="6.9" y2="8.9"/>
+		<line class="st1" x1="8.9" y1="5.4" x2="8.9" y2="7.9"/>
+		<line class="st1" x1="4.9" y1="5.4" x2="4.9" y2="7.9"/>
+	</g>
+	<path class="st2" d="M14,5.1l1-1c0.3-0.3,0.3-0.8,0-1.1s-0.8-0.3-1.1,0l-1,1l-1-1c-0.3-0.3-0.8-0.3-1.1,0s-0.3,0.8,0,1.1l1,1l-1,1
+		c-0.3,0.3-0.3,0.8,0,1.1c0.1,0.1,0.3,0.2,0.5,0.2s0.6-0.1,0.7-0.3l1-1l1,1c0.1,0.1,0.3,0.2,0.5,0.2s0.4,0,0.5-0.2
+		c0.3-0.3,0.3-0.8,0-1.1L14,5.1z"/>
+</g>
+</svg>
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/js/rule.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/js/rule.js
index 48f055b41..db29fedbf 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/js/rule.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/js/rule.js
@@ -12,8 +12,8 @@ import RuleSchema from './rule.ui';
 define('pgadmin.node.rule', [
   'sources/gettext', 'sources/url_for', 'jquery', 'underscore',
   'sources/pgadmin', 'pgadmin.browser', 'pgadmin.backform',
-  'pgadmin.node.schema.dir/schema_child_tree_node',
-], function(gettext, url_for, $, _, pgAdmin, pgBrowser, Backform, SchemaChildTreeNode) {
+  'pgadmin.node.schema.dir/schema_child_tree_node', 'pgadmin.alertifyjs',
+], function(gettext, url_for, $, _, pgAdmin, pgBrowser, Backform, SchemaChildTreeNode, alertify) {
 
   /**
     Create and add a rule collection into nodes
@@ -112,8 +112,93 @@ define('pgadmin.node.rule', [
           icon: 'wcTabIcon icon-rule', data: {action: 'create', check: true},
           enable: 'canCreate',
         },
+        {
+          name: 'enable_rule', node: 'rule', module: this,
+          applies: ['object', 'context'], callback: 'enable_rule',
+          category: 'connect', priority: 3, label: gettext('Enable rule'),
+          icon: 'fa fa-check', enable: 'canCreate_with_rule_enable',
+        },{
+          name: 'disable_rule', node: 'rule', module: this,
+          applies: ['object', 'context'], callback: 'disable_rule',
+          category: 'drop', priority: 3, label: gettext('Disable rule'),
+          icon: 'fa fa-times', enable: 'canCreate_with_rule_disable'
+        }
         ]);
       },
+      callbacks: {
+        /* Enable rule */
+        enable_rule: function(args) {
+          var input = args || {},
+            obj = this,
+            t = pgBrowser.tree,
+            i = input.item || t.selected(),
+            d = i  ? t.itemData(i) : undefined;
+
+          if (!d)
+            return false;
+
+          var data = d;
+          $.ajax({
+            url: obj.generate_url(i, 'obj' , d, true),
+            type:'PUT',
+            data: {'enabled' : 'true'},
+            dataType: 'json',
+          })
+            .done(function() {
+              alertify.success('Rule updated.');
+              t.removeIcon(i);
+              data.icon = 'icon-rule';
+              t.addIcon(i, {icon: data.icon});
+              t.unload(i);
+              t.setInode(false);
+              t.deselect(i);
+              // Fetch updated data from server
+              setTimeout(function() {
+                t.select(i);
+              }, 10);
+            })
+            .fail(function(xhr, status, error) {
+              alertify.pgRespErrorNotify(xhr, error);
+              t.unload(i);
+            });
+        },
+        /* Disable rule */
+        disable_rule: function(args) {
+          var input = args || {},
+            obj = this,
+            t = pgBrowser.tree,
+            i = input.item || t.selected(),
+            d = i  ? t.itemData(i) : undefined;
+
+          if (!d)
+            return false;
+
+          var data = d;
+          $.ajax({
+            url: obj.generate_url(i, 'obj' , d, true),
+            type:'PUT',
+            data: {'enabled' : 'false'},
+            dataType: 'json',
+          })
+            .done(function() {
+              alertify.success('Rule updated');
+              t.removeIcon(i);
+              data.icon = 'icon-rule-bad';
+              t.addIcon(i, {icon: data.icon});
+              t.unload(i);
+              t.setInode(false);
+              t.deselect(i);
+              // Fetch updated data from server
+              setTimeout(function() {
+                t.select(i);
+              }, 10);
+            })
+            .fail(function(xhr, status, error) {
+              alertify.pgRespErrorNotify(xhr, error, gettext('Disable rule failed'));
+              t.unload(i);
+            });
+        },
+      },
       getSchema: function(treeNodeInfo, itemNodeData) {
         return new RuleSchema(
           {
@@ -216,6 +301,26 @@ define('pgadmin.node.rule', [
 
       },
 
+      canCreate_with_rule_enable: function(itemData, item, data) {
+        var treeData = pgBrowser.tree.getTreeNodeHierarchy(item);
+        if ('view' in treeData) {
+          return false;
+        }
+
+        return itemData.icon === 'icon-rule-bad' &&
+          this.canCreate.apply(this,[itemData, item, data]);
+      },
+      // Check to whether rule is enable ?
+      canCreate_with_rule_disable: function(itemData, item, data) {
+        var treeData = pgBrowser.tree.getTreeNodeHierarchy(item);
+        if ('view' in treeData) {
+          return false;
+        }
+
+        return itemData.icon === 'icon-rule' &&
+          this.canCreate.apply(itemData, item, data);
+      },
+
     });
   }
 
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/js/rule.ui.js b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/js/rule.ui.js
index 777de8b17..80a33778f 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/js/rule.ui.js
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/static/js/rule.ui.js
@@ -66,6 +66,11 @@ export default class RuleSchema extends BaseUISchema {
           state.view = obj.fieldOptions.nodeData.label;
         },
       },
+      {
+        id: 'enabled', label: gettext('Enabled?'),
+        type: 'switch', mode: ['properties', 'edit'], 
+        group: gettext('Definition'),
+      },
       {
         id: 'event', label: gettext('Event'), control: 'select2',
         group: gettext('Definition'), type: 'select',
@@ -94,10 +99,6 @@ export default class RuleSchema extends BaseUISchema {
         id: 'system_rule', label: gettext('System rule?'),
         type: 'switch', mode: ['properties'],
       },
-      {
-        id: 'enabled', label: gettext('Enabled?'),
-        type: 'switch', mode: ['properties'],
-      },
       {
         id: 'comment', label: gettext('Comment'), cell: 'text', type: 'multiline',
       },
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/templates/rules/css/rule.css b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/templates/rules/css/rule.css
index 0d7339cf8..63f4ecbce 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/templates/rules/css/rule.css
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/rules/templates/rules/css/rule.css
@@ -7,3 +7,14 @@
   vertical-align: middle;
   height: 1.3em;
 }
+
+
+.icon-rule-bad{
+  background-image: url('{{ url_for('NODE-rule.static', filename='img/rule-bad.svg') }}') !important;
+  border-radius: 10px;
+  background-size: 20px !important;
+  background-repeat: no-repeat;
+  align-content: center;
+  vertical-align: middle;
+  height: 1.3em;
+}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/rules/sql/nodes.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/rules/sql/nodes.sql
index 67ddae002..2019197a1 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/rules/sql/nodes.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/rules/sql/nodes.sql
@@ -1,6 +1,7 @@
 SELECT
     rw.oid AS oid,
-    rw.rulename AS name
+    rw.rulename AS name,
+    CASE WHEN rw.ev_enabled != 'D' THEN True ELSE False END AS enabled
 FROM
     pg_catalog.pg_rewrite rw
 WHERE
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/rules/sql/update.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/rules/sql/update.sql
index e033af7b8..81c961d8f 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/rules/sql/update.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/rules/sql/update.sql
@@ -35,3 +35,7 @@ CREATE OR REPLACE RULE {{ conn|qtIdent(rule_name) }} AS
 {% set old_comment = o_data.comment|default('', true) %}
 {% if (data.comment is defined and (data.comment != old_comment)) %}
 COMMENT ON RULE {{ conn|qtIdent(rule_name) }} ON {{ conn|qtIdent(o_data.schema, o_data.view) }} IS {{ data.comment|qtLiteral }};{% endif %}
+
+{% if data.enabled is defined and o_data.enabled != data.enabled %}
+ALTER TABLE {{ conn|qtIdent(o_data.schema, o_data.view) }} {% if (data.enabled in  ['false', False]) %}DISABLE{% endif %}{% if (data.enabled in ['true', True]) %}ENABLE{% endif %} RULE {{ conn|qtIdent(o_data.name) }};
+{% endif %}


view thread (4+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: [pgAdmin][RM6794]: Provide functionality to disable/enable rules
  In-Reply-To: <CAJ9T6Ssr2N-4M+_Xmd0Tv2=GoOx0B7zuLPupAqivzBKSKuSNPw@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox