-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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
feat(database): Database Filtering via custom configuration #24580
feat(database): Database Filtering via custom configuration #24580
Conversation
Codecov Report
@@ Coverage Diff @@
## master #24580 +/- ##
==========================================
- Coverage 69.08% 69.06% -0.02%
==========================================
Files 1906 1906
Lines 74168 74114 -54
Branches 8164 8165 +1
==========================================
- Hits 51239 51187 -52
+ Misses 20807 20804 -3
- Partials 2122 2123 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 12 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
fdea47a
to
ece4c77
Compare
- Add new configuration so we can inject extra filters to our databases when running the DatabaseFilter in base_filters - Add tests for our new config and its usage
ece4c77
to
579c359
Compare
assert rv.status_code == 200 | ||
|
||
# Cleanup | ||
first_model = db.session.query(Database).get(first_response.get("id")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to figure out how to do this via fixture or after every test so we can always land back in a normal state
uri = f"api/v1/database/" | ||
rv = self.client.get(uri) | ||
data = json.loads(rv.data.decode("utf-8")) | ||
self.assertEqual(data["count"], len(dbs)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hughhhh here I am testing our current behavior (default) where all databases must be returned if nothing is being set in the config, so dynamic_filter
is not defined. Then, I'm adding the patch for the config to add the filter function and testing it's being applied because dynamic_filter
is defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added explicit assertions to check whether the filter method has been called when defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here I am testing our current behavior (default) where all databases must be returned if nothing is being set in the config, so dynamic_filter is not defined. Then, I'm adding the patch for the config to add the filter function and testing it's being applied because dynamic_filter is defined.
Can you write that down in the function docstring? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you provide some scenarios where you would use this? I also wonder if this should be generalized for all entity types.
Additionally I’m not saying I’m a supporter of Monkey Patching, but we should be cognizant that sometimes the method is used to relax (as opposed to further restrict) filters whereas this approach only addresses the later.
superset/databases/filters.py
Outdated
@@ -41,6 +41,16 @@ class DatabaseFilter(BaseFilter): # pylint: disable=too-few-public-methods | |||
# TODO(bogdan): consider caching. | |||
|
|||
def apply(self, query: Query, value: Any) -> Query: | |||
# Dynamic Filters need to be applied to the Query before we filter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like a good place for a docstring comment.
- Add explicit assertions to validate the filter is not called when not defined - Use docstring comment
There might be cases where I have a given database that I want to make visible/hidden to my users based on a When it comes to |
- Generalize the new config so we can use it for other entities down the road. - Pull the new database key from the config so we apply any given filter method in the databases API - Adjust our tests with the new config name and structure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Elegant!
uri = f"api/v1/database/" | ||
rv = self.client.get(uri) | ||
data = json.loads(rv.data.decode("utf-8")) | ||
self.assertEqual(data["count"], len(dbs)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here I am testing our current behavior (default) where all databases must be returned if nothing is being set in the config, so dynamic_filter is not defined. Then, I'm adding the patch for the config to add the filter function and testing it's being applied because dynamic_filter is defined.
Can you write that down in the function docstring? :)
- Add more info to our comments in our tests
@Antonio-RiveroMartnez regarding your comment,
If that was the case shouldn't the permission override logic be handled in the security manager? Note I'm not blocking this change, I just think there's merit in making sure we measure twice cut once in terms of adding new functionality. |
SUMMARY
There might be scenarios where you want to perform custom filtering on the list of databases returned by the DatabaseRestApi, right now, there's no way besides monkey patching that you can do so. This PR adds a new config definition in our app and makes use of it in the
DatabaseFilter
which is applied to all searches so you can add custom filtering if needed via config, adding live filtering capabilities to our searches and easing customization.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TESTING INSTRUCTIONS
ADDITIONAL INFORMATION