fix: Fix parsing onSaving reports toast when user hasn't saved chart (#16330)

* don't maniuplate error message

* remove extra idx reference

* u

* change print

* update with test

* add case for dashboards

* fix test
This commit is contained in:
Hugh A. Miles II 2021-08-19 14:04:23 -04:00 committed by GitHub
parent 37f09bd296
commit 50d896f1b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 97 additions and 1 deletions

View File

@ -111,7 +111,7 @@ export const addReport = report => dispatch => {
const parsedError = await getClientErrorObject(e);
const errorMessage = parsedError.message;
const errorArr = Object.keys(errorMessage);
const error = errorMessage[errorArr[0]][0];
const error = errorMessage[errorArr[0]];
dispatch(
addDangerToast(
t('An error occurred while editing this report: %s', error),

View File

@ -22,9 +22,12 @@ from marshmallow import ValidationError
from superset.charts.dao import ChartDAO
from superset.commands.base import BaseCommand
from superset.dashboards.dao import DashboardDAO
from superset.models.reports import ReportCreationMethodType
from superset.reports.commands.exceptions import (
ChartNotFoundValidationError,
ChartNotSavedValidationError,
DashboardNotFoundValidationError,
DashboardNotSavedValidationError,
ReportScheduleChartOrDashboardValidationError,
)
@ -47,6 +50,17 @@ class BaseReportScheduleCommand(BaseCommand):
""" Validate chart or dashboard relation """
chart_id = self._properties.get("chart")
dashboard_id = self._properties.get("dashboard")
creation_method = self._properties.get("creation_method")
if creation_method == ReportCreationMethodType.CHARTS and not chart_id:
# User has not saved chart yet in Explore view
exceptions.append(ChartNotSavedValidationError())
return
if creation_method == ReportCreationMethodType.DASHBOARDS and not dashboard_id:
exceptions.append(DashboardNotSavedValidationError())
return
if chart_id and dashboard_id:
exceptions.append(ReportScheduleChartOrDashboardValidationError())
if chart_id:

View File

@ -80,6 +80,32 @@ class ReportScheduleChartOrDashboardValidationError(ValidationError):
super().__init__(_("Choose a chart or dashboard not both"), field_name="chart")
class ChartNotSavedValidationError(ValidationError):
"""
Marshmallow validation error for charts that haven't been saved yet
"""
def __init__(self) -> None:
super().__init__(
_("Please save your chart first, then try creating a new email report."),
field_name="chart",
)
class DashboardNotSavedValidationError(ValidationError):
"""
Marshmallow validation error for dashboards that haven't been saved yet
"""
def __init__(self) -> None:
super().__init__(
_(
"Please save your dashboard first, then try creating a new email report."
),
field_name="dashboard",
)
class ReportScheduleInvalidError(CommandInvalidError):
message = _("Report Schedule parameters are invalid.")

View File

@ -734,6 +734,62 @@ class TestReportSchedulesApi(SupersetTestCase):
assert data["result"]["timezone"] == "America/Los_Angeles"
assert rv.status_code == 201
@pytest.mark.usefixtures(
"load_birth_names_dashboard_with_slices", "create_report_schedules"
)
def test_unsaved_report_schedule_schema(self):
"""
ReportSchedule Api: Test create report schedule with unsaved chart
"""
self.login(username="admin")
chart = db.session.query(Slice).first()
dashboard = db.session.query(Dashboard).first()
example_db = get_example_database()
report_schedule_data = {
"type": ReportScheduleType.REPORT,
"name": "name3",
"description": "description",
"creation_method": ReportCreationMethodType.CHARTS,
"crontab": "0 9 * * *",
"chart": 0,
}
uri = "api/v1/report/"
rv = self.client.post(uri, json=report_schedule_data)
data = json.loads(rv.data.decode("utf-8"))
assert rv.status_code == 422
assert (
data["message"]["chart"]
== "Please save your chart first, then try creating a new email report."
)
@pytest.mark.usefixtures(
"load_birth_names_dashboard_with_slices", "create_report_schedules"
)
def test_no_dashboard_report_schedule_schema(self):
"""
ReportSchedule Api: Test create report schedule with not dashboard id
"""
self.login(username="admin")
chart = db.session.query(Slice).first()
dashboard = db.session.query(Dashboard).first()
example_db = get_example_database()
report_schedule_data = {
"type": ReportScheduleType.REPORT,
"name": "name3",
"description": "description",
"creation_method": ReportCreationMethodType.DASHBOARDS,
"crontab": "0 9 * * *",
}
uri = "api/v1/report/"
rv = self.client.post(uri, json=report_schedule_data)
data = json.loads(rv.data.decode("utf-8"))
assert rv.status_code == 422
assert (
data["message"]["dashboard"]
== "Please save your dashboard first, then try creating a new email report."
)
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
def test_create_report_schedule_chart_dash_validation(self):
"""