Skip to content

[api] New, add custom filters#1327

Merged
dpgaspar merged 7 commits intomasterfrom
feature/search-filters-override
Mar 30, 2020
Merged

[api] New, add custom filters#1327
dpgaspar merged 7 commits intomasterfrom
feature/search-filters-override

Conversation

@dpgaspar
Copy link
Owner

@dpgaspar dpgaspar commented Mar 25, 2020

Custom filters already exist on FAB and are widely used for base filtering on ModelViews or ModelRestApi. They allow you to implement a custom query that is always applied to all CRUD operations (normally to implement custom security for data access)

This PR implements a simple solution for adding custom filters to search filters, these custom filters can be defined and used the following way:

class FilterFullName(BaseFilter):
    name = _("Full name")
    arg_name = "first_or_last"

    def apply(self, query, value):
        return query.filter(
            or_(
                Contact.first_name.ilike(value + "%"),
                Contact.last_name.ilike(value + "%")
            )
        )


class SomeRestApi(ModelRestApi):
    ...
    search_filters = {"title": [FilterFullName]}
    ...

@dpgaspar dpgaspar marked this pull request as ready for review March 30, 2020 08:43
@dpgaspar dpgaspar merged commit 7f52f65 into master Mar 30, 2020
@dpgaspar dpgaspar deleted the feature/search-filters-override branch March 30, 2020 12:48
@dpgaspar dpgaspar mentioned this pull request Apr 8, 2020
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant