def validate(self) -> None: exceptions: List[ValidationError] = list() database_id = self._properties["database"] table_name = self._properties["table_name"] schema = self._properties.get("schema", None) owner_ids: Optional[List[int]] = self._properties.get("owners") # Validate uniqueness if not DatasetDAO.validate_uniqueness(database_id, schema, table_name): exceptions.append(DatasetExistsValidationError(table_name)) # Validate/Populate database database = DatasetDAO.get_database_by_id(database_id) if not database: exceptions.append(DatabaseNotFoundValidationError()) self._properties["database"] = database # Validate table exists on dataset if database and not DatasetDAO.validate_table_exists( database, table_name, schema): exceptions.append(TableNotFoundValidationError(table_name)) try: owners = populate_owners(self._actor, owner_ids) self._properties["owners"] = owners except ValidationError as ex: exceptions.append(ex) if exceptions: exception = DatasetInvalidError() exception.add_list(exceptions) raise exception
def validate(self) -> None: exceptions: List[ValidationError] = [] owner_ids: Optional[List[int]] = self._properties.get("owners") slug: Optional[str] = self._properties.get("slug") # Validate/populate model exists self._model = DashboardDAO.find_by_id(self._model_id) if not self._model: raise DashboardNotFoundError() # Check ownership try: check_ownership(self._model) except SupersetSecurityException: raise DashboardForbiddenError() # Validate slug uniqueness if not DashboardDAO.validate_update_slug_uniqueness( self._model_id, slug): exceptions.append(DashboardSlugExistsValidationError()) # Validate/Populate owner if owner_ids is None: owner_ids = [owner.id for owner in self._model.owners] try: owners = populate_owners(self._actor, owner_ids) self._properties["owners"] = owners except ValidationError as ex: exceptions.append(ex) if exceptions: exception = DashboardInvalidError() exception.add_list(exceptions) raise exception
def validate(self) -> None: exceptions = list() owner_ids: Optional[List[int]] = self._properties.get("owners") # Validate/populate model exists self._model = DatasetDAO.find_by_id(self._model_id) if not self._model: raise DatasetNotFoundError() # Check ownership try: check_ownership(self._model) except SupersetSecurityException: raise DatasetForbiddenError() database_id = self._properties.get("database", None) table_name = self._properties.get("table_name", None) # Validate uniqueness if not DatasetDAO.validate_update_uniqueness( self._model.database_id, self._model_id, table_name ): exceptions.append(DatasetExistsValidationError(table_name)) # Validate/Populate database not allowed to change if database_id and database_id != self._model: exceptions.append(DatabaseChangeValidationError()) # Validate/Populate owner try: owners = populate_owners(self._actor, owner_ids) self._properties["owners"] = owners except ValidationError as e: exceptions.append(e) if exceptions: exception = DatasetInvalidError() exception.add_list(exceptions) raise exception
def validate(self) -> None: exceptions = list() datasource_type = self._properties["datasource_type"] datasource_id = self._properties["datasource_id"] dashboard_ids = self._properties.get("dashboards", []) owner_ids: Optional[List[int]] = self._properties.get("owners") # Validate/Populate datasource try: datasource = get_datasource_by_id(datasource_id, datasource_type) self._properties["datasource_name"] = datasource.name except ValidationError as ex: exceptions.append(ex) # Validate/Populate dashboards dashboards = DashboardDAO.find_by_ids(dashboard_ids) if len(dashboards) != len(dashboard_ids): exceptions.append(DashboardsNotFoundValidationError()) self._properties["dashboards"] = dashboards try: owners = populate_owners(self._actor, owner_ids) self._properties["owners"] = owners except ValidationError as ex: exceptions.append(ex) if exceptions: exception = ChartInvalidError() exception.add_list(exceptions) raise exception
def validate(self) -> None: exceptions: List[ValidationError] = list() owner_ids: Optional[List[int]] = self._properties.get("owners") role_ids: Optional[List[int]] = self._properties.get("roles") slug: str = self._properties.get("slug", "") # Validate slug uniqueness if not DashboardDAO.validate_slug_uniqueness(slug): exceptions.append(DashboardSlugExistsValidationError()) try: owners = populate_owners(self._actor, owner_ids) self._properties["owners"] = owners except ValidationError as ex: exceptions.append(ex) if exceptions: exception = DashboardInvalidError() exception.add_list(exceptions) raise exception try: roles = populate_roles(role_ids) self._properties["roles"] = roles except ValidationError as ex: exceptions.append(ex) if exceptions: exception = DashboardInvalidError() exception.add_list(exceptions) raise exception
def populate_owners(owner_ids: Optional[List[int]] = None) -> List[User]: """ Populate list of owners. If current user is missing in `owner_ids`, current user is added unless belonging to the Admin role. :param owner_ids: list of owners by id's :raises OwnersNotFoundValidationError: if at least one owner can't be resolved :returns: Final list of owners """ return populate_owners(owner_ids, default_to_user=False)
def validate(self) -> None: exceptions: List[ValidationError] = list() owner_ids: Optional[List[int]] = self._properties.get("owners") report_type = self._properties.get("type", ReportScheduleType.ALERT) name = self._properties.get("name", "") self._model = ReportScheduleDAO.find_by_id(self._model_id) # Does the report exist? if not self._model: raise ReportScheduleNotFoundError() # Validate name uniqueness if not ReportScheduleDAO.validate_update_uniqueness( name, report_schedule_id=self._model_id): exceptions.append(ReportScheduleNameUniquenessValidationError()) # validate relation by report type if not report_type: report_type = self._model.type if report_type == ReportScheduleType.ALERT: database_id = self._properties.get("database") # If database_id was sent let's validate it exists if database_id: database = DatabaseDAO.find_by_id(database_id) if not database: exceptions.append(DatabaseNotFoundValidationError()) self._properties["database"] = database # Validate chart or dashboard relations self.validate_chart_dashboard(exceptions, update=True) if "validator_config_json" in self._properties: self._properties["validator_config_json"] = json.dumps( self._properties["validator_config_json"]) # Check ownership try: check_ownership(self._model) except SupersetSecurityException: raise ReportScheduleForbiddenError() # Validate/Populate owner if owner_ids is None: owner_ids = [owner.id for owner in self._model.owners] try: owners = populate_owners(self._actor, owner_ids) self._properties["owners"] = owners except ValidationError as ex: exceptions.append(ex) if exceptions: exception = ReportScheduleInvalidError() exception.add_list(exceptions) raise exception
def save(self) -> FlaskResponse: data = request.form.get("data") if not isinstance(data, str): return json_error_response(_("Request missing data field."), status=500) datasource_dict = json.loads(data) datasource_id = datasource_dict.get("id") datasource_type = datasource_dict.get("type") database_id = datasource_dict["database"].get("id") orm_datasource = ConnectorRegistry.get_datasource( datasource_type, datasource_id, db.session) orm_datasource.database_id = database_id if "owners" in datasource_dict and orm_datasource.owner_class is not None: # Check ownership if app.config["OLD_API_CHECK_DATASET_OWNERSHIP"]: # mimic the behavior of the new dataset command that # checks ownership and ensures that non-admins aren't locked out # of the object try: check_ownership(orm_datasource) except SupersetSecurityException as ex: raise DatasetForbiddenError() from ex user = security_manager.get_user_by_id(g.user.id) datasource_dict["owners"] = populate_owners( user, datasource_dict["owners"], default_to_user=False) else: # legacy behavior datasource_dict["owners"] = (db.session.query( orm_datasource.owner_class).filter( orm_datasource.owner_class.id.in_( datasource_dict["owners"] or [])).all()) duplicates = [ name for name, count in Counter( [col["column_name"] for col in datasource_dict["columns"]]).items() if count > 1 ] if duplicates: return json_error_response( _( "Duplicate column name(s): %(columns)s", columns=",".join(duplicates), ), status=409, ) orm_datasource.update_from_object(datasource_dict) data = orm_datasource.data db.session.commit() return self.json_response(data)
def validate(self) -> None: exceptions: List[ValidationError] = list() dashboard_ids = self._properties.get("dashboards") owner_ids: Optional[List[int]] = self._properties.get("owners") # Validate if datasource_id is provided datasource_type is required datasource_id = self._properties.get("datasource_id") if datasource_id is not None: datasource_type = self._properties.get("datasource_type", "") if not datasource_type: exceptions.append( DatasourceTypeUpdateRequiredValidationError()) # Validate/populate model exists self._model = ChartDAO.find_by_id(self._model_id) if not self._model: raise ChartNotFoundError() # Check ownership try: check_ownership(self._model) except SupersetSecurityException: raise ChartForbiddenError() # Validate/Populate datasource if datasource_id is not None: try: datasource = get_datasource_by_id(datasource_id, datasource_type) self._properties["datasource_name"] = datasource.name except ValidationError as ex: exceptions.append(ex) # Validate/Populate dashboards only if it's a list if dashboard_ids is not None: dashboards = DashboardDAO.find_by_ids(dashboard_ids) if len(dashboards) != len(dashboard_ids): exceptions.append(DashboardsNotFoundValidationError()) self._properties["dashboards"] = dashboards # Validate/Populate owner try: owners = populate_owners(self._actor, owner_ids) self._properties["owners"] = owners except ValidationError as ex: exceptions.append(ex) if exceptions: exception = ChartInvalidError() exception.add_list(exceptions) raise exception
def save(self) -> FlaskResponse: data = request.form.get("data") if not isinstance(data, str): return json_error_response(_("Request missing data field."), status=500) datasource_dict = json.loads(data) datasource_id = datasource_dict.get("id") datasource_type = datasource_dict.get("type") database_id = datasource_dict["database"].get("id") orm_datasource = ConnectorRegistry.get_datasource( datasource_type, datasource_id, db.session) orm_datasource.database_id = database_id if "owners" in datasource_dict and orm_datasource.owner_class is not None: # Check ownership try: check_ownership(orm_datasource) except SupersetSecurityException as ex: raise DatasetForbiddenError() from ex user = security_manager.get_user_by_id(g.user.id) datasource_dict["owners"] = populate_owners(user, datasource_dict["owners"], default_to_user=False) duplicates = [ name for name, count in Counter( [col["column_name"] for col in datasource_dict["columns"]]).items() if count > 1 ] if duplicates: return json_error_response( _( "Duplicate column name(s): %(columns)s", columns=",".join(duplicates), ), status=409, ) orm_datasource.update_from_object(datasource_dict) data = orm_datasource.data db.session.commit() return self.json_response(sanitize_datasource_data(data))
def validate(self) -> None: exceptions: List[ValidationError] = list() owner_ids: Optional[List[int]] = self._properties.get("owners") name = self._properties.get("name", "") report_type = self._properties.get("type") # Validate type is required if not report_type: exceptions.append(ReportScheduleRequiredTypeValidationError()) # Validate name type uniqueness if report_type and not ReportScheduleDAO.validate_update_uniqueness( name, report_type): exceptions.append(ReportScheduleNameUniquenessValidationError()) # validate relation by report type if report_type == ReportScheduleType.ALERT: database_id = self._properties.get("database") if not database_id: exceptions.append( ReportScheduleAlertRequiredDatabaseValidationError()) else: database = DatabaseDAO.find_by_id(database_id) if not database: exceptions.append(DatabaseNotFoundValidationError()) self._properties["database"] = database # Validate chart or dashboard relations self.validate_chart_dashboard(exceptions) if "validator_config_json" in self._properties: self._properties["validator_config_json"] = json.dumps( self._properties["validator_config_json"]) try: owners = populate_owners(self._actor, owner_ids) self._properties["owners"] = owners except ValidationError as ex: exceptions.append(ex) if exceptions: exception = ReportScheduleInvalidError() exception.add_list(exceptions) raise exception
def validate(self) -> None: exceptions: List[ValidationError] = list() owner_ids: Optional[List[int]] = self._properties.get("owners") report_type = self._properties.get("type", ReportScheduleType.ALERT) name = self._properties.get("name", "") self._model = ReportScheduleDAO.find_by_id(self._model_id) # Does the report exist? if not self._model: raise ReportScheduleNotFoundError() # Change the state to not triggered when the user deactivates # A report that is currently in a working state. This prevents # an alert/report from being kept in a working state if activated back if (self._model.last_state == ReportState.WORKING and "active" in self._properties and not self._properties["active"]): self._properties["last_state"] = ReportState.NOOP # validate relation by report type if not report_type: report_type = self._model.type # Validate name type uniqueness if not ReportScheduleDAO.validate_update_uniqueness( name, report_type, report_schedule_id=self._model_id): exceptions.append(ReportScheduleNameUniquenessValidationError()) if report_type == ReportScheduleType.ALERT: database_id = self._properties.get("database") # If database_id was sent let's validate it exists if database_id: database = DatabaseDAO.find_by_id(database_id) if not database: exceptions.append(DatabaseNotFoundValidationError()) self._properties["database"] = database # Validate chart or dashboard relations self.validate_chart_dashboard(exceptions, update=True) if "validator_config_json" in self._properties: self._properties["validator_config_json"] = json.dumps( self._properties["validator_config_json"]) # Check ownership try: check_ownership(self._model) except SupersetSecurityException: raise ReportScheduleForbiddenError() # Validate/Populate owner if owner_ids is None: owner_ids = [owner.id for owner in self._model.owners] try: owners = populate_owners(self._actor, owner_ids) self._properties["owners"] = owners except ValidationError as ex: exceptions.append(ex) if exceptions: exception = ReportScheduleInvalidError() exception.add_list(exceptions) raise exception