Skip to content

Commit

Permalink
can specify omnisearch-columns
Browse files Browse the repository at this point in the history
fixes #93
  • Loading branch information
cldellow committed Feb 20, 2023
1 parent a2f29c1 commit 11b23dc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
10 changes: 9 additions & 1 deletion datasette_ui_extras/omnisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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={}
Expand Down
24 changes: 24 additions & 0 deletions docs/src/pages/docs/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
}
}
}
}
}
}
```
7 changes: 7 additions & 0 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
},
"users": {
"label_column": "display_name"
},
"posts": {
"plugins": {
"datasette-ui-extras": {
"omnisearch-columns": ["post_type", "owner_user_id", "tags", "title"]
}
}
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup
import os

VERSION = "0.28"
VERSION = "0.29"


def get_long_description():
Expand Down

0 comments on commit 11b23dc

Please sign in to comment.