mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Search now applies to current filters
Combined search into the same form as filters. Closes #133
This commit is contained in:
parent
8a37baba14
commit
a802cbee74
5 changed files with 42 additions and 11 deletions
|
|
@ -528,12 +528,14 @@ class TableView(RowTableShared):
|
||||||
fts_table = fts_rows[0][0]
|
fts_table = fts_rows[0][0]
|
||||||
|
|
||||||
search = special_args.get('_search')
|
search = special_args.get('_search')
|
||||||
|
search_description = None
|
||||||
if search and fts_table:
|
if search and fts_table:
|
||||||
where_clauses.append(
|
where_clauses.append(
|
||||||
'rowid in (select rowid from [{fts_table}] where [{fts_table}] match :search)'.format(
|
'rowid in (select rowid from [{fts_table}] where [{fts_table}] match :search)'.format(
|
||||||
fts_table=fts_table
|
fts_table=fts_table
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
search_description = 'search matches "{}"'.format(search)
|
||||||
params['search'] = search
|
params['search'] = search
|
||||||
|
|
||||||
next = special_args.get('_next')
|
next = special_args.get('_next')
|
||||||
|
|
@ -642,10 +644,13 @@ class TableView(RowTableShared):
|
||||||
# Almost certainly hit the timeout
|
# Almost certainly hit the timeout
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# human_filter_description combines filters AND search, if provided
|
||||||
|
human_description = filters.human_description(extra=search_description)
|
||||||
|
|
||||||
async def extra_template():
|
async def extra_template():
|
||||||
return {
|
return {
|
||||||
'database_hash': hash,
|
'database_hash': hash,
|
||||||
'human_filter_description': filters.human_description(),
|
'human_filter_description': human_description,
|
||||||
'supports_search': bool(fts_table),
|
'supports_search': bool(fts_table),
|
||||||
'search': search or '',
|
'search': search or '',
|
||||||
'use_rowid': use_rowid,
|
'use_rowid': use_rowid,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
body {
|
body {
|
||||||
margin: 0 1em;
|
margin: 0 1em;
|
||||||
font-family: "Helvetica Neue", sans-serif;
|
font-family: "Helvetica Neue", sans-serif;
|
||||||
|
|
@ -107,11 +106,12 @@ form label {
|
||||||
form input[type=text],
|
form input[type=text],
|
||||||
form input[type=search] {
|
form input[type=search] {
|
||||||
border: 1px solid #ccc;
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 3px;
|
||||||
width: 60%;
|
width: 60%;
|
||||||
padding: 9px 4px;
|
padding: 9px 4px;
|
||||||
font-family: monospace;
|
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
font-size: 1.1em;
|
font-size: 1em;
|
||||||
|
font-family: Helvetica, sans-serif;
|
||||||
}
|
}
|
||||||
@media only screen and (max-width: 576px) {
|
@media only screen and (max-width: 576px) {
|
||||||
form.sql textarea {
|
form.sql textarea {
|
||||||
|
|
@ -137,6 +137,16 @@ form input[type=submit] {
|
||||||
.filter-row {
|
.filter-row {
|
||||||
margin-bottom: 0.6em;
|
margin-bottom: 0.6em;
|
||||||
}
|
}
|
||||||
|
.search-row {
|
||||||
|
margin-bottom: 1.8em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.search-row label {
|
||||||
|
font-size: 1.2em;
|
||||||
|
padding-right: 0.5em;
|
||||||
|
display: inline-block;
|
||||||
|
width: 80px;
|
||||||
|
}
|
||||||
|
|
||||||
.select-wrapper {
|
.select-wrapper {
|
||||||
border: 1px solid #ccc;
|
border: 1px solid #ccc;
|
||||||
|
|
|
||||||
|
|
@ -24,13 +24,10 @@
|
||||||
</h3>
|
</h3>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if supports_search %}
|
|
||||||
<form action="/{{ database }}-{{ database_hash }}/{{ table|quote_plus }}" method="get">
|
|
||||||
<p><input type="search" name="_search" value="{{ search }}"> <input type="submit" value="Search"></p>
|
|
||||||
</form>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
<form class="filters" action="/{{ database }}-{{ database_hash }}/{{ table|quote_plus }}" method="get">
|
<form class="filters" action="/{{ database }}-{{ database_hash }}/{{ table|quote_plus }}" method="get">
|
||||||
|
{% if supports_search %}
|
||||||
|
<div class="search-row"><label for="_search">Search:</label><input id="_search" type="search" name="_search" value="{{ search }}"></div>
|
||||||
|
{% endif %}
|
||||||
{% for column, lookup, value in filters.selections() %}
|
{% for column, lookup, value in filters.selections() %}
|
||||||
<div class="filter-row">
|
<div class="filter-row">
|
||||||
<div class="select-wrapper">
|
<div class="select-wrapper">
|
||||||
|
|
|
||||||
|
|
@ -359,8 +359,10 @@ class Filters:
|
||||||
for filter in self._filters:
|
for filter in self._filters:
|
||||||
yield filter.key, filter.display, filter.no_argument
|
yield filter.key, filter.display, filter.no_argument
|
||||||
|
|
||||||
def human_description(self):
|
def human_description(self, extra=None):
|
||||||
bits = []
|
bits = []
|
||||||
|
if extra:
|
||||||
|
bits.append(extra)
|
||||||
for column, lookup, value in self.selections():
|
for column, lookup, value in self.selections():
|
||||||
filter = self._filters_by_key.get(lookup, None)
|
filter = self._filters_by_key.get(lookup, None)
|
||||||
if filter:
|
if filter:
|
||||||
|
|
|
||||||
|
|
@ -404,6 +404,23 @@ def test_existing_filter_redirects(app_client):
|
||||||
assert '?' not in response.headers['Location']
|
assert '?' not in response.headers['Location']
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_search_parameter_gets_removed(app_client):
|
||||||
|
path_base = app_client.get(
|
||||||
|
'/test_tables/simple_primary_key', allow_redirects=False, gather_request=False
|
||||||
|
).headers['Location']
|
||||||
|
path = path_base + '?' + urllib.parse.urlencode({
|
||||||
|
'_search': '',
|
||||||
|
'_filter_column': 'name',
|
||||||
|
'_filter_op': 'exact',
|
||||||
|
'_filter_value': 'chidi',
|
||||||
|
})
|
||||||
|
response = app_client.get(path, allow_redirects=False, gather_request=False)
|
||||||
|
assert response.status == 302
|
||||||
|
assert response.headers['Location'].endswith(
|
||||||
|
'?name__exact=chidi'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
TABLES = '''
|
TABLES = '''
|
||||||
CREATE TABLE simple_primary_key (
|
CREATE TABLE simple_primary_key (
|
||||||
pk varchar(30) primary key,
|
pk varchar(30) primary key,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue