From 11b23dcbbe2aa195055d94da71a3093db8a10720 Mon Sep 17 00:00:00 2001 From: Colin Dellow Date: Mon, 20 Feb 2023 10:56:26 -0500 Subject: [PATCH] can specify omnisearch-columns fixes #93 --- datasette_ui_extras/omnisearch.py | 10 +++++++++- docs/src/pages/docs/metadata.md | 24 ++++++++++++++++++++++++ metadata.json | 7 +++++++ setup.py | 2 +- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/datasette_ui_extras/omnisearch.py b/datasette_ui_extras/omnisearch.py index 86c6e22..74b3d82 100644 --- a/datasette_ui_extras/omnisearch.py +++ b/datasette_ui_extras/omnisearch.py @@ -16,6 +16,8 @@ async def omnisearch(datasette, db, table, q): if not q: return [] + ok_columns = (datasette.plugin_config('datasette-ui-extras', db.name, table) or {}).get('omnisearch-columns', None) + # TODO: dates banned_columns = {} @@ -24,7 +26,7 @@ async def omnisearch(datasette, db, table, q): label_column = await db.label_column_for_table(table) row_results = [] - if label_column: + if label_column and (not ok_columns or label_column in ok_columns): banned_columns[label_column] = True def get_results(conn): return suggest_row_results(datasette, conn, db.name, table, label_column, q) @@ -35,6 +37,9 @@ def get_results(conn): fkey_results = [] for other_table, my_column, other_column in fkey_columns: + if ok_columns and not my_column in ok_columns: + continue + banned_columns[my_column] = True label_column = await db.label_column_for_table(other_table) @@ -45,6 +50,9 @@ def get_results(conn): all_columns = list(await db.execute('select di.name, dcs.* from dux_column_stats dcs join dux_ids di on di.id = dcs.column_id where table_id = (select id from dux_ids where name = ?)', [table])) string_results = [] for column in all_columns: + if ok_columns and not column['name'] in ok_columns: + continue + if column['name'] in banned_columns: continue # column__exact={}, column__contains={} diff --git a/docs/src/pages/docs/metadata.md b/docs/src/pages/docs/metadata.md index 30f11b7..235d60f 100644 --- a/docs/src/pages/docs/metadata.md +++ b/docs/src/pages/docs/metadata.md @@ -5,6 +5,8 @@ description: Learn how to control datasette-ui-extras' behaviour through metadat The read-only changes are all enabled by default. +## Enable edit UI by default + To enable the edit UI by default for a given table, list them in your metadata.json: ```json @@ -25,3 +27,25 @@ You can also add `?_dux_edit=1` to the URL of a row page to get the edit view. In either case, a user will only see the edit UI if they have the [`update-row`](https://docs.datasette.io/en/latest/authentication.html#update-row) permission for the given table or view. + +## Restrict which columns are searched by omnisearch + +By default, any column with a string-ish type is searched by the omnisearch feature. + +You can instead explicitly specify the columns to be searched: + +```json +{ + "databases": { + "cooking": { + "posts": { + "plugins": { + "datasette-ui-extras": { + "omnisearch-columns": ["title", "owner_user_id", "post_type", "tags"] + } + } + } + } + } +} +``` diff --git a/metadata.json b/metadata.json index 1d28004..44fe8ea 100644 --- a/metadata.json +++ b/metadata.json @@ -43,6 +43,13 @@ }, "users": { "label_column": "display_name" + }, + "posts": { + "plugins": { + "datasette-ui-extras": { + "omnisearch-columns": ["post_type", "owner_user_id", "tags", "title"] + } + } } } }, diff --git a/setup.py b/setup.py index 942445b..cfa567f 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup import os -VERSION = "0.28" +VERSION = "0.29" def get_long_description():