chore: remove sanitize (#11532)

* chore: remove sanitize

* Fix lint
This commit is contained in:
Beto Dealmeida 2020-11-02 15:13:44 -08:00 committed by GitHub
parent fac29f9dff
commit fd10c47bc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 51 deletions

View File

@ -21,13 +21,14 @@ import logging
from typing import Iterator, Tuple from typing import Iterator, Tuple
import yaml import yaml
from werkzeug.utils import secure_filename
from superset.charts.commands.exceptions import ChartNotFoundError from superset.charts.commands.exceptions import ChartNotFoundError
from superset.charts.dao import ChartDAO from superset.charts.dao import ChartDAO
from superset.datasets.commands.export import ExportDatasetsCommand from superset.datasets.commands.export import ExportDatasetsCommand
from superset.importexport.commands.base import ExportModelsCommand from superset.importexport.commands.base import ExportModelsCommand
from superset.models.slice import Slice from superset.models.slice import Slice
from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION, sanitize from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -43,7 +44,7 @@ class ExportChartsCommand(ExportModelsCommand):
@staticmethod @staticmethod
def export(model: Slice) -> Iterator[Tuple[str, str]]: def export(model: Slice) -> Iterator[Tuple[str, str]]:
chart_slug = sanitize(model.slice_name) chart_slug = secure_filename(model.slice_name)
file_name = f"charts/{chart_slug}.yaml" file_name = f"charts/{chart_slug}.yaml"
payload = model.export_to_dict( payload = model.export_to_dict(

View File

@ -21,13 +21,14 @@ import logging
from typing import Iterator, Tuple from typing import Iterator, Tuple
import yaml import yaml
from werkzeug.utils import secure_filename
from superset.charts.commands.export import ExportChartsCommand from superset.charts.commands.export import ExportChartsCommand
from superset.dashboards.commands.exceptions import DashboardNotFoundError from superset.dashboards.commands.exceptions import DashboardNotFoundError
from superset.dashboards.dao import DashboardDAO from superset.dashboards.dao import DashboardDAO
from superset.importexport.commands.base import ExportModelsCommand from superset.importexport.commands.base import ExportModelsCommand
from superset.models.dashboard import Dashboard from superset.models.dashboard import Dashboard
from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION, sanitize from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -43,7 +44,7 @@ class ExportDashboardsCommand(ExportModelsCommand):
@staticmethod @staticmethod
def export(model: Dashboard) -> Iterator[Tuple[str, str]]: def export(model: Dashboard) -> Iterator[Tuple[str, str]]:
dashboard_slug = sanitize(model.dashboard_title) dashboard_slug = secure_filename(model.dashboard_title)
file_name = f"dashboards/{dashboard_slug}.yaml" file_name = f"dashboards/{dashboard_slug}.yaml"
payload = model.export_to_dict( payload = model.export_to_dict(

View File

@ -21,12 +21,13 @@ import logging
from typing import Iterator, Tuple from typing import Iterator, Tuple
import yaml import yaml
from werkzeug.utils import secure_filename
from superset.databases.commands.exceptions import DatabaseNotFoundError from superset.databases.commands.exceptions import DatabaseNotFoundError
from superset.databases.dao import DatabaseDAO from superset.databases.dao import DatabaseDAO
from superset.importexport.commands.base import ExportModelsCommand from superset.importexport.commands.base import ExportModelsCommand
from superset.models.core import Database from superset.models.core import Database
from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION, sanitize from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -38,7 +39,7 @@ class ExportDatabasesCommand(ExportModelsCommand):
@staticmethod @staticmethod
def export(model: Database) -> Iterator[Tuple[str, str]]: def export(model: Database) -> Iterator[Tuple[str, str]]:
database_slug = sanitize(model.database_name) database_slug = secure_filename(model.database_name)
file_name = f"databases/{database_slug}.yaml" file_name = f"databases/{database_slug}.yaml"
payload = model.export_to_dict( payload = model.export_to_dict(
@ -61,7 +62,7 @@ class ExportDatabasesCommand(ExportModelsCommand):
yield file_name, file_content yield file_name, file_content
for dataset in model.tables: for dataset in model.tables:
dataset_slug = sanitize(dataset.table_name) dataset_slug = secure_filename(dataset.table_name)
file_name = f"datasets/{database_slug}/{dataset_slug}.yaml" file_name = f"datasets/{database_slug}/{dataset_slug}.yaml"
payload = dataset.export_to_dict( payload = dataset.export_to_dict(

View File

@ -21,12 +21,13 @@ import logging
from typing import Iterator, Tuple from typing import Iterator, Tuple
import yaml import yaml
from werkzeug.utils import secure_filename
from superset.connectors.sqla.models import SqlaTable from superset.connectors.sqla.models import SqlaTable
from superset.datasets.commands.exceptions import DatasetNotFoundError from superset.datasets.commands.exceptions import DatasetNotFoundError
from superset.datasets.dao import DatasetDAO from superset.datasets.dao import DatasetDAO
from superset.importexport.commands.base import ExportModelsCommand from superset.importexport.commands.base import ExportModelsCommand
from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION, sanitize from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -38,8 +39,8 @@ class ExportDatasetsCommand(ExportModelsCommand):
@staticmethod @staticmethod
def export(model: SqlaTable) -> Iterator[Tuple[str, str]]: def export(model: SqlaTable) -> Iterator[Tuple[str, str]]:
database_slug = sanitize(model.database.database_name) database_slug = secure_filename(model.database.database_name)
dataset_slug = sanitize(model.table_name) dataset_slug = secure_filename(model.table_name)
file_name = f"datasets/{database_slug}/{dataset_slug}.yaml" file_name = f"datasets/{database_slug}/{dataset_slug}.yaml"
payload = model.export_to_dict( payload = model.export_to_dict(

View File

@ -21,12 +21,13 @@ import logging
from typing import Iterator, Tuple from typing import Iterator, Tuple
import yaml import yaml
from werkzeug.utils import secure_filename
from superset.importexport.commands.base import ExportModelsCommand from superset.importexport.commands.base import ExportModelsCommand
from superset.models.sql_lab import SavedQuery from superset.models.sql_lab import SavedQuery
from superset.queries.saved_queries.commands.exceptions import SavedQueryNotFoundError from superset.queries.saved_queries.commands.exceptions import SavedQueryNotFoundError
from superset.queries.saved_queries.dao import SavedQueryDAO from superset.queries.saved_queries.dao import SavedQueryDAO
from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION, sanitize from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -39,9 +40,9 @@ class ExportSavedQueriesCommand(ExportModelsCommand):
@staticmethod @staticmethod
def export(model: SavedQuery) -> Iterator[Tuple[str, str]]: def export(model: SavedQuery) -> Iterator[Tuple[str, str]]:
# build filename based on database, optional schema, and label # build filename based on database, optional schema, and label
database_slug = sanitize(model.database.database_name) database_slug = secure_filename(model.database.database_name)
schema_slug = sanitize(model.schema) schema_slug = secure_filename(model.schema)
query_slug = sanitize(model.label) or str(model.uuid) query_slug = secure_filename(model.label) or str(model.uuid)
file_name = f"queries/{database_slug}/{schema_slug}/{query_slug}.yaml" file_name = f"queries/{database_slug}/{schema_slug}/{query_slug}.yaml"
payload = model.export_to_dict( payload = model.export_to_dict(

View File

@ -15,8 +15,6 @@
# specific language governing permissions and limitations # specific language governing permissions and limitations
# under the License. # under the License.
import logging import logging
import re
import unicodedata
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
@ -98,18 +96,3 @@ def import_from_dict(
session.commit() session.commit()
else: else:
logger.info("Supplied object is not a dictionary.") logger.info("Supplied object is not a dictionary.")
def strip_accents(text: str) -> str:
text = unicodedata.normalize("NFD", text).encode("ascii", "ignore").decode("utf-8")
return str(text)
def sanitize(name: str) -> str:
"""Sanitize a post title into a directory name."""
name = name.lower().replace(" ", "_")
name = re.sub(r"[^\w]", "", name)
name = strip_accents(name)
return name

View File

@ -37,13 +37,13 @@ class TestExportChartsCommand(SupersetTestCase):
expected = [ expected = [
"metadata.yaml", "metadata.yaml",
"charts/energy_sankey.yaml", "charts/Energy_Sankey.yaml",
"datasets/examples/energy_usage.yaml", "datasets/examples/energy_usage.yaml",
"databases/examples.yaml", "databases/examples.yaml",
] ]
assert expected == list(contents.keys()) assert expected == list(contents.keys())
metadata = yaml.safe_load(contents["charts/energy_sankey.yaml"]) metadata = yaml.safe_load(contents["charts/Energy_Sankey.yaml"])
assert metadata == { assert metadata == {
"slice_name": "Energy Sankey", "slice_name": "Energy Sankey",
"viz_type": "sankey", "viz_type": "sankey",
@ -90,7 +90,7 @@ class TestExportChartsCommand(SupersetTestCase):
command = ExportChartsCommand([example_chart.id]) command = ExportChartsCommand([example_chart.id])
contents = dict(command.run()) contents = dict(command.run())
metadata = yaml.safe_load(contents["charts/energy_sankey.yaml"]) metadata = yaml.safe_load(contents["charts/Energy_Sankey.yaml"])
assert list(metadata.keys()) == [ assert list(metadata.keys()) == [
"slice_name", "slice_name",
"viz_type", "viz_type",

View File

@ -39,24 +39,23 @@ class TestExportDashboardsCommand(SupersetTestCase):
expected_paths = { expected_paths = {
"metadata.yaml", "metadata.yaml",
"dashboards/world_banks_data.yaml", "dashboards/World_Banks_Data.yaml",
"charts/box_plot.yaml", "charts/Region_Filter.yaml",
"datasets/examples/wb_health_population.yaml", "datasets/examples/wb_health_population.yaml",
"databases/examples.yaml", "databases/examples.yaml",
"charts/treemap.yaml", "charts/Worlds_Population.yaml",
"charts/region_filter.yaml", "charts/Most_Populated_Countries.yaml",
"charts/_rural.yaml", "charts/Growth_Rate.yaml",
"charts/worlds_population.yaml", "charts/Rural.yaml",
"charts/most_populated_countries.yaml", "charts/Life_Expectancy_VS_Rural.yaml",
"charts/growth_rate.yaml", "charts/Rural_Breakdown.yaml",
"charts/life_expectancy_vs_rural_.yaml", "charts/Worlds_Pop_Growth.yaml",
"charts/rural_breakdown.yaml", "charts/Box_plot.yaml",
"charts/worlds_pop_growth.yaml", "charts/Treemap.yaml",
} }
assert expected_paths == set(contents.keys()) assert expected_paths == set(contents.keys())
metadata = yaml.safe_load(contents["dashboards/world_banks_data.yaml"]) metadata = yaml.safe_load(contents["dashboards/World_Banks_Data.yaml"])
# remove chart UUIDs from metadata so we can compare # remove chart UUIDs from metadata so we can compare
for chart_info in metadata["position"].values(): for chart_info in metadata["position"].values():
@ -178,7 +177,7 @@ class TestExportDashboardsCommand(SupersetTestCase):
command = ExportDashboardsCommand([example_dashboard.id]) command = ExportDashboardsCommand([example_dashboard.id])
contents = dict(command.run()) contents = dict(command.run())
metadata = yaml.safe_load(contents["dashboards/world_banks_data.yaml"]) metadata = yaml.safe_load(contents["dashboards/World_Banks_Data.yaml"])
assert list(metadata.keys()) == [ assert list(metadata.keys()) == [
"dashboard_title", "dashboard_title",
"description", "description",

View File

@ -54,12 +54,12 @@ class TestExportSavedQueriesCommand(SupersetTestCase):
expected = [ expected = [
"metadata.yaml", "metadata.yaml",
"queries/examples/schema1/the_answer.yaml", "queries/examples/schema1/The_answer.yaml",
"databases/examples.yaml", "databases/examples.yaml",
] ]
assert expected == list(contents.keys()) assert expected == list(contents.keys())
metadata = yaml.safe_load(contents["queries/examples/schema1/the_answer.yaml"]) metadata = yaml.safe_load(contents["queries/examples/schema1/The_answer.yaml"])
assert metadata == { assert metadata == {
"schema": "schema1", "schema": "schema1",
"label": "The answer", "label": "The answer",
@ -98,7 +98,7 @@ class TestExportSavedQueriesCommand(SupersetTestCase):
command = ExportSavedQueriesCommand([self.example_query.id]) command = ExportSavedQueriesCommand([self.example_query.id])
contents = dict(command.run()) contents = dict(command.run())
metadata = yaml.safe_load(contents["queries/examples/schema1/the_answer.yaml"]) metadata = yaml.safe_load(contents["queries/examples/schema1/The_answer.yaml"])
assert list(metadata.keys()) == [ assert list(metadata.keys()) == [
"schema", "schema",
"label", "label",