From c1a4faf141da8045f806d3f3313fc064128d1aa4 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Tue, 3 Sep 2024 14:17:53 -0500 Subject: [PATCH] Beginnings of a SQL edit window --- src/pgwui_sql/pgwui_sql.py | 3 +- src/pgwui_sql/templates/sql.mak | 54 ++------------ src/pgwui_sql/templates/sql_base.mak | 105 +++++++++++++++++++++++++++ src/pgwui_sql/templates/sql_edit.mak | 44 +++++++++++ src/pgwui_sql/views/sql.py | 30 ++++++++ tests/test_pgwui_sql.py | 2 +- 6 files changed, 189 insertions(+), 49 deletions(-) create mode 100644 src/pgwui_sql/templates/sql_base.mak create mode 100644 src/pgwui_sql/templates/sql_edit.mak diff --git a/src/pgwui_sql/pgwui_sql.py b/src/pgwui_sql/pgwui_sql.py index 640f494..9e3f1e7 100644 --- a/src/pgwui_sql/pgwui_sql.py +++ b/src/pgwui_sql/pgwui_sql.py @@ -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() diff --git a/src/pgwui_sql/templates/sql.mak b/src/pgwui_sql/templates/sql.mak index ec92107..7f1affc 100644 --- a/src/pgwui_sql/templates/sql.mak +++ b/src/pgwui_sql/templates/sql.mak @@ -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 @@ -30,28 +31,10 @@ <%! 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 name="meta_keywords"> - - - -<%block name="meta_description"> - - - -<%block name="stylesheet_links"> - ${parent.stylesheet_links()} - - +<%inherit file="${sql_base_mak}" /> <%block name="action_success">

Executed SQL without errors, @@ -70,33 +53,6 @@ Execute SQL -<%def name="sql_row(tab_index)"> - - <%self.lib:td_label for_id="sql_id">SQL - - - <%self.lib:td_input tab_index="${tab_index}" colspan="2"> - - - - - -<%def name="submit(tab_index)"> - - <% tab_index.inc() %> - - -<%def name="table_rows(tab_index)"> - <%parent:table_rows tab_index="${tab_index}" args="tab_index"> - ## A blank table row for spacing - - ${sql_row(tab_index)} - - - <%def name="render_heading(headings)"> <% if headings is None: return STOP_RENDERING @@ -173,6 +129,10 @@ + + <% 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 index 0000000..a47d896 --- /dev/null +++ b/src/pgwui_sql/templates/sql_base.mak @@ -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 + . + + Template used by sql pages. + + Karl O. Pinc + + This template uses the following variables in it's context: + + sql Text of the sql command(s) + + + +<%! + 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}'} + + +<%block name="title">${pgwui['pgwui_sql']['menu_label']} +<%block name="meta_keywords"> + + + +<%block name="meta_description"> + + + +<%block name="stylesheet_links"> + ${parent.stylesheet_links()} + + + +<%def name="submit(tab_index)"> + + <% tab_index.inc() %> + + +<%def name="sql_row(tab_index)"> + + <%self.lib:td_label for_id="sql_id">SQL + + <% tab_index.inc() %> + + + <%self.lib:td_input tab_index="${tab_index}" colspan="2"> + + + + + +<%def name="table_rows(tab_index)"> + <%parent:table_rows tab_index="${tab_index}" args="tab_index"> + ## A blank table row for spacing + + ${sql_row(tab_index)} + + + +<%block name="hidden_vars"> + <%parent:hidden_vars> + + + + diff --git a/src/pgwui_sql/templates/sql_edit.mak b/src/pgwui_sql/templates/sql_edit.mak new file mode 100644 index 0000000..e5d605c --- /dev/null +++ b/src/pgwui_sql/templates/sql_edit.mak @@ -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 + . + + Template for sql command entry page. + + Karl O. Pinc + + This template uses the following variables in it's context: + + sql Text of the sql command(s) + + + +<%! + 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 + + +<% tab_index = self.attr.TabIndex() %> +${self.main_form(tab_index, target=self.sql_results_target(), + action=request.route_path('pgwui_sql'))} diff --git a/src/pgwui_sql/views/sql.py b/src/pgwui_sql/views/sql.py index 3b5c1b7..5a6afe5 100644 --- a/src/pgwui_sql/views/sql.py +++ b/src/pgwui_sql/views/sql.py @@ -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 diff --git a/tests/test_pgwui_sql.py b/tests/test_pgwui_sql.py index f14720a..e3f0360 100644 --- a/tests/test_pgwui_sql.py +++ b/tests/test_pgwui_sql.py @@ -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() -- 2.34.1