Beginnings of a SQL edit window
authorKarl O. Pinc <kop@karlpinc.com>
Tue, 3 Sep 2024 19:17:53 +0000 (14:17 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Tue, 3 Sep 2024 19:17:53 +0000 (14:17 -0500)
src/pgwui_sql/pgwui_sql.py
src/pgwui_sql/templates/sql.mak
src/pgwui_sql/templates/sql_base.mak [new file with mode: 0644]
src/pgwui_sql/templates/sql_edit.mak [new file with mode: 0644]
src/pgwui_sql/views/sql.py
tests/test_pgwui_sql.py

index 640f494b0a524c56f5d96dee16b6ad513b0606f2..9e3f1e750e4f04836a15f8d654dfc686dec3c343 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2018, 2020, 2021 The Meme Factory, Inc.
+# Copyright (C) 2018, 2020, 2021, 2024 The Meme Factory, Inc.
 # http://www.karlpinc.com/
 
 # This file is part of PGWUI_SQL
@@ -47,4 +47,5 @@ def includeme(config):
         f'{PGWUI_COMPONENT}:static/',
         cache_max_age=3600)
     config.add_route(PGWUI_COMPONENT, DEFAULT_SQL_ROUTE)
+    config.add_route('pgwui_sql_edit', '/sql_edit')
     config.scan()
index ec92107404f2958eab53911e5bb236460059bb07..7f1affc93e78e1f62dd838167337cf7d4a6edcad 100644 (file)
@@ -23,6 +23,7 @@
 
   This template uses the following variables in it's context:
    
+    sql          Text of the sql command(s)
     result_rows  List of SQLResult objects
 </%doc>
 
 <%!
     from pgwui_common.path import asset_abspath
 
-    db_base_mak = asset_abspath('pgwui_common:templates/db_base.mak')
+    sql_base_mak = asset_abspath('pgwui_sql:templates/sql_base.mak')
 %>
 
-<%inherit file="${db_base_mak}" />
-
-<%block name="title">${pgwui['pgwui_sql']['menu_label']}</%block>
-<%block name="meta_keywords">
-  <meta name="keywords"
-        content="PGWUI SQL" />
-</%block>
-
-<%block name="meta_description">
-  <meta name="description"
-        content="PostgreSQL Web User Interface, Interactive SQL execution" />
-</%block>
-
-<%block name="stylesheet_links">
-  ${parent.stylesheet_links()}
-  <link rel="stylesheet"
-        href="${request.static_url('pgwui_sql:static/pgwui_sql.css')}"
-        type="text/css" />
-</%block>
+<%inherit file="${sql_base_mak}" />
 
 <%block name="action_success">
   <p><em class="success">Executed SQL without errors</em>,
   Execute SQL
 </%block>
 
-<%def name="sql_row(tab_index)">
-      <tr>
-        <%self.lib:td_label for_id="sql_id">SQL</%self.lib:td_label>
-      </tr>
-      <tr>
-        <%self.lib:td_input tab_index="${tab_index}" colspan="2">
-          <textarea name="sql"
-                    class="sqltext"
-                    tabindex="${tab_index.val}"
-                    id="sql_id">${sql}</textarea>
-        </%self.lib:td_input>
-      </tr>
-</%def>
-
-<%def name="submit(tab_index)">
-  <input value="Execute" tabindex="${tab_index.val}" type="submit" />
-  <% tab_index.inc() %>
-</%def>
-
-<%def name="table_rows(tab_index)">
-  <%parent:table_rows tab_index="${tab_index}" args="tab_index">
-    ## A blank table row for spacing
-    <tr class="verticalgap"><td/><td/></tr>
-    ${sql_row(tab_index)}
-  </%parent:table_rows>
-</%def>
-
 <%def name="render_heading(headings)">
   <% if headings is None:
        return STOP_RENDERING
   </form>
 </%def>
 
+<script>
+  window.name='${self.sql_results_target()}';
+</script>
+
 <% tab_index = self.attr.TabIndex() %>
 % if result_rows:
     ${render_results()}
diff --git a/src/pgwui_sql/templates/sql_base.mak b/src/pgwui_sql/templates/sql_base.mak
new file mode 100644 (file)
index 0000000..a47d896
--- /dev/null
@@ -0,0 +1,105 @@
+<%doc>
+  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/>.
+
+  Template used by sql pages.
+  Karl O. Pinc <kop@karlpinc.com>
+
+  This template uses the following variables in it's context:
+   
+    sql  Text of the sql command(s)
+</%doc>
+
+
+<%!
+    from pgwui_common.path import asset_abspath
+
+    db_base_mak = asset_abspath('pgwui_common:templates/db_base.mak')
+%>
+
+<%inherit file="${db_base_mak}" />
+
+<%def name="sql_results_target()" buffered="True" filter='trim'>
+  ${f'sql_results:{request.host}:{db}:{user}'}
+</%def>
+
+<%block name="title">${pgwui['pgwui_sql']['menu_label']}</%block>
+<%block name="meta_keywords">
+  <meta name="keywords"
+        content="PGWUI SQL" />
+</%block>
+
+<%block name="meta_description">
+  <meta name="description"
+        content="PostgreSQL Web User Interface, Interactive SQL execution" />
+</%block>
+
+<%block name="stylesheet_links">
+  ${parent.stylesheet_links()}
+  <link rel="stylesheet"
+        href="${request.static_url('pgwui_sql:static/pgwui_sql.css')}"
+        type="text/css" />
+</%block>
+
+<%def name="submit(tab_index)">
+  <input value="Execute" tabindex="${tab_index.val}" type="submit" />
+  <% tab_index.inc() %>
+</%def>
+
+<%def name="sql_row(tab_index)">
+      <tr>
+        <%self.lib:td_label for_id="sql_id">SQL</%self.lib:td_label>
+        <td><button type="button" tabindex="${tab_index.val}"
+                    onclick="window.open(
+                      '${request.route_path("pgwui_sql_edit")|n}',
+                      'sql_edit',
+                      'popup=true,width=800,height=600')"
+            >Show In Window</button></td>
+            <% tab_index.inc() %>
+      </tr>
+      <tr>
+        <%self.lib:td_input tab_index="${tab_index}" colspan="2">
+          <textarea name="sql"
+                    class="sqltext"
+                    tabindex="${tab_index.val}"
+                    id="sql_id">${sql}</textarea>
+        </%self.lib:td_input>
+      </tr>
+</%def>
+
+<%def name="table_rows(tab_index)">
+  <%parent:table_rows tab_index="${tab_index}" args="tab_index">
+    ## A blank table row for spacing
+    <tr class="verticalgap"><td/><td/></tr>
+    ${sql_row(tab_index)}
+  </%parent:table_rows>
+</%def>
+
+<%block name="hidden_vars">
+  <%parent:hidden_vars>
+    <input type="hidden"
+           name="db"
+           value="${db}"
+           />
+    <input type="hidden"
+           name="user"
+           value="${user}"
+           />
+  </%parent:hidden_vars>
+</%block>
diff --git a/src/pgwui_sql/templates/sql_edit.mak b/src/pgwui_sql/templates/sql_edit.mak
new file mode 100644 (file)
index 0000000..e5d605c
--- /dev/null
@@ -0,0 +1,44 @@
+<%doc>
+  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/>.
+
+  Template for sql command entry page.
+  Karl O. Pinc <kop@karlpinc.com>
+
+  This template uses the following variables in it's context:
+   
+    sql  Text of the sql command(s)
+</%doc>
+
+
+<%!
+    from pgwui_common.path import asset_abspath
+
+    sql_base_mak = asset_abspath('pgwui_sql:templates/sql_base.mak')
+%>
+
+<%inherit file="${sql_base_mak}" />
+
+<%block name="page_heading">
+  SQL Entry Window
+</%block>
+
+<% tab_index = self.attr.TabIndex() %>
+${self.main_form(tab_index, target=self.sql_results_target(),
+                 action=request.route_path('pgwui_sql'))}
index 3b5c1b7f8f6f575f4a64f5a7347a68de2e55b6d3..5a6afe5eaeec1b1534fc1b393aa8918bd1a9cf16 100644 (file)
@@ -251,3 +251,33 @@ def sql_view(request):
                      db=response['db'],
                      user=response['user']))
     return response
+
+
+@view_config(route_name='pgwui_sql_edit',
+             renderer='pgwui_sql:templates/sql_edit.mak')
+@auth_base_view
+def sql_edit_view(request):
+
+    uh = SQLHandler(request).init()
+    response = pgwui_core.core.UploadEngine(uh).run()
+
+    settings = request.registry.settings
+    response.setdefault('pgwui', dict())
+    response['pgwui']['pgwui_sql'] = settings['pgwui']['pgwui_sql']
+
+    response['result_rows'] = uh.sql_results
+
+    if response['report_success']:
+        # if pgwui_core.utils.is_checked(response['csv_checked']):
+        #     download_fmt = 'CSV'
+        # else:
+        #     download_fmt = 'TAB'
+        log.info('Successful sql: DB {db}:'
+                 # ' File ({filename}):'
+                 # ' Format {format}:'
+                 ' By user {user}'
+                 .format(  # filename=response['filename'],
+                     # format=download_fmt,
+                     db=response['db'],
+                     user=response['user']))
+    return response
index f14720a33e0e28968b709ced803a905abd91cb17..e3f0360ecd9839072f3d0d067dd30688c242de80 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()
-        mocked_add_route.assert_called_once()
+        assert mocked_add_route.call_count == 2
         mocked_scan.assert_called_once()