From b3aa5f4313a5bc41c9e83f3a4778d4b0794ea4a8 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 2 May 2020 12:04:54 -0700 Subject: [PATCH] Added 'not like' table filter, refs #750 --- datasette/filters.py | 3 +++ docs/json_api.rst | 3 +++ tests/test_filters.py | 2 ++ 3 files changed, 8 insertions(+) diff --git a/datasette/filters.py b/datasette/filters.py index 5897a3ed..43cd29f9 100644 --- a/datasette/filters.py +++ b/datasette/filters.py @@ -137,6 +137,9 @@ class Filters: "lte", "\u2264", '"{c}" <= :{p}', "{c} \u2264 {v}", numeric=True ), TemplatedFilter("like", "like", '"{c}" like :{p}', '{c} like "{v}"'), + TemplatedFilter( + "notlike", "not like", '"{c}" not like :{p}', '{c} not like "{v}"' + ), TemplatedFilter("glob", "glob", '"{c}" glob :{p}', '{c} glob "{v}"'), InFilter(), NotInFilter(), diff --git a/docs/json_api.rst b/docs/json_api.rst index d40f8956..7d37d425 100644 --- a/docs/json_api.rst +++ b/docs/json_api.rst @@ -216,6 +216,9 @@ You can filter the data returned by the table based on column values using a que ``?column__like=value`` Match rows with a LIKE clause, case insensitive and with ``%`` as the wildcard character. +``?column__notlike=value`` + Match rows that do not match the provided LIKE clause. + ``?column__glob=value`` Similar to LIKE but uses Unix wildcard syntax and is case sensitive. diff --git a/tests/test_filters.py b/tests/test_filters.py index 8598087f..c33c2411 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -32,6 +32,8 @@ import pytest ['"foo" like :p0', '"foo" like :p1'], ["2%2", "3%3"], ), + # notlike: + ((("foo__notlike", "2%2"),), ['"foo" not like :p0'], ["2%2"],), ( (("foo__isnull", "1"), ("baz__isnull", "1"), ("bar__gt", "10")), ['"bar" > :p0', '"baz" is null', '"foo" is null'],