From 1f58e18b6f134e2ad4c6865c6b63b40135889ebc Mon Sep 17 00:00:00 2001 From: Benedict Jin <1571805553@qq.com> Date: Thu, 9 Feb 2017 03:52:58 +0800 Subject: [PATCH] Some code refactoring (#2139) --- superset/data/__init__.py | 2 +- superset/dataframe.py | 4 ++-- superset/forms.py | 4 ++-- superset/jinja_context.py | 4 ++-- superset/migrations/env.py | 2 +- superset/models.py | 13 ++++++------- superset/security.py | 14 +++++++------- superset/utils.py | 4 ++-- superset/views.py | 4 ++-- tests/access_tests.py | 1 + tests/base_tests.py | 3 +-- tests/email_tests.py | 1 - tests/superset_test_config.py | 5 +++-- tests/utils_tests.py | 10 +++++----- 14 files changed, 35 insertions(+), 36 deletions(-) diff --git a/superset/data/__init__.py b/superset/data/__init__.py index e96ef36f35af9..70564e54c2691 100644 --- a/superset/data/__init__.py +++ b/superset/data/__init__.py @@ -1110,7 +1110,7 @@ def load_multiformat_time_series_data(): def load_misc_dashboard(): - """Loading a dasbhoard featuring misc charts""" + """Loading a dashboard featuring misc charts""" print("Creating the dashboard") db.session.expunge_all() diff --git a/superset/dataframe.py b/superset/dataframe.py index 3f315d40ba3c2..1785fda32f6c9 100644 --- a/superset/dataframe.py +++ b/superset/dataframe.py @@ -79,10 +79,10 @@ def datetime_conversion_rate(data_series): success = 0 total = 0 for value in data_series: - total = total + 1 + total += 1 try: pd.to_datetime(value) - success = success + 1 + success += 1 except Exception: continue return 100 * success / total diff --git a/superset/forms.py b/superset/forms.py index 150c0b7097bbe..63e1f1084257c 100755 --- a/superset/forms.py +++ b/superset/forms.py @@ -20,7 +20,7 @@ config = app.config TIMESTAMP_CHOICES = [ - ('smart_date', 'Adaptative formating'), + ('smart_date', 'Adaptive formatting'), ("%m/%d/%Y", '"%m/%d/%Y" | 01/14/2019'), ("%Y-%m-%d", '"%Y-%m-%d" | 2019-01-14'), ("%Y-%m-%d %H:%M:%S", @@ -354,7 +354,7 @@ def __init__(self, viz): "choices": self.choicify(['auto', 50, 75, 100, 125, 150, 200]), "default": 'auto', "description": _( - "Bottom marging, in pixels, allowing for more room for " + "Bottom margin, in pixels, allowing for more room for " "axis labels"), }), 'page_length': (FreeFormSelectField, { diff --git a/superset/jinja_context.py b/superset/jinja_context.py index 1324a52be4731..c861d9ac20643 100644 --- a/superset/jinja_context.py +++ b/superset/jinja_context.py @@ -92,7 +92,7 @@ def _partition_query(table_name, limit=0, order_by=None, filters=None): order :type order_by: list of (str, bool) tuples :param filters: a list of filters to apply - :param filters: dict of field anme and filter value combinations + :param filters: dict of field name and filter value combinations """ limit_clause = "LIMIT {}".format(limit) if limit else '' order_by_clause = '' @@ -173,7 +173,7 @@ def latest_sub_partition(self, table_name, **kwargs): part_fields = indexes[0]['column_names'] for k in kwargs.keys(): if k not in k in part_fields: - msg = "Field [{k}] is not part of the partionning key" + msg = "Field [{k}] is not part of the portioning key" raise SupersetTemplateException(msg) if len(kwargs.keys()) != len(part_fields) - 1: msg = ( diff --git a/superset/migrations/env.py b/superset/migrations/env.py index 5232c7e5c2924..428e6e3b534bd 100755 --- a/superset/migrations/env.py +++ b/superset/migrations/env.py @@ -84,7 +84,7 @@ def process_revision_directives(context, revision, directives): context.configure(connection=connection, target_metadata=target_metadata, - #compare_type=True, + # compare_type=True, process_revision_directives=process_revision_directives, **kwargs) diff --git a/superset/models.py b/superset/models.py index 71ab613abe90f..ae0ba525c7844 100644 --- a/superset/models.py +++ b/superset/models.py @@ -12,7 +12,6 @@ import numpy import pickle import re -import six import textwrap from copy import deepcopy, copy from datetime import timedelta, datetime, date @@ -233,10 +232,10 @@ class CssTemplate(Model, AuditMixinNullable): slice_user = Table('slice_user', Model.metadata, - Column('id', Integer, primary_key=True), - Column('user_id', Integer, ForeignKey('ab_user.id')), - Column('slice_id', Integer, ForeignKey('slices.id')) -) + Column('id', Integer, primary_key=True), + Column('user_id', Integer, ForeignKey('ab_user.id')), + Column('slice_id', Integer, ForeignKey('slices.id')) + ) class Slice(Model, AuditMixinNullable, ImportMixin): @@ -1987,7 +1986,7 @@ def get_metric_obj(self, metric_name): def import_obj(cls, i_datasource, import_time=None): """Imports the datasource from the object to the database. - Metrics and columns and datasource will be overrided if exists. + Metrics and columns and datasource will be overridden if exists. This function can be used to import/export dashboards between multiple superset instances. Audit metadata isn't copies over. """ @@ -2258,7 +2257,7 @@ def values_for_column(self, to_dttm, limit=500): """Retrieve some values for the given column""" - # TODO: Use Lexicographic TopNMeticSpec onces supported by PyDruid + # TODO: Use Lexicographic TopNMetricSpec once supported by PyDruid from_dttm = from_dttm.replace(tzinfo=config.get("DRUID_TZ")) to_dttm = to_dttm.replace(tzinfo=config.get("DRUID_TZ")) diff --git a/superset/security.py b/superset/security.py index daf5d8947c149..1834213f040fd 100644 --- a/superset/security.py +++ b/superset/security.py @@ -9,22 +9,22 @@ from superset import conf, db, models, sm, source_registry -READ_ONLY_MODELVIEWS = { +READ_ONLY_MODEL_VIEWS = { 'DatabaseAsync', 'DatabaseView', 'DruidClusterModelView', } -GAMMA_READ_ONLY_MODELVIEWS = { +GAMMA_READ_ONLY_MODEL_VIEWS = { 'SqlMetricInlineView', 'TableColumnInlineView', 'TableModelView', 'DruidColumnInlineView', 'DruidDatasourceModelView', 'DruidMetricInlineView', -} | READ_ONLY_MODELVIEWS +} | READ_ONLY_MODEL_VIEWS -ADMIN_ONLY_VIEW_MENUES = { +ADMIN_ONLY_VIEW_MENUS = { 'AccessRequestsModelView', 'Manage', 'SQL Lab', @@ -103,15 +103,15 @@ def get_or_create_main_db(): def is_admin_only(pvm): # not readonly operations on read only model views allowed only for admins - if (pvm.view_menu.name in READ_ONLY_MODELVIEWS and + if (pvm.view_menu.name in READ_ONLY_MODEL_VIEWS and pvm.permission.name not in READ_ONLY_PERMISSION): return True - return (pvm.view_menu.name in ADMIN_ONLY_VIEW_MENUES or + return (pvm.view_menu.name in ADMIN_ONLY_VIEW_MENUS or pvm.permission.name in ADMIN_ONLY_PERMISSIONS) def is_alpha_only(pvm): - if (pvm.view_menu.name in GAMMA_READ_ONLY_MODELVIEWS and + if (pvm.view_menu.name in GAMMA_READ_ONLY_MODEL_VIEWS and pvm.permission.name not in READ_ONLY_PERMISSION): return True return pvm.permission.name in ALPHA_ONLY_PERMISSIONS diff --git a/superset/utils.py b/superset/utils.py index a60f1c0e4ee9d..d6f0e484210e6 100644 --- a/superset/utils.py +++ b/superset/utils.py @@ -329,8 +329,8 @@ def markdown(s, markup_wrap=False): return s -def readfile(filepath): - with open(filepath) as f: +def readfile(file_path): + with open(file_path) as f: content = f.read() return content diff --git a/superset/views.py b/superset/views.py index 18150c6bf04c2..c9b4aea096e38 100755 --- a/superset/views.py +++ b/superset/views.py @@ -1348,7 +1348,7 @@ def clean_fulfilled_requests(session): user = sm.get_user_by_id(r.created_by_fk) if not datasource or \ self.datasource_access(datasource, user): - # datasource doesnot exist anymore + # datasource does not exist anymore session.delete(r) session.commit() datasource_type = request.args.get('datasource_type') @@ -2493,7 +2493,7 @@ def results(self, key): return Response( json.dumps({ 'error': ( - "Data could not be retrived. You may want to " + "Data could not be retrieved. You may want to " "re-run the query." ) }), diff --git a/tests/access_tests.py b/tests/access_tests.py index 83eaeb2b93045..1d0b2ac1e74a0 100644 --- a/tests/access_tests.py +++ b/tests/access_tests.py @@ -56,6 +56,7 @@ DB_ACCESS_ROLE = 'db_access_role' SCHEMA_ACCESS_ROLE = 'schema_access_role' + def create_access_request(session, ds_type, ds_name, role_name, user_name): ds_class = SourceRegistry.sources[ds_type] # TODO: generalize datasource names diff --git a/tests/base_tests.py b/tests/base_tests.py index 0c20a09bb935b..fb218b9c538db 100644 --- a/tests/base_tests.py +++ b/tests/base_tests.py @@ -27,7 +27,7 @@ def __init__(self, *args, **kwargs): if ( self.requires_examples and not os.environ.get('SOLO_TEST') and - not os.environ.get('examples_loaded') + not os.environ.get('examples_loaded') ): logging.info("Loading examples") cli.load_examples(load_test_data=True) @@ -275,4 +275,3 @@ def assert_can_all(view_menu): self.assertIn(('can_fave_slices', 'Superset'), gamma_perm_set) self.assertIn(('can_save_dash', 'Superset'), gamma_perm_set) self.assertIn(('can_slice', 'Superset'), gamma_perm_set) - diff --git a/tests/email_tests.py b/tests/email_tests.py index 9996d00f6ef19..4c505b247acc9 100644 --- a/tests/email_tests.py +++ b/tests/email_tests.py @@ -59,7 +59,6 @@ def test_send_bcc_smtp(self, mock_send_mime): mimeapp = MIMEApplication('attachment') assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload() - @mock.patch('smtplib.SMTP_SSL') @mock.patch('smtplib.SMTP') def test_send_mime(self, mock_smtp, mock_smtp_ssl): diff --git a/tests/superset_test_config.py b/tests/superset_test_config.py index 9f1dd3dd0e9fc..d98159b0c1cc0 100644 --- a/tests/superset_test_config.py +++ b/tests/superset_test_config.py @@ -10,8 +10,8 @@ if 'SUPERSET__SQLALCHEMY_DATABASE_URI' in os.environ: SQLALCHEMY_DATABASE_URI = os.environ.get('SUPERSET__SQLALCHEMY_DATABASE_URI') -SQL_CELERY_DB_FILE_PATH = os.path.join(DATA_DIR, 'celerydb.sqlite') -SQL_CELERY_RESULTS_DB_FILE_PATH = os.path.join(DATA_DIR, 'celery_results.sqlite') +SQL_CELERY_DB_FILE_PATH = os.path.join(DATA_DIR, 'celerydb.sqlite') +SQL_CELERY_RESULTS_DB_FILE_PATH = os.path.join(DATA_DIR, 'celery_results.sqlite') SQL_SELECT_AS_CTA = True SQL_MAX_ROW = 666 @@ -23,6 +23,7 @@ AUTH_ROLE_PUBLIC = 'Public' EMAIL_NOTIFICATIONS = False + class CeleryConfig(object): BROKER_URL = 'sqla+sqlite:///' + SQL_CELERY_DB_FILE_PATH CELERY_IMPORTS = ('superset.sql_lab', ) diff --git a/tests/utils_tests.py b/tests/utils_tests.py index bac0a7b80d8dd..231f03b84fe8a 100644 --- a/tests/utils_tests.py +++ b/tests/utils_tests.py @@ -35,11 +35,11 @@ def test_json_iso_dttm_ser(self): json_iso_dttm_ser("this is not a date") def test_base_json_conv(self): - assert isinstance(base_json_conv(numpy.bool_(1)), bool) == True - assert isinstance(base_json_conv(numpy.int64(1)), int) == True - assert isinstance(base_json_conv(set([1])), list) == True - assert isinstance(base_json_conv(Decimal('1.0')), float) == True - assert isinstance(base_json_conv(uuid.uuid4()), str) == True + assert isinstance(base_json_conv(numpy.bool_(1)), bool) is True + assert isinstance(base_json_conv(numpy.int64(1)), int) is True + assert isinstance(base_json_conv(set([1])), list) is True + assert isinstance(base_json_conv(Decimal('1.0')), float) is True + assert isinstance(base_json_conv(uuid.uuid4()), str) is True @patch('superset.utils.datetime') def test_parse_human_timedelta(self, mock_now):