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

base_filters doesn't work for filtering relationship #1745

Closed
forestlzj opened this issue Nov 25, 2021 · 12 comments
Closed

base_filters doesn't work for filtering relationship #1745

forestlzj opened this issue Nov 25, 2021 · 12 comments
Labels

Comments

@forestlzj
Copy link

Environment

Flask-Appbuilder version: 3.3.3

pip freeze output:

Describe the expected results

Tell us what should happen.
In example: extendsecurity/extendsecurity2, if base_filters set to filter on relationship, it doesn't take effect to filter by the user's company. Instead it shows all the records

E.g. In extendsecurity2 example

def get_user_companies():
    return set([company.id for company in g.user.companies])

class ContactModelView(ModelView):
...
     base_filters = [["created_by.companies", FilterInFunction, get_user_companies]]

Steps to reproduce

Run example: extendsecurity/extendsecurity2

@ThomasP0815
Copy link
Contributor

ThomasP0815 commented Nov 26, 2021

Why do you change the example?
Use FilterInManyFunction instead of FilterInFunction.

@forestlzj
Copy link
Author

i had also tried the original code:

class FilterInManyFunction(BaseFilter):
    name = "Filter view where field is in a list returned by a function"

    def apply(self, query, func):
        query, field = get_field_setup_query(query, self.model, self.column_name)
        return query.filter(field.any(Company.id.in_(func())))


def get_user_companies():
    return set([company.id for company in g.user.companies])
    return g.user.companies


class ContactModelView(ModelView):
    datamodel = SQLAInterface(Contact)
    list_columns = [
        "name",
        "personal_celphone",
        "birthday",
        "contact_group.name",
        "created_by",
        "created_by.companies"
    ]
    add_columns = [
        "name",
        "address",
        "birthday",
        "personal_phone",
        "personal_celphone",
        "contact_group",
        "gender",
    ]
    edit_columns = [
        "name",
        "address",
        "birthday",
        "personal_phone",
        "personal_celphone",
        "contact_group",
        "gender",
    ]
    base_order = ("name", "asc")
    base_filters = [["created_by.companies", FilterInManyFunction, get_user_companies]]

when login with user2_company2, in view "List Contacts", it shows all the 99 records instead of only records for company2

@ThomasP0815
Copy link
Contributor

ThomasP0815 commented Nov 27, 2021

But it works for me.
grafik
grafik

user2_company2 sees contacts from 2 users.

@forestlzj
Copy link
Author

Flask-Appbuilder version: 3.3.3

pip freeze output:

apispec==3.3.0
attrs==19.1.0
Babel==2.6.0
backcall==0.2.0
chardet==4.0.0
click==8.0.1
colorama==0.4.1
decorator==5.1.0
defusedxml==0.5.0
dnspython==1.16.0
email-validator==1.0.5
et-xmlfile==1.1.0
fab-addon-audit==0.0.1
Flask==1.1.1
Flask-AppBuilder==3.3.3
Flask-Babel==1.0.0
Flask-BabelPkg==0.9.6
Flask-Excel==0.0.7
Flask-JWT-Extended==3.18.0
Flask-Login==0.4.1
Flask-OpenID==1.3.0
Flask-SQLAlchemy==2.4.0
Flask-WTF==0.14.2
idna==2.9
importlib-metadata==4.8.1
ipython==7.28.0
itsdangerous==1.1.0
jedi==0.18.0
Jinja2==2.10.1
jsonschema==3.0.1
lml==0.1.0
MarkupSafe==1.1.1
marshmallow==3.5.1
marshmallow-enum==1.5.1
marshmallow-sqlalchemy==0.23.0
matplotlib-inline==0.1.3
numpy==1.21.2
openpyxl==3.0.9
pandas==1.3.3
parso==0.8.2
pickleshare==0.7.5
Pillow==8.3.2
prison==0.2.1
prompt-toolkit==3.0.20
pyasn1==0.4.8
pyasn1-modules==0.2.8
pyexcel==0.6.7
pyexcel-io==0.6.4
pyexcel-webio==0.1.4
Pygments==2.10.0
PyJWT==1.7.1
pyrsistent==0.14.11
python-dateutil==2.8.0
python-ldap @ file:///D:/downloads/python_ldap-3.3.1-cp37-cp37m-win_amd64.whl
python3-openid==3.1.0
pytz==2018.9
PyYAML==5.1
six==1.12.0
speaklater==1.3
SQLAlchemy==1.3.1
SQLAlchemy-Utils==0.33.9
texttable==1.6.4
traitlets==5.1.0
typing-extensions==3.10.0.2
wcwidth==0.2.5
Werkzeug==0.15.5
WTForms==2.2.1
zipp==3.6.0

It does't work for me :(
image

image

@ThomasP0815
Copy link
Contributor

ThomasP0815 commented Nov 28, 2021

Yes you are right!
Sorry I used a virtual environment linking to an old development version (<3.0.0).
I found the breaking changes (#1398) in commit c5ca06b.
The previous commit (c8c7438) works fine.

@dpgaspar please have a look at this issue.

grafik

@ThomasP0815
Copy link
Contributor

ThomasP0815 commented Nov 28, 2021

The function "get_inner_filters" in "interface.py" causes the problem.
if "flt.column_name" is dotted it can't be a relation

My solution:

-   elif self.is_relation_many_to_one(flt.column_name
-   ) or self.is_relation_one_to_one(flt.column_name):  
+   elif self.is_relation_many_to_one(get_column_root_relation(flt.column_name)
+   ) or self.is_relation_one_to_one(get_column_root_relation(flt.column_name)):    
     _filters.append((flt.column_name, flt.__class__, value)) 

@forestlzj
Copy link
Author

thanks @ThomasP0815 .
@dpgaspar Will the fix be included in next release?

@RastaKolobar
Copy link

Thank you @ThomasP0815 .

@ptitlouis
Copy link

Hello the solution get_inner_filters using get_column_root_relation is working well on the example. But the List of Values (for my application) doesn't get the filter even though the list view has been filtered correctly. Is there something wrong with my code?
image
Many thanks.

@ThomasP0815
Copy link
Contributor

ThomasP0815 commented Dec 20, 2021

Base_filters will only apply on list views !
I think you are looking for "add_query_rel_fields" or "edit_query_rel_fields" (add/edit view).

@ptitlouis
Copy link

Ok thank you, I'm looking this way.

@stale
Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Feel free to reopen it if it's still relevant to you. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants