Add "Reset" button to reset back to default search_path
authorKarl O. Pinc <kop@karlpinc.com>
Sun, 15 Sep 2024 23:04:27 +0000 (18:04 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Sun, 15 Sep 2024 23:04:27 +0000 (18:04 -0500)
src/pgwui_sql/pgwui_sql.py
src/pgwui_sql/templates/sql_edit.mak
src/pgwui_sql/views/search_path.py [new file with mode: 0644]
tests/test_pgwui_sql.py

index 9e3f1e750e4f04836a15f8d654dfc686dec3c343..bee5905d08d5b7ea7da54a064e38ec6ac2a5ab1c 100644 (file)
@@ -48,4 +48,5 @@ def includeme(config):
         cache_max_age=3600)
     config.add_route(PGWUI_COMPONENT, DEFAULT_SQL_ROUTE)
     config.add_route('pgwui_sql_edit', '/sql_edit')
+    config.add_route('pgwui_sql_db_search_path', '/sql_db_search_path')
     config.scan()
index 02963f419dd7982b326f8100732700ec7eebab06..c394398e7a346a77e57009cce3f2b1f72a1fa4b9 100644 (file)
 <%block name="top_hr">
 </%block>
 
+<script>
+  function show_search_path() {
+    fetch('${request.route_path("pgwui_sql_db_search_path")|n}')
+      .then((response) => {
+            if (!response.ok) {
+              window.alert('Problem obtaining the search_path');
+            }
+            return response.text();
+            })
+      .then((search_path) => {
+            const sp_elemt = document.getElementById('search_path_id');
+            sp_elemt.value = search_path;
+            })
+      .catch((error) => {
+          window.alert('Problem showing the search_path: ' + error);
+          })
+  };
+</script>
+
 <%def name="sql_row(tab_index)">
       <tr>
         <%self.lib:td_label for_id="search_path_id">
                  value="${search_path}"
                  />
         </%self.lib:td_input>
+        <td>
+          <button type="button" tabindex="${tab_index.val}"
+                  onclick="show_search_path();"
+          >Reset</button>
+          <% tab_index.inc() %>
+        </td>
       </tr>
       <tr>
-        <%self.lib:td_input tab_index="${tab_index}" colspan="2">
+        <%self.lib:td_input tab_index="${tab_index}" colspan="3">
           <textarea name="sql"
                     class="sqltext"
                     tabindex="${tab_index.val}"
diff --git a/src/pgwui_sql/views/search_path.py b/src/pgwui_sql/views/search_path.py
new file mode 100644 (file)
index 0000000..303a508
--- /dev/null
@@ -0,0 +1,72 @@
+# Copyright (C) 2024 The Meme Factory, Inc. http://www.karlpinc.com/
+
+# This file is part of PGWUI_SQL.
+#
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU Affero General Public License
+# as published by the Free Software Foundation, either version 3 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with this program.  If not, see
+# <http://www.gnu.org/licenses/>.
+#
+
+from pyramid.view import view_config
+import logging
+import pgwui_core.core
+import pgwui_core.forms
+import pgwui_sql.views.base
+import pgwui_sql.views.search_path_base
+
+log = logging.getLogger(__name__)
+
+
+@view_config(route_name='pgwui_sql_db_search_path',
+             renderer='string')
+def sql_edit_view(request):
+    # This can use the UnsafeUploadEngine because it does not alter
+    # server state.  We are not getting any form data from the user.
+    # We don't have access to the CSRF token because the page is not
+    # sent a POST request.
+
+    uh = pgwui_sql.views.search_path_base.SQLEditHandler(request).init()
+    response, errors = pgwui_core.core.UnsafeUploadEngine(uh).run()
+    if errors:
+        log.warning(f'Failed to get search_path: ({errors})')
+
+    if uh.data is None:
+        log.debug('search_path requested when user not logged in: DB {db}:'
+                  # ' File ({filename}):'
+                  # ' Format {format}:'
+                  ' By user {user}:'
+                  .format(  # filename=response['filename'],
+                      # format=download_fmt,
+                      db=response['db'],
+                      user=response['user']))
+        search_path = ''
+    else:
+        # The hander executed; the user is logged in
+        search_path = uh.data.stmts[0].result
+
+        # if pgwui_core.utils.is_checked(response['csv_checked']):
+        #     download_fmt = 'CSV'
+        # else:
+        #     download_fmt = 'TAB'
+        log.debug('Successful sql editor request: DB {db}:'
+                  # ' File ({filename}):'
+                  # ' Format {format}:'
+                  ' By user {user}:'
+                  ' search_path ({search_path})'
+                  .format(  # filename=response['filename'],
+                      # format=download_fmt,
+                      db=response['db'],
+                      user=response['user'],
+                      search_path=search_path))
+
+    return search_path
index e3f0360ecd9839072f3d0d067dd30688c242de80..604a510fc9cae126b0bf5be34955b25d63df76bc 100644 (file)
@@ -100,5 +100,5 @@ def test_includeme(mock_establish_settings, mock_add_static_view,
 
         mock_establish_settings.assert_called_once()
         mocked_add_static_view.assert_called_once()
-        assert mocked_add_route.call_count == 2
+        assert mocked_add_route.call_count == 3
         mocked_scan.assert_called_once()