Skip to main content

filterify is a pydantic-based library to handle filters from the query params.

Project description

Filterify

filterify is a pydantic-based library to handle filters from the query params.

Test Coverage Package version Code style: black Imports: isort


Documentation: https://filterify.boardpack.org

Source Code: https://github.com/boardpack/filterify


Requirements

Python 3.8+

filterify has the next dependencies:

Installation

$ pip install filterify

---> 100%

First steps

To start to work with filterify, you just need to have some Pydantic model you want to have as filters.

Let's define simple Address and Shipment models. Then just pass the Shipment model to the Filterify constructor and you will get a callable object to parse query params. By default, the parser returns a dictionary structure with the parsing results.

from pydantic import BaseModel
from filterify import Filterify


class Address(BaseModel):
    street: str
    city: str
    country: str


class Shipment(BaseModel):
    name: str
    sender: Address
    recipient: Address
    weight: float


model_filter = Filterify(Shipment)

print(model_filter('name=shoes&sender__country=US&recipient__country__ne=CA'))
# [
#     {
#         'field': [
#             'name'
#         ],
#         'value': 'shoes',
#         'operation': 'eq'
#     },
#     {
#         'field': [
#             'sender',
#             'country'
#         ],
#         'value': 'US',
#         'operation': 'eq'
#     },
#     {
#         'field': [
#             'recipient',
#             'country'
#         ],
#         'value': 'CA',
#         'operation': 'ne'
#     }
# ]

(This script is complete, it should run "as is")

Filterify supports nested models and uses __ as a delimiter for the nested models and operations. If you want to change it, pass the needed delimiter to the constructor as it's shown in the next example.

from pydantic import BaseModel
from filterify import Filterify


class Address(BaseModel):
    country: str


class Shipment(BaseModel):
    sender: Address


model_filter = Filterify(Shipment, delimiter='$')

print(model_filter('sender$country$ne=US'))
# [
#     {
#         'field': [
#             'sender',
#             'country'
#         ],
#         'value': 'US',
#         'operation': 'ne'
#     }
# ]

(This script is complete, it should run "as is")

Also, by default unknown fields are ignored, but you can change this behavior by passing False to the constructor parameter ignore_unknown_name.

from pydantic import BaseModel
from filterify import Filterify


class User(BaseModel):
    name: str


model_filter = Filterify(User, ignore_unknown_name=False)

print(model_filter('sender=US'))
# filterify.exceptions.UnknownFieldError: Filter name is not presented in the model: sender

(This script is complete, it should run "as is")

Ordering option

You can add an ordering field that accepts all model field names. Currently, it's used a django-like style when desc is passed as -field_name.

from pydantic import BaseModel
from filterify import Filterify


class Address(BaseModel):
    country: str


class Shipment(BaseModel):
    name: str
    sender: Address


model_filter = Filterify(Shipment, ordering=True)

print(model_filter('ordering=unknown_field'))
# raises standard pydantic ValidationError with the next message:
# unexpected value; permitted: 'name', '-name', 'sender__country', '-sender__country'

(This script is complete, it should run "as is")

If you want to change the accepted field name list, you can pass a list instead of the True value.

from pydantic import BaseModel
from filterify import Filterify


class Address(BaseModel):
    country: str


class Shipment(BaseModel):
    name: str
    sender: Address


model_filter = Filterify(Shipment, ordering=['name'])

print(model_filter('ordering=unknown_field'))
# raises standard pydantic ValidationError with the next message:
# unexpected value; permitted: 'name', '-name'

(This script is complete, it should run "as is")

Usage with FastAPI

Most validation work is done by pydantic, so filterify can be easily used with FastAPI. The internal validation model is wrapped by fastapi.Depends and exposed by the as_dependency method.

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel

from filterify import Filterify


class Address(BaseModel):
    street: str
    city: str
    country: str


class Shipment(BaseModel):
    name: str
    sender: Address
    recipient: Address
    weight: float
    length: float
    height: float


shipment_filter = Filterify(Shipment)


app = FastAPI()


@app.get('/shipments', dependencies=[shipment_filter.as_dependency()])
def shipments():
    return []


@app.get('/another_shipments')
def another_shipments(filters=shipment_filter.as_dependency()):
    print(filters)
    return []


if __name__ == '__main__':
    uvicorn.run(app)

(This script is complete, it should run "as is")

Acknowledgments

Special thanks to Sebastián Ramírez and his FastAPI project, some scripts and documentation structure and parts were used from there.

License

This project is licensed under the terms of the MIT license.

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

filterify-0.0.1.tar.gz (55.3 kB view details)

Uploaded Source

Built Distribution

filterify-0.0.1-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file filterify-0.0.1.tar.gz.

File metadata

  • Download URL: filterify-0.0.1.tar.gz
  • Upload date:
  • Size: 55.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for filterify-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c57b4a703975b5787a1581de5b8ff2941319db38c1abd069dec93f39b16ce39c
MD5 80420ea69fa2e63dfc33aaea3d8fb186
BLAKE2b-256 ed1c7023d51bd771877b8ca48ad84f6678b054bbfe6456d2c804f2b9764bcd5a

See more details on using hashes here.

File details

Details for the file filterify-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: filterify-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for filterify-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7e06fd204d92db0f730dbf3e16662e564dabf8732701a6264048358950319a4d
MD5 47809b6515f4b2b3f1c73d0b826795c5
BLAKE2b-256 a768b0b10b41066d82d72f732a41313474ec3c8af7f07a1209b1d24a361164b0

See more details on using hashes here.

Supported by

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