Skip to content

Commit

Permalink
backend creation method validation
Browse files Browse the repository at this point in the history
  • Loading branch information
AAfghahi committed Aug 23, 2021
1 parent efe850b commit 62fb974
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
4 changes: 1 addition & 3 deletions superset/models/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ def copy_dashboard(
)


class Dashboard( # pylint: disable=too-many-instance-attributes
Model, AuditMixinNullable, ImportExportMixin
):
class Dashboard(Model, AuditMixinNullable, ImportExportMixin):

"""The dashboard object!"""

Expand Down
2 changes: 1 addition & 1 deletion superset/models/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
logger = logging.getLogger(__name__)


class Slice( # pylint: disable=too-many-instance-attributes,too-many-public-methods
class Slice( # pylint: disable=too-many-instance-attributes, too-many-public-methods
Model, AuditMixinNullable, ImportExportMixin
):
"""A slice is essentially a report or a view on data"""
Expand Down
17 changes: 16 additions & 1 deletion superset/reports/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
from superset.commands.base import CreateMixin
from superset.dao.exceptions import DAOCreateFailedError
from superset.databases.dao import DatabaseDAO
from superset.models.reports import ReportScheduleType
from superset.models.reports import ReportCreationMethodType, ReportScheduleType
from superset.reports.commands.base import BaseReportScheduleCommand
from superset.reports.commands.exceptions import (
DatabaseNotFoundValidationError,
ReportScheduleAlertRequiredDatabaseValidationError,
ReportScheduleCreateFailedError,
ReportScheduleCreationMethodUniquenessValidationError,
ReportScheduleInvalidError,
ReportScheduleNameUniquenessValidationError,
ReportScheduleRequiredTypeValidationError,
Expand Down Expand Up @@ -59,6 +60,10 @@ def validate(self) -> None:
owner_ids: Optional[List[int]] = self._properties.get("owners")
name = self._properties.get("name", "")
report_type = self._properties.get("type")
creation_method = self._properties.get("creation_method")
chart_id = self._properties.get("chart_id")
dashboard_id = self._properties.get("dashboard_id")
user_id = self._actor.id

# Validate type is required
if not report_type:
Expand All @@ -84,6 +89,16 @@ def validate(self) -> None:
# Validate chart or dashboard relations
self.validate_chart_dashboard(exceptions)

# Validate that each chart or dashboard only has one report with
# the respective creation method.
if (
creation_method != ReportCreationMethodType.ALERTS_REPORTS
and not ReportScheduleDAO.validate_unique_creation_method(
user_id, dashboard_id, chart_id
)
):
exceptions.append(ReportScheduleCreationMethodUniquenessValidationError)

if "validator_config_json" in self._properties:
self._properties["validator_config_json"] = json.dumps(
self._properties["validator_config_json"]
Expand Down
12 changes: 12 additions & 0 deletions superset/reports/commands/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ def __init__(self) -> None:
super().__init__([_("Name must be unique")], field_name="name")


class ReportScheduleCreationMethodUniquenessValidationError(ValidationError):
"""
Marshmallow validation error for Report Schedule with creation method charts
or dashboards already existing on a report.
"""

def __init__(self) -> None:
super().__init__(
[_("Resource already has an attached report")], field_name="creation_method"
)


class AlertQueryMultipleRowsError(CommandException):

message = _("Alert query returned more then one row.")
Expand Down
18 changes: 18 additions & 0 deletions superset/reports/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ def bulk_delete(
db.session.rollback()
raise DAODeleteFailedError(str(ex)) from ex

@staticmethod
def validate_unique_creation_method(
user_id: int, dashboard_id: Optional[int] = None, chart_id: Optional[int] = None
) -> bool:
"""
Validate if the user already has a chart or dashboard
with a report attached form the self subscribe reports
"""
query = db.session.query(ReportSchedule).filter(
ReportSchedule.created_by_fk == user_id
)
if dashboard_id is not None:
query = query.filter(ReportSchedule.dashboard_id == dashboard_id)

if chart_id is not None:
query = query.filter(ReportSchedule.chart_id == chart_id)
return query.one_or_none()

@staticmethod
def validate_update_uniqueness(
name: str, report_type: str, report_schedule_id: Optional[int] = None
Expand Down

0 comments on commit 62fb974

Please sign in to comment.