Skip to content

Commit

Permalink
Search now applies to current filters
Browse files Browse the repository at this point in the history
Combined search into the same form as filters.

Closes #133
  • Loading branch information
simonw committed Nov 24, 2017
1 parent 8a37bab commit a802cbe
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
7 changes: 6 additions & 1 deletion datasette/app.py
Expand Up @@ -528,12 +528,14 @@ async def data(self, request, name, hash, table):
fts_table = fts_rows[0][0]

search = special_args.get('_search')
search_description = None
if search and fts_table:
where_clauses.append(
'rowid in (select rowid from [{fts_table}] where [{fts_table}] match :search)'.format(
fts_table=fts_table
)
)
search_description = 'search matches "{}"'.format(search)
params['search'] = search

next = special_args.get('_next')
Expand Down Expand Up @@ -642,10 +644,13 @@ async def data(self, request, name, hash, table):
# Almost certainly hit the timeout
pass

# human_filter_description combines filters AND search, if provided
human_description = filters.human_description(extra=search_description)

async def extra_template():
return {
'database_hash': hash,
'human_filter_description': filters.human_description(),
'human_filter_description': human_description,
'supports_search': bool(fts_table),
'search': search or '',
'use_rowid': use_rowid,
Expand Down
16 changes: 13 additions & 3 deletions datasette/static/app.css
@@ -1,4 +1,3 @@

body {
margin: 0 1em;
font-family: "Helvetica Neue", sans-serif;
Expand Down Expand Up @@ -107,11 +106,12 @@ form label {
form input[type=text],
form input[type=search] {
border: 1px solid #ccc;
border-radius: 3px;
width: 60%;
padding: 9px 4px;
font-family: monospace;
display: inline-block;
font-size: 1.1em;
font-size: 1em;
font-family: Helvetica, sans-serif;
}
@media only screen and (max-width: 576px) {
form.sql textarea {
Expand All @@ -137,6 +137,16 @@ form input[type=submit] {
.filter-row {
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 {
border: 1px solid #ccc;
Expand Down
9 changes: 3 additions & 6 deletions datasette/templates/table.html
Expand Up @@ -24,13 +24,10 @@ <h3>{% if filtered_table_rows or filtered_table_rows == 0 %}{{ "{:,}".format(fil
</h3>
{% 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">
{% 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() %}
<div class="filter-row">
<div class="select-wrapper">
Expand Down
4 changes: 3 additions & 1 deletion datasette/utils.py
Expand Up @@ -359,8 +359,10 @@ def lookups(self):
for filter in self._filters:
yield filter.key, filter.display, filter.no_argument

def human_description(self):
def human_description(self, extra=None):
bits = []
if extra:
bits.append(extra)
for column, lookup, value in self.selections():
filter = self._filters_by_key.get(lookup, None)
if filter:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_app.py
Expand Up @@ -404,6 +404,23 @@ def test_existing_filter_redirects(app_client):
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 = '''
CREATE TABLE simple_primary_key (
pk varchar(30) primary key,
Expand Down

0 comments on commit a802cbe

Please sign in to comment.