Skip to content
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

fix: Making the database/cluster read-only #10823

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions superset-frontend/src/components/TableSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const propTypes = {
onChange: PropTypes.func,
clearable: PropTypes.bool,
handleError: PropTypes.func.isRequired,
isDatabaseSelectEnabled: PropTypes.bool,
};

const defaultProps = {
Expand All @@ -67,6 +68,7 @@ const defaultProps = {
sqlLabMode: true,
formMode: false,
clearable: true,
isDatabaseSelectEnabled: true,
};

export default class TableSelector extends React.PureComponent {
Expand Down Expand Up @@ -313,6 +315,7 @@ export default class TableSelector extends React.PureComponent {
optionRenderer={this.renderDatabaseOption}
mutator={this.dbMutator}
placeholder={t('Select a database')}
isDisabled={!this.props.isDatabaseSelectEnabled}
autoSelect
/>,
);
Expand Down
20 changes: 9 additions & 11 deletions superset-frontend/src/datasource/DatasourceEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,13 @@ export class DatasourceEditor extends React.PureComponent {
onSchemaChange={schema =>
this.onDatasourcePropChange('schema', schema)
}
onDbChange={database =>
this.onDatasourcePropChange('database', database)
}
onTableChange={table =>
this.onDatasourcePropChange('datasource_name', table)
}
sqlLabMode={false}
clearable={false}
handleError={this.props.addDangerToast}
isDatabaseSelectEnabled={false}
/>
}
description={t(
Expand Down Expand Up @@ -748,6 +746,14 @@ export class DatasourceEditor extends React.PureComponent {
return (
<DatasourceContainer>
{this.renderErrors()}
<div className="m-t-10">
<Alert bsStyle="warning">
<strong>{t('Be careful.')} </strong>
{t(
'Changing these settings will affect all charts using this dataset, including charts owned by other people.',
)}
</Alert>
</div>
<Tabs
id="table-tabs"
onSelect={this.handleTabSelect}
Expand Down Expand Up @@ -830,14 +836,6 @@ export class DatasourceEditor extends React.PureComponent {
<Tab eventKey={4} title={t('Settings')}>
{activeTabKey === 4 && (
<div>
<div className="m-t-10">
<Alert bsStyle="warning">
<strong>{t('Be careful.')} </strong>
{t(
'Changing these settings will affect all charts using this dataset, including charts owned by other people.',
)}
</Alert>
</div>
<Col md={6}>
<FormContainer>{this.renderSettingsFieldset()}</FormContainer>
</Col>
Expand Down
15 changes: 15 additions & 0 deletions superset/connectors/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,28 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing import Any

from flask import Markup
from flask_appbuilder.fieldwidgets import BS3TextFieldWidget

from superset.connectors.base.models import BaseDatasource
from superset.exceptions import SupersetException
from superset.views.base import SupersetModelView


class BS3TextFieldROWidget( # pylint: disable=too-few-public-methods
BS3TextFieldWidget
):
"""
Custom read only text field widget.
"""

def __call__(self, field: Any, **kwargs: Any) -> Markup:
kwargs["readonly"] = "true"
return super().__call__(field, **kwargs)


class DatasourceModelView(SupersetModelView):
def pre_delete(self, item: BaseDatasource) -> None:
if item.slices:
Expand Down
17 changes: 14 additions & 3 deletions superset/connectors/druid/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_appbuilder.security.decorators import has_access
from flask_babel import lazy_gettext as _
from wtforms import StringField
from wtforms.ext.sqlalchemy.fields import QuerySelectField

from superset import db, security_manager
from superset.connectors.base.views import DatasourceModelView
from superset.connectors.base.views import BS3TextFieldROWidget, DatasourceModelView
from superset.connectors.connector_registry import ConnectorRegistry
from superset.connectors.druid import models
from superset.constants import RouteMethod
Expand Down Expand Up @@ -98,7 +99,7 @@ class DruidColumnInlineView(CompactCRUDMixin, SupersetModelView):
add_form_extra_fields = {
"datasource": QuerySelectField(
"Datasource",
query_factory=lambda: db.session().query(models.DruidDatasource),
query_factory=lambda: db.session.query(models.DruidDatasource),
allow_blank=True,
widget=Select2Widget(extra_classes="readonly"),
)
Expand Down Expand Up @@ -179,7 +180,7 @@ class DruidMetricInlineView(CompactCRUDMixin, SupersetModelView):
add_form_extra_fields = {
"datasource": QuerySelectField(
"Datasource",
query_factory=lambda: db.session().query(models.DruidDatasource),
query_factory=lambda: db.session.query(models.DruidDatasource),
allow_blank=True,
widget=Select2Widget(extra_classes="readonly"),
)
Expand Down Expand Up @@ -333,6 +334,16 @@ class DruidDatasourceModelView(DatasourceModelView, DeleteMixin, YamlExportMixin
"changed_by_": _("Changed By"),
"modified": _("Modified"),
}
edit_form_extra_fields = {
"cluster": QuerySelectField(
"Cluster",
query_factory=lambda: db.session.query(models.DruidCluster),
widget=Select2Widget(extra_classes="readonly"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make a patch release for FAB to make the readonly work again
dpgaspar/Flask-AppBuilder#1467

),
"datasource_name": StringField(
"Datasource Name", widget=BS3TextFieldROWidget()
),
}

def pre_add(self, item: "DruidDatasourceModelView") -> None:
with db.session.no_autoflush:
Expand Down
6 changes: 3 additions & 3 deletions superset/connectors/sqla/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class TableColumnInlineView( # pylint: disable=too-many-ancestors
add_form_extra_fields = {
"table": QuerySelectField(
"Table",
query_factory=lambda: db.session().query(models.SqlaTable),
query_factory=lambda: db.session.query(models.SqlaTable),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch!

allow_blank=True,
widget=Select2Widget(extra_classes="readonly"),
)
Expand Down Expand Up @@ -232,7 +232,7 @@ class SqlMetricInlineView( # pylint: disable=too-many-ancestors
add_form_extra_fields = {
"table": QuerySelectField(
"Table",
query_factory=lambda: db.session().query(models.SqlaTable),
query_factory=lambda: db.session.query(models.SqlaTable),
allow_blank=True,
widget=Select2Widget(extra_classes="readonly"),
)
Expand Down Expand Up @@ -393,7 +393,7 @@ class TableModelView( # pylint: disable=too-many-ancestors
edit_form_extra_fields = {
"database": QuerySelectField(
"Database",
query_factory=lambda: db.session().query(models.Database),
query_factory=lambda: db.session.query(models.Database),
widget=Select2Widget(extra_classes="readonly"),
)
}
Expand Down