mirror of
https://github.com/apache/superset.git
synced 2026-04-24 18:44:53 +00:00
refactor: Repeated boilerplate code between upload to database forms (#16756)
* abstract boilerplate code into class and rename csv to file * add db migration * fix some stuff * more renaming of csv to file * rename in translations * update down revision * update down revision * bump chart version * switch to alter column name approach in db migration * fix db migration for MySQL * db migration conflict
This commit is contained in:
committed by
GitHub
parent
4f1d202430
commit
ef3afbde82
@@ -42,53 +42,55 @@ from superset.models.core import Database
|
||||
config = app.config
|
||||
|
||||
|
||||
class CsvToDatabaseForm(DynamicForm):
|
||||
class UploadToDatabaseForm(DynamicForm):
|
||||
# pylint: disable=E0211
|
||||
def csv_allowed_dbs() -> List[Database]: # type: ignore
|
||||
csv_enabled_dbs = (
|
||||
db.session.query(Database).filter_by(allow_csv_upload=True).all()
|
||||
def file_allowed_dbs() -> List[Database]: # type: ignore
|
||||
file_enabled_dbs = (
|
||||
db.session.query(Database).filter_by(allow_file_upload=True).all()
|
||||
)
|
||||
return [
|
||||
csv_enabled_db
|
||||
for csv_enabled_db in csv_enabled_dbs
|
||||
if CsvToDatabaseForm.at_least_one_schema_is_allowed(csv_enabled_db)
|
||||
file_enabled_db
|
||||
for file_enabled_db in file_enabled_dbs
|
||||
if UploadToDatabaseForm.at_least_one_schema_is_allowed(file_enabled_db)
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def at_least_one_schema_is_allowed(database: Database) -> bool:
|
||||
"""
|
||||
If the user has access to the database or all datasource
|
||||
1. if schemas_allowed_for_csv_upload is empty
|
||||
1. if schemas_allowed_for_file_upload is empty
|
||||
a) if database does not support schema
|
||||
user is able to upload csv without specifying schema name
|
||||
b) if database supports schema
|
||||
user is able to upload csv to any schema
|
||||
2. if schemas_allowed_for_csv_upload is not empty
|
||||
2. if schemas_allowed_for_file_upload is not empty
|
||||
a) if database does not support schema
|
||||
This situation is impossible and upload will fail
|
||||
b) if database supports schema
|
||||
user is able to upload to schema in schemas_allowed_for_csv_upload
|
||||
user is able to upload to schema in schemas_allowed_for_file_upload
|
||||
elif the user does not access to the database or all datasource
|
||||
1. if schemas_allowed_for_csv_upload is empty
|
||||
1. if schemas_allowed_for_file_upload is empty
|
||||
a) if database does not support schema
|
||||
user is unable to upload csv
|
||||
b) if database supports schema
|
||||
user is unable to upload csv
|
||||
2. if schemas_allowed_for_csv_upload is not empty
|
||||
2. if schemas_allowed_for_file_upload is not empty
|
||||
a) if database does not support schema
|
||||
This situation is impossible and user is unable to upload csv
|
||||
b) if database supports schema
|
||||
user is able to upload to schema in schemas_allowed_for_csv_upload
|
||||
user is able to upload to schema in schemas_allowed_for_file_upload
|
||||
"""
|
||||
if security_manager.can_access_database(database):
|
||||
return True
|
||||
schemas = database.get_schema_access_for_csv_upload()
|
||||
schemas = database.get_schema_access_for_file_upload()
|
||||
if schemas and security_manager.get_schemas_accessible_by_user(
|
||||
database, schemas, False
|
||||
):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class CsvToDatabaseForm(UploadToDatabaseForm):
|
||||
name = StringField(
|
||||
_("Table Name"),
|
||||
description=_("Name of table to be created from csv data."),
|
||||
@@ -116,7 +118,7 @@ class CsvToDatabaseForm(DynamicForm):
|
||||
)
|
||||
con = QuerySelectField(
|
||||
_("Database"),
|
||||
query_factory=csv_allowed_dbs,
|
||||
query_factory=UploadToDatabaseForm.file_allowed_dbs,
|
||||
get_pk=lambda a: a.id,
|
||||
get_label=lambda a: a.database_name,
|
||||
)
|
||||
@@ -239,54 +241,7 @@ class CsvToDatabaseForm(DynamicForm):
|
||||
)
|
||||
|
||||
|
||||
class ExcelToDatabaseForm(DynamicForm):
|
||||
# pylint: disable=E0211
|
||||
def excel_allowed_dbs() -> List[Database]: # type: ignore
|
||||
# TODO: change allow_csv_upload to allow_file_upload
|
||||
excel_enabled_dbs = (
|
||||
db.session.query(Database).filter_by(allow_csv_upload=True).all()
|
||||
)
|
||||
return [
|
||||
excel_enabled_db
|
||||
for excel_enabled_db in excel_enabled_dbs
|
||||
if ExcelToDatabaseForm.at_least_one_schema_is_allowed(excel_enabled_db)
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def at_least_one_schema_is_allowed(database: Database) -> bool:
|
||||
"""
|
||||
If the user has access to the database or all datasource
|
||||
1. if schemas_allowed_for_csv_upload is empty
|
||||
a) if database does not support schema
|
||||
user is able to upload excel without specifying schema name
|
||||
b) if database supports schema
|
||||
user is able to upload excel to any schema
|
||||
2. if schemas_allowed_for_csv_upload is not empty
|
||||
a) if database does not support schema
|
||||
This situation is impossible and upload will fail
|
||||
b) if database supports schema
|
||||
user is able to upload to schema in schemas_allowed_for_csv_upload
|
||||
elif the user does not access to the database or all datasource
|
||||
1. if schemas_allowed_for_csv_upload is empty
|
||||
a) if database does not support schema
|
||||
user is unable to upload excel
|
||||
b) if database supports schema
|
||||
user is unable to upload excel
|
||||
2. if schemas_allowed_for_csv_upload is not empty
|
||||
a) if database does not support schema
|
||||
This situation is impossible and user is unable to upload excel
|
||||
b) if database supports schema
|
||||
user is able to upload to schema in schemas_allowed_for_csv_upload
|
||||
"""
|
||||
if security_manager.can_access_database(database):
|
||||
return True
|
||||
schemas = database.get_schema_access_for_csv_upload()
|
||||
if schemas and security_manager.schemas_accessible_by_user(
|
||||
database, schemas, False
|
||||
):
|
||||
return True
|
||||
return False
|
||||
|
||||
class ExcelToDatabaseForm(UploadToDatabaseForm):
|
||||
name = StringField(
|
||||
_("Table Name"),
|
||||
description=_("Name of table to be created from excel data."),
|
||||
@@ -322,7 +277,7 @@ class ExcelToDatabaseForm(DynamicForm):
|
||||
|
||||
con = QuerySelectField(
|
||||
_("Database"),
|
||||
query_factory=excel_allowed_dbs,
|
||||
query_factory=UploadToDatabaseForm.file_allowed_dbs,
|
||||
get_pk=lambda a: a.id,
|
||||
get_label=lambda a: a.database_name,
|
||||
)
|
||||
@@ -419,56 +374,7 @@ class ExcelToDatabaseForm(DynamicForm):
|
||||
)
|
||||
|
||||
|
||||
class ColumnarToDatabaseForm(DynamicForm):
|
||||
# pylint: disable=E0211
|
||||
def columnar_allowed_dbs() -> List[Database]: # type: ignore
|
||||
# TODO: change allow_csv_upload to allow_file_upload
|
||||
columnar_enabled_dbs = (
|
||||
db.session.query(Database).filter_by(allow_csv_upload=True).all()
|
||||
)
|
||||
return [
|
||||
columnar_enabled_db
|
||||
for columnar_enabled_db in columnar_enabled_dbs
|
||||
if ColumnarToDatabaseForm.at_least_one_schema_is_allowed(
|
||||
columnar_enabled_db
|
||||
)
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def at_least_one_schema_is_allowed(database: Database) -> bool:
|
||||
"""
|
||||
If the user has access to the database or all datasource
|
||||
1. if schemas_allowed_for_csv_upload is empty
|
||||
a) if database does not support schema
|
||||
user is able to upload columnar without specifying schema name
|
||||
b) if database supports schema
|
||||
user is able to upload columnar to any schema
|
||||
2. if schemas_allowed_for_csv_upload is not empty
|
||||
a) if database does not support schema
|
||||
This situation is impossible and upload will fail
|
||||
b) if database supports schema
|
||||
user is able to upload to schema in schemas_allowed_for_csv_upload
|
||||
elif the user does not access to the database or all datasource
|
||||
1. if schemas_allowed_for_csv_upload is empty
|
||||
a) if database does not support schema
|
||||
user is unable to upload columnar
|
||||
b) if database supports schema
|
||||
user is unable to upload columnar
|
||||
2. if schemas_allowed_for_csv_upload is not empty
|
||||
a) if database does not support schema
|
||||
This situation is impossible and user is unable to upload columnar
|
||||
b) if database supports schema
|
||||
user is able to upload to schema in schemas_allowed_for_csv_upload
|
||||
"""
|
||||
if security_manager.can_access_database(database):
|
||||
return True
|
||||
schemas = database.get_schema_access_for_csv_upload()
|
||||
if schemas and security_manager.schemas_accessible_by_user(
|
||||
database, schemas, False
|
||||
):
|
||||
return True
|
||||
return False
|
||||
|
||||
class ColumnarToDatabaseForm(UploadToDatabaseForm):
|
||||
name = StringField(
|
||||
_("Table Name"),
|
||||
description=_("Name of table to be created from columnar data."),
|
||||
@@ -499,7 +405,7 @@ class ColumnarToDatabaseForm(DynamicForm):
|
||||
|
||||
con = QuerySelectField(
|
||||
_("Database"),
|
||||
query_factory=columnar_allowed_dbs,
|
||||
query_factory=UploadToDatabaseForm.file_allowed_dbs,
|
||||
get_pk=lambda a: a.id,
|
||||
get_label=lambda a: a.database_name,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user