Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
fix: Making the database read-only (apache#10823)
Browse files Browse the repository at this point in the history
Co-authored-by: John Bodley <john.bodley@airbnb.com>
(cherry picked from commit a3e2e65)
  • Loading branch information
john-bodley authored and John Bodley committed Sep 10, 2020
1 parent 8bdf45e commit 005c046
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
3 changes: 3 additions & 0 deletions superset-frontend/src/components/TableSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const propTypes = {
onChange: PropTypes.func,
clearable: PropTypes.bool,
handleError: PropTypes.func.isRequired,
isDatabaseSelectEnabled: PropTypes.bool,
};

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

export default class TableSelector extends React.PureComponent {
Expand Down Expand Up @@ -315,6 +317,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 @@ -459,15 +459,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 @@ -750,6 +748,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 @@ -832,14 +838,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"),
),
"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),
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

0 comments on commit 005c046

Please sign in to comment.