Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] New, add custom filters #1327

Merged
merged 7 commits into from
Mar 30, 2020
Merged

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