-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Parameter feedback - #1 Server errors #4312
Open
ranbena
wants to merge
6
commits into
master
Choose a base branch
from
param-feedback-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c553f00
Parameter input feedback - server only
ranbena ac77587
Added unit tests
ranbena e20b2b5
pep8
ranbena 5213b52
Sorting param names in error msgs
ranbena 13e5500
Parameter feedback - #2 Client errors in query page (#4319)
ranbena 9b3f31b
Restyle Parameter feedback - #1 Server errors (#6226)
restyled-io[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
from numbers import Number | ||
from redash.utils import mustache_render, json_loads | ||
from redash.permissions import require_access, view_only | ||
from funcy import distinct | ||
from funcy import distinct, lpluck, compact | ||
from dateutil.parser import parse | ||
|
||
from six import string_types, text_type | ||
|
@@ -107,6 +107,15 @@ def _is_date_range(obj): | |
return False | ||
|
||
|
||
def _is_date_range_type(type): | ||
return type in ["date-range", "datetime-range", "datetime-range-with-seconds"] | ||
|
||
|
||
def _is_tag_in_template(name, template): | ||
tags = _collect_query_parameters(template) | ||
return name in tags | ||
|
||
|
||
def _is_value_within_options(value, dropdown_options, allow_list=False): | ||
if isinstance(value, list): | ||
return allow_list and set(map(text_type, value)).issubset(set(dropdown_options)) | ||
|
@@ -122,23 +131,32 @@ def __init__(self, template, schema=None, org=None): | |
self.parameters = {} | ||
|
||
def apply(self, parameters): | ||
invalid_parameter_names = [key for (key, value) in parameters.items() if not self._valid(key, value)] | ||
if invalid_parameter_names: | ||
raise InvalidParameterError(invalid_parameter_names) | ||
# filter out params not defined in schema | ||
if self.schema: | ||
names_with_definition = lpluck("name", self.schema) | ||
parameters = {k: v for (k, v) in parameters.items() if k in names_with_definition} | ||
|
||
invalid_parameters = compact({k: self._invalid_message(k, v) for (k, v) in parameters.items()}) | ||
if invalid_parameters: | ||
raise InvalidParameterError(invalid_parameters) | ||
else: | ||
self.parameters.update(parameters) | ||
self.query = mustache_render(self.template, join_parameter_list_values(parameters, self.schema)) | ||
|
||
return self | ||
|
||
def _valid(self, name, value): | ||
def _invalid_message(self, name, value): | ||
if value is None: | ||
return 'Required parameter' | ||
|
||
# skip if no schema | ||
if not self.schema: | ||
return True | ||
return None | ||
|
||
definition = next((definition for definition in self.schema if definition["name"] == name), None) | ||
|
||
if not definition: | ||
return False | ||
return 'Parameter no longer exists in query.' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kept as safety but can not be reached since no-definition params are filtered out beforehand. |
||
|
||
enum_options = definition.get('enumOptions') | ||
query_id = definition.get('queryId') | ||
|
@@ -147,7 +165,7 @@ def _valid(self, name, value): | |
if isinstance(enum_options, string_types): | ||
enum_options = enum_options.split('\n') | ||
|
||
validators = { | ||
value_validators = { | ||
"text": lambda value: isinstance(value, string_types), | ||
"number": _is_number, | ||
"enum": lambda value: _is_value_within_options(value, | ||
|
@@ -164,9 +182,32 @@ def _valid(self, name, value): | |
"datetime-range-with-seconds": _is_date_range, | ||
} | ||
|
||
validate = validators.get(definition["type"], lambda x: False) | ||
validate_value = value_validators.get(definition["type"], lambda x: False) | ||
|
||
if not validate_value(value): | ||
return 'Invalid value' | ||
|
||
tag_error_msg = self._validate_tag(name, definition["type"]) | ||
if tag_error_msg is not None: | ||
return tag_error_msg | ||
|
||
return None | ||
|
||
def _validate_tag(self, name, type): | ||
error_msg = '{{{{ {0} }}}} not found in query' | ||
if _is_date_range_type(type): | ||
start_tag = '{}.start'.format(name) | ||
if not _is_tag_in_template(start_tag, self.template): | ||
return error_msg.format(start_tag) | ||
|
||
return validate(value) | ||
end_tag = '{}.end'.format(name) | ||
if not _is_tag_in_template(end_tag, self.template): | ||
return error_msg.format(end_tag) | ||
|
||
elif not _is_tag_in_template(name, self.template): | ||
return error_msg.format(name) | ||
|
||
return None | ||
|
||
@property | ||
def is_safe(self): | ||
|
@@ -175,23 +216,46 @@ def is_safe(self): | |
|
||
@property | ||
def missing_params(self): | ||
query_parameters = set(_collect_query_parameters(self.template)) | ||
query_parameters = _collect_query_parameters(self.template) | ||
return set(query_parameters) - set(_parameter_names(self.parameters)) | ||
|
||
@property | ||
def missing_params_error(self): | ||
missing_params = self.missing_params | ||
if not missing_params: | ||
return None | ||
|
||
parameter_names = ', '.join('"{}"'.format(name) for name in missing_params) | ||
if len(missing_params) > 1: | ||
message = 'Parameters {} are missing.'.format(parameter_names) | ||
else: | ||
message = 'Parameter {} is missing.'.format(parameter_names) | ||
|
||
parameter_errors = {name: 'Missing parameter' for name in missing_params} | ||
return message, parameter_errors | ||
|
||
@property | ||
def text(self): | ||
return self.query | ||
|
||
|
||
class InvalidParameterError(Exception): | ||
def __init__(self, parameters): | ||
parameter_names = ", ".join(parameters) | ||
message = "The following parameter values are incompatible with their definitions: {}".format(parameter_names) | ||
super(InvalidParameterError, self).__init__(message) | ||
def __init__(self, parameter_errors): | ||
parameter_names = ', '.join('"{}"'.format(name) for name in parameter_errors.keys()) | ||
if len(parameter_errors) > 1: | ||
message = 'Parameters {} are invalid.'.format(parameter_names) | ||
else: | ||
message = 'Parameter {} is invalid.'.format(parameter_names) | ||
|
||
self.message = message | ||
self.parameter_errors = parameter_errors | ||
|
||
super().__init__(message, parameter_errors) | ||
|
||
|
||
class QueryDetachedFromDataSourceError(Exception): | ||
def __init__(self, query_id): | ||
self.query_id = query_id | ||
super(QueryDetachedFromDataSourceError, self).__init__( | ||
"This query is detached from any data source. Please select a different query.") | ||
self.message = "This query is detached from any data source. Please select a different query." | ||
|
||
super().__init__(self.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I called it
error_data
but lmk if there's a standard naming convention.