Skip to main content

Filterparams

Project description

# Python Filterparams #

Filterparams is a library for parsing URL paramters for filter
purposes in a backend. It provides a syntax to map SQL-like
queries on top of the query parameters and parses it into a
python object.

This is a helper library for providing filter collection APIs.
The primary use case for developing the library is to
use it with a REST-API which uses the [JSONAPI](http://jsonapi.org/)
standard. Because of this the syntax is completely compatible with
the standard and encapsulates everything in the `filter` query
parameter.

## Example ##

Given the URL (non URL escaped for better readability):
```
/users?filter[param][name][like][no_default_name]=doe&filter[param][first_name]=doe%&filter[binding]=(!no_brand_name&first_name)&filter[order]=name&filter[order]=desc(first_name)
```

It can be parsed by the given function:

```python
from filterparams import build_parser
valid_filters = ['eq', 'like']
default_filter = 'eq'

parser = build_parser(
valid_filters=valid_filters,
default_filter=default_filter,
)

query = parser(

)
```

Would parse the data. You can access the parsed filters through
`.param_order` and the orders through `.orders`. The param order
in this specific case would be resolved to:

```python
And(
left=Parameter(
name='name',
alias='no_default_name',
filter='like',
value='doe%',
),
right=Parameter(
name='first_name',
alias='first_name',
filter='eq',
value='doe',
)
)
```

The orders would be:

```python
[Order(name='name', direction='asc'),
Order(name='first_name', direction='desc')]
```

## Syntax ##

All arguments must be prefixed by "filter". It is possible to
query for specific data with filters, apply orders to the result
and to combine filters through AND, NOT and OR bindings.

The syntax builds under the filter parameter a virtual object.
The keys of the object are simulated through specifying `[{key}]`
in the passed query parameter. Thus `filter[param]` would point
to the param key in the filter object.

### Filter specification ###

The solution supports to query data through the `param` subkey.

```
filter[param][{parameter_name}][{operation}][{alias}] = {to_query_value}
```

The `operation` and `alias` parameters may be omitted. If no
`alias` is provided the given parameter name is used for it.
If no `operation` is given, the default one is used (in the
example this would be equal).

Example:
```
filter[param][phone_number][like]=001%
```

This would add a filter to all phone numbers which start with "001".

### Filter binding ###

Per default all filters are combined through AND clauses.
You can change that by specifying the `filter[binding]` argument.

This is where the aliases which you can define come into place.
The binding provides means to combine filters with AND and OR.
Also you are able to negate filters here.

The filters are addressed by their alias or name, if no alias is
provided.

If you have a filter `search_for_name`, `search_for_phone_number`
and `search_for_account_number` defined you can say
`search_for_name OR NOT search_for_number AND search_for_account_number`
by specifying the following filter:

```
filter[binding]=search_for_name|(!search_for_phone_number&search_for_account_number)
```

Even though the brackets are useless here, you can use them in
more complex filters.

The following table summarizes the possible configuration options:
<table>
<thead>
<tr>
<th>Type</th>
<th>Symbol</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr>
<td>AND</td>
<td>&</td>
<td>a&b</td>
</tr>
<tr>
<td>OR</td>
<td>|</td>
<td>a|b</td>
</tr>
<tr>
<td>NOT</td>
<td>!</td>
<td>!a</td>
</tr>
<tr>
<td>Bracket</td>
<td>()</td>
<td>(a|b)&c</td>
</tr>
</tbody>
</table>

### Ordering ###

To specify a sort order of the results the `filter[order]` parameter
may be used. The value can be specified multiple times. To add
ordering you have to provide the name of the parameter which should
be ordered, not its alias!

If you want to order by `name`, `first_name` and in reverse order
`balance` you can do so by specifying the following query url
parameters:

```
filter[order]=name&filter[order]=first_name&filter[order]=desc(balance)
```

As you can see the `desc()` definition can be used to indicate
reverse ordering.

### Filter definition ###

Not every backend does or should support all possible filter
mechanisms. This is why the filters which should be accepted
by the backend have to be added before processing the query
parameters.

You can limit the allowed filters by building a parse through the
`filterparams.build_parser` function. You can configure the allowed
filters through the `valid_filters` definition. Additionally you
have to add the default filter by using the second `default_filter`
parameter.

```python
from filterparams import build_parser

valid_filters = ['eq', 'like']
default_filter = 'eq'

parser = build_parser(
valid_filters=valid_filters,
default_filter=default_filter,
)

query = parser({})
```

If you don't want any validation you can use the `parse` function.

```python
from filterparams import parse

query = parse({})
```

## Notes ##

- There do no yet exist any public projects which use this library to provide transparent mapping to an underlying
backend. I plan long-term to add another library which does use this package and provide a way to map it on sqlalchemy models.
If you are planning to do this or use it for other data mapping please contact me and I'll add a reference to it in
the README.
- The same as mentioned above is valid for client libraries, which generate the filter query structure in any language.
Again, as soon as the API is stable I'll probably add a JavaScript library.
- Depending on your backend it might not make sense to support all features (ordering, parameter binding) of the
language. You might still want to use it to parse your basic parameters though and ignore the rest.

## Used Libraries ##

For evaluating the filter params ordering the [funcparserlib](https://github.com/vlasovskikh/funcparserlib) ([MIT license](https://github.com/vlasovskikh/funcparserlib/blob/master/LICENSE))
module is used. Additionally the [Werkzeug](https://github.com/mitsuhiko/werkzeug/blob/master/LICENSE) package is used for supporting dicts with multiple values in the same key.

## Other Languages ##

This is a list of projects implementing the same API for other languages.
Currently this list only has one entry.

- Go - [go-filterparams](https://github.com/cbrand/go-filterparams)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

filterparams-1.0.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

filterparams-1.0.1-py3.5.egg (5.4 kB view details)

Uploaded Egg

filterparams-1.0.1-py3.4.egg (12.8 kB view details)

Uploaded Egg

filterparams-1.0.1-py3.3.egg (13.0 kB view details)

Uploaded Egg

filterparams-1.0.1-py3.2.egg (11.0 kB view details)

Uploaded Egg

filterparams-1.0.1-py3.1.egg (10.8 kB view details)

Uploaded Egg

filterparams-1.0.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

filterparams-1.0.1-py2.7.egg (5.5 kB view details)

Uploaded Egg

filterparams-1.0.1-py2-none-any.whl (6.8 kB view details)

Uploaded Python 2

File details

Details for the file filterparams-1.0.1.tar.gz.

File metadata

  • Download URL: filterparams-1.0.1.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for filterparams-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2d5191fc80a52cdc813059876fc9fb4e1c794dcf9447c6f8676222489741650f
MD5 6c1bb47b86d8d033be44c2b868936820
BLAKE2b-256 7dfbe6437aa7acde54bc4d1015437a81edcf5a56ab003f53675a0996afff30b2

See more details on using hashes here.

File details

Details for the file filterparams-1.0.1-py3.5.egg.

File metadata

File hashes

Hashes for filterparams-1.0.1-py3.5.egg
Algorithm Hash digest
SHA256 faf64a78d03c91959f8be450d7148fac790a42ecad5185dd731bb426c3122065
MD5 4f069d54b0d48085ee456ba1fad649a9
BLAKE2b-256 4618155941b170b878c3d32b19624eba884e878b4c6f76cf7301d35b42de95a4

See more details on using hashes here.

File details

Details for the file filterparams-1.0.1-py3.4.egg.

File metadata

File hashes

Hashes for filterparams-1.0.1-py3.4.egg
Algorithm Hash digest
SHA256 874e2adf9edfb7d824990832db52c243aaaf4eaf97d7997a661d5f56f04e0b39
MD5 f66f2487a8c54d22d820d79522dd4413
BLAKE2b-256 4fe9a0ffe25dcc4a43e4f45d8e94162da5a115fd8ccdd785a75f86f56fb52aae

See more details on using hashes here.

File details

Details for the file filterparams-1.0.1-py3.3.egg.

File metadata

File hashes

Hashes for filterparams-1.0.1-py3.3.egg
Algorithm Hash digest
SHA256 7af6970a3e1ad970150141331f0033a0a142699b0fc4ca875d7967de1be63cd7
MD5 61db5690f47b1595e6bf715be6ff1a5a
BLAKE2b-256 17decf0fa92ea955088b706a6afe3b079b96fb38c10ca0383275cc856306211a

See more details on using hashes here.

File details

Details for the file filterparams-1.0.1-py3.2.egg.

File metadata

File hashes

Hashes for filterparams-1.0.1-py3.2.egg
Algorithm Hash digest
SHA256 7971cb652ab67316cd58c675a6ba68585979964e2abc0bb5cd9cc85c5d4ebb9d
MD5 428da9160a1db9f06c5e22e5a165e838
BLAKE2b-256 4f96d2ad137be93698ea3565bc1cd9529014b1e7a1db74d8f2a4d8b4d48eec00

See more details on using hashes here.

File details

Details for the file filterparams-1.0.1-py3.1.egg.

File metadata

File hashes

Hashes for filterparams-1.0.1-py3.1.egg
Algorithm Hash digest
SHA256 e4d99f9281940c2dded4bdc79a432f8078a16dca8734926852436fa88ea9951a
MD5 72aaf0c3f80cf3e6d8a9ddd120111ad9
BLAKE2b-256 361761570ff2de8ded9636636027e1a45489debab6bc6763017c7722d54d8f2b

See more details on using hashes here.

File details

Details for the file filterparams-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for filterparams-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9db673544f37294c02e70d3e02cdbbade5b8f06bef8e3c7754b7852566df7366
MD5 13d1e0806e1d1c091447b3c153c08367
BLAKE2b-256 09ae6cbc861fc748b360ad4c880fe88ba1d43a2400d54ed5ddbfb690fe68a82e

See more details on using hashes here.

File details

Details for the file filterparams-1.0.1-py2.7.egg.

File metadata

File hashes

Hashes for filterparams-1.0.1-py2.7.egg
Algorithm Hash digest
SHA256 ef64fb2f5f000b12b7cd5f8a34b2fa0ea50551d21cad11f1115695675f00bb4d
MD5 859cc6cd5d17c0632c6e6101e0bdcfa3
BLAKE2b-256 e7674ea43c82e60b9dc18b266de63c840b2e71e8b09cb8185d923c660d35352d

See more details on using hashes here.

File details

Details for the file filterparams-1.0.1-py2-none-any.whl.

File metadata

File hashes

Hashes for filterparams-1.0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 16095cda8fe0f06258996938d2b9bc5c35310d3bca977e26c52023773947b2a8
MD5 9274a772469d5cea02aba089fdf9cb01
BLAKE2b-256 f4004534bee24008f064663a246862f81fc9291aa58ca900fac1bb9f7a50e1cf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page