diff --git a/CHANGELOG.rst b/CHANGELOG.rst index acf5f73..f5936b8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,10 @@ History ======= +v1.2.2 +------ +* export preview as CSV, JSONL or XLSX + v1.2.1 ------ * comment out "signal" from sitecopy management command diff --git a/README.rst b/README.rst index 3404085..760d9fb 100644 --- a/README.rst +++ b/README.rst @@ -144,6 +144,7 @@ App settings QUERY_INSPECTOR_QUERY_SUPERUSER_ONLY = True QUERY_INSPECTOR_QUERY_DEFAULT_LIMIT = 0 QUERY_INSPECTOR_QUERY_STOCK_QUERIES = [] + DEFAULT_CSV_FIELD_DELIMITER = ';' QUERY_INSPECTOR_SQL_BLACKLIST = ( 'ALTER', 'RENAME ', diff --git a/query_inspector/admin.py b/query_inspector/admin.py index ec05d69..2492fe4 100644 --- a/query_inspector/admin.py +++ b/query_inspector/admin.py @@ -16,6 +16,8 @@ from .models import Query from .sql import perform_query from .sql import reload_stock_queries +from .views import normalized_export_filename +from .views import export_any_dataset @admin.register(Query) @@ -140,6 +142,7 @@ def preview(self, request, object_id): recordset = [] elapsed = None if request.method == 'POST': + try: start = time.perf_counter() @@ -148,6 +151,18 @@ def preview(self, request, object_id): sql += ' limit %d' % sql_limit recordset = perform_query(sql, params, log=True, validate=True) + if 'btn-export-csv' in request.POST: + filename = normalized_export_filename(obj.slug, "csv") + response = export_any_dataset(request, "*", queryset=recordset, filename=filename) + return response + elif 'btn-export-jsonl' in request.POST: + filename = normalized_export_filename(obj.slug, "jsonl") + response = export_any_dataset(request, "*", queryset=recordset, filename=filename) + return response + elif 'btn-export-xlsx' in request.POST: + filename = normalized_export_filename(obj.slug, "xlsx") + response = export_any_dataset(request, "*", queryset=recordset, filename=filename) + return response # Save default parameters obj.default_parameters = params @@ -160,6 +175,12 @@ def preview(self, request, object_id): elapsed = '' messages.error(request, str(e)) + try: + import xlsxwriter + xlsxwriter_available = True + except ModuleNotFoundError as e: + xlsxwriter_available = False + return render( request, 'admin/query_inspector/query/preview.html', { @@ -177,5 +198,6 @@ def preview(self, request, object_id): 'recordset': recordset, 'elapsed': elapsed, 'sql_limit': sql_limit, + 'xlsxwriter_available': xlsxwriter_available, } ) diff --git a/query_inspector/app_settings.py b/query_inspector/app_settings.py index 8697d3a..fa69aaf 100644 --- a/query_inspector/app_settings.py +++ b/query_inspector/app_settings.py @@ -3,6 +3,9 @@ QUERY_SUPERUSER_ONLY = getattr(settings, 'QUERY_INSPECTOR_QUERY_SUPERUSER_ONLY', True) QUERY_DEFAULT_LIMIT = getattr(settings, 'QUERY_INSPECTOR_QUERY_DEFAULT_LIMIT', '0') QUERY_STOCK_QUERIES = getattr(settings, 'QUERY_INSPECTOR_QUERY_STOCK_QUERIES', []) +DEFAULT_CSV_FIELD_DELIMITER = getattr(settings, 'QUERY_INSPECTOR_DEFAULT_CSV_FIELD_DELIMITER', ';') + + SQL_BLACKLIST = getattr( settings, 'QUERY_INSPECTOR_SQL_BLACKLIST', ( diff --git a/query_inspector/templates/admin/query_inspector/query/preview.html b/query_inspector/templates/admin/query_inspector/query/preview.html index 73abf60..fdba3f2 100644 --- a/query_inspector/templates/admin/query_inspector/query/preview.html +++ b/query_inspector/templates/admin/query_inspector/query/preview.html @@ -26,8 +26,6 @@