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

Additive Filtering #336

Closed
peterschutt opened this issue Jun 11, 2020 · 2 comments · Fixed by #338
Closed

Additive Filtering #336

peterschutt opened this issue Jun 11, 2020 · 2 comments · Fixed by #338

Comments

@peterschutt
Copy link
Contributor

I have a base view that defines quite a few filters that is shared across a few different views. On one of the sub-class views I want to add another ColumnFilter, e.g.:

class BaseView(GenericModelView):
    ...
    filtering = Filtering(...)

class SubclassView(BaseView):
    ...
    filtering = ...  # I want to add a filter to the BaseView filtering without it affecting the other sub-classes.

I've done this:

from flask_resty.filtering import Filtering as Filtering_

class Filtering(Filtering_):
    def __add__(self, other):
        if not isinstance(other, Filtering):
            return NotImplemented
        new = Filtering()
        new._arg_filters = dict(**self._arg_filters)
        new._arg_filters.update(other._arg_filters)
        return new

So on the sub-class I then do:

class SubclassView(BaseView):
    ...
    filtering = BaseView.filtering + Filtering(another=ColumnFilter(...))

Is there a supported way to do this type of thing?

@sloria
Copy link
Contributor

sloria commented Jun 11, 2020

This would be nice. I suggested the same thing here: #236 (comment) . I think it'd technically be more correct to use the union operator.

Feel free to send a PR!

@taion
Copy link
Contributor

taion commented Jun 11, 2020

Yup; for now, the best way to do this is to define a dictionary, and do something like:

filtering = Filtering(
    **BASE_FILTERS,
    another=operator.eq,
)

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 a pull request may close this issue.

3 participants