From 1411ddec75dd1fd5d2e2b729421df5bb43794bff Mon Sep 17 00:00:00 2001 From: Rogan Date: Sun, 25 Jun 2017 04:33:30 +0800 Subject: [PATCH 1/2] add `_()` to Exception messages --- superset/connectors/sqla/models.py | 4 +- superset/connectors/sqla/views.py | 4 +- superset/views/base.py | 3 +- superset/views/core.py | 2 +- superset/viz.py | 72 +++++++++++++++--------------- 5 files changed, 43 insertions(+), 42 deletions(-) mode change 100755 => 100644 superset/viz.py diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index 1cd0818d07063..3af7a7031df7a 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -571,9 +571,9 @@ def fetch_metadata(self): try: table = self.get_sqla_table_object() except Exception: - raise Exception( + raise Exception(_( "Table doesn't seem to exist in the specified database, " - "couldn't fetch column information") + "couldn't fetch column information")) TC = TableColumn # noqa shortcut to class M = SqlMetric # noqa diff --git a/superset/connectors/sqla/views.py b/superset/connectors/sqla/views.py index 5ba10dac54f46..1fcf58e59d538 100644 --- a/superset/connectors/sqla/views.py +++ b/superset/connectors/sqla/views.py @@ -220,11 +220,11 @@ def pre_add(self, table): table.get_sqla_table_object() except Exception as e: logging.exception(e) - raise Exception( + raise Exception(_( "Table [{}] could not be found, " "please double check your " "database connection, schema, and " - "table name".format(table.name)) + "table name").format(table.name)) def post_add(self, table, flash_message=True): table.fetch_metadata() diff --git a/superset/views/base.py b/superset/views/base.py index f2cbd9bd0ec18..d03625821c944 100644 --- a/superset/views/base.py +++ b/superset/views/base.py @@ -5,6 +5,7 @@ from flask import g, redirect, Response, flash, abort from flask_babel import gettext as __ +from flask_babel import lazy_gettext as _ from flask_appbuilder import BaseView from flask_appbuilder import ModelView @@ -202,7 +203,7 @@ def validate_json(form, field): # noqa json.loads(field.data) except Exception as e: logging.exception(e) - raise Exception("json isn't valid") + raise Exception(_("json isn't valid")) class DeleteMixin(object): diff --git a/superset/views/core.py b/superset/views/core.py index 8a5de069efe19..03fa209b7e472 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -2007,7 +2007,7 @@ def sql_json(self): query_id = query.id session.commit() # shouldn't be necessary if not query_id: - raise Exception("Query record was not created as expected.") + raise Exception(_("Query record was not created as expected.")) logging.info("Triggering query_id: {}".format(query_id)) # Async request. diff --git a/superset/viz.py b/superset/viz.py old mode 100755 new mode 100644 index 75cb4113b7d8d..44634ee65b10c --- a/superset/viz.py +++ b/superset/viz.py @@ -45,7 +45,7 @@ class BaseViz(object): def __init__(self, datasource, form_data): if not datasource: - raise Exception("Viz is missing a datasource") + raise Exception(_("Viz is missing a datasource")) self.datasource = datasource self.request = request self.viz_type = form_data.get("viz_type") @@ -144,7 +144,7 @@ def query_obj(self): until = extra_filters.get('__to') or form_data.get("until", "now") to_dttm = utils.parse_human_datetime(until) if from_dttm > to_dttm: - raise Exception("From date cannot be larger than to date") + raise Exception(_("From date cannot be larger than to date")) # extras are used to query elements specific to a datasource type # for instance the extra where clause that applies only to Tables @@ -320,9 +320,9 @@ def should_be_timeseries(self): (fd.get('granularity_sqla') and fd.get('time_grain_sqla')) ) if fd.get('include_time') and not conditions_met: - raise Exception( + raise Exception(_( "Pick a granularity in the Time section or " - "uncheck 'Include Time'") + "uncheck 'Include Time'")) return fd.get('include_time') def query_obj(self): @@ -330,9 +330,9 @@ def query_obj(self): fd = self.form_data if fd.get('all_columns') and (fd.get('groupby') or fd.get('metrics')): - raise Exception( + raise Exception(_( "Choose either fields to [Group By] and [Metrics] or " - "[Columns], not both") + "[Columns], not both")) if fd.get('all_columns'): d['columns'] = fd.get('all_columns') @@ -372,13 +372,13 @@ def query_obj(self): if not groupby: groupby = [] if not groupby: - raise Exception("Please choose at least one \"Group by\" field ") + raise Exception(_("Please choose at least one \"Group by\" field ")) if not metrics: - raise Exception("Please choose at least one metric") + raise Exception(_("Please choose at least one metric")) if ( any(v in groupby for v in columns) or any(v in columns for v in groupby)): - raise Exception("groupby and columns can't overlap") + raise Exception(_("groupby and columns can't overlap")) d['groupby'] = list(set(groupby) | set(columns)) return d @@ -665,7 +665,7 @@ def query_obj(self): self.y_metric, ] if not all(d['metrics'] + [self.entity]): - raise Exception("Pick a metric for x, y and size") + raise Exception(_("Pick a metric for x, y and size")) return d def get_data(self, df): @@ -717,7 +717,7 @@ def as_floats(field): self.metric, ] if not self.metric: - raise Exception("Pick a metric to display") + raise Exception(_("Pick a metric to display")) return d def get_data(self, df): @@ -748,7 +748,7 @@ def query_obj(self): d = super(BigNumberViz, self).query_obj() metric = self.form_data.get('metric') if not metric: - raise Exception("Pick a metric!") + raise Exception(_("Pick a metric!")) d['metrics'] = [self.form_data.get('metric')] self.form_data['metric'] = metric return d @@ -777,7 +777,7 @@ def query_obj(self): d = super(BigNumberTotalViz, self).query_obj() metric = self.form_data.get('metric') if not metric: - raise Exception("Pick a metric!") + raise Exception(_("Pick a metric!")) d['metrics'] = [self.form_data.get('metric')] self.form_data['metric'] = metric return d @@ -843,7 +843,7 @@ def get_data(self, df): fd = self.form_data df = df.fillna(0) if fd.get("granularity") == "all": - raise Exception("Pick a time granularity for your time series") + raise Exception(_("Pick a time granularity for your time series")) df = df.pivot_table( index=DTTM_ALIAS, @@ -933,12 +933,12 @@ def query_obj(self): m2 = self.form_data.get('metric_2') d['metrics'] = [m1, m2] if not m1: - raise Exception("Pick a metric for left axis!") + raise Exception(_("Pick a metric for left axis!")) if not m2: - raise Exception("Pick a metric for right axis!") + raise Exception(_("Pick a metric for right axis!")) if m1 == m2: - raise Exception("Please choose different metrics" - " on left and right axis") + raise Exception(_("Please choose different metrics" + " on left and right axis")) return d def to_series(self, df, classed=''): @@ -980,7 +980,7 @@ def get_data(self, df): df = df.fillna(0) if self.form_data.get("granularity") == "all": - raise Exception("Pick a time granularity for your time series") + raise Exception(_("Pick a time granularity for your time series")) metric = fd.get('metric') metric_2 = fd.get('metric_2') @@ -1051,7 +1051,7 @@ def query_obj(self): 'row_limit', int(config.get('VIZ_ROW_LIMIT'))) numeric_column = self.form_data.get('all_columns_x') if numeric_column is None: - raise Exception("Must have one numeric column specified") + raise Exception(_("Must have one numeric column specified")) d['columns'] = [numeric_column] return d @@ -1076,11 +1076,11 @@ def query_obj(self): cols = fd.get('columns') or [] d['groupby'] = set(gb + cols) if len(d['groupby']) < len(gb) + len(cols): - raise Exception("Can't have overlap between Series and Breakdowns") + raise Exception(_("Can't have overlap between Series and Breakdowns")) if not self.metrics: - raise Exception("Pick at least one metric") + raise Exception(_("Pick at least one metric")) if not self.groupby: - raise Exception("Pick at least one field for [Series]") + raise Exception(_("Pick at least one field for [Series]")) return d def get_data(self, df): @@ -1173,7 +1173,7 @@ class SankeyViz(BaseViz): def query_obj(self): qry = super(SankeyViz, self).query_obj() if len(qry['groupby']) != 2: - raise Exception("Pick exactly 2 columns as [Source / Target]") + raise Exception(_("Pick exactly 2 columns as [Source / Target]")) qry['metrics'] = [ self.form_data['metric']] return qry @@ -1204,9 +1204,9 @@ def visit(vertex): cycle = find_cycle(hierarchy) if cycle: - raise Exception( + raise Exception(_( "There's a loop in your Sankey, please provide a tree. " - "Here's a faulty link: {}".format(cycle)) + "Here's a faulty link: {}").format(cycle)) return recs @@ -1222,7 +1222,7 @@ class DirectedForceViz(BaseViz): def query_obj(self): qry = super(DirectedForceViz, self).query_obj() if len(self.form_data['groupby']) != 2: - raise Exception("Pick exactly 2 columns to 'Group By'") + raise Exception(_("Pick exactly 2 columns to 'Group By'")) qry['metrics'] = [self.form_data['metric']] return qry @@ -1323,7 +1323,7 @@ def query_obj(self): qry = super(FilterBoxViz, self).query_obj() groupby = self.form_data.get('groupby') if len(groupby) < 1 and not self.form_data.get('date_filter'): - raise Exception("Pick at least one filter field") + raise Exception(_("Pick at least one filter field")) qry['metrics'] = [ self.form_data['metric']] return qry @@ -1469,8 +1469,8 @@ def query_obj(self): if label_col and len(label_col) >= 1: if label_col[0] == "count": - raise Exception( - "Must have a [Group By] column to have 'count' as the [Label]") + raise Exception(_( + "Must have a [Group By] column to have 'count' as the [Label]")) d['columns'].append(label_col[0]) if fd.get('point_radius') != 'Auto': @@ -1482,18 +1482,18 @@ def query_obj(self): if (label_col and len(label_col) >= 1 and label_col[0] != "count" and label_col[0] not in fd.get('groupby')): - raise Exception( - "Choice of [Label] must be present in [Group By]") + raise Exception(_( + "Choice of [Label] must be present in [Group By]")) if (fd.get("point_radius") != "Auto" and fd.get("point_radius") not in fd.get('groupby')): - raise Exception( - "Choice of [Point Radius] must be present in [Group By]") + raise Exception(_( + "Choice of [Point Radius] must be present in [Group By]")) if (fd.get('all_columns_x') not in fd.get('groupby') or fd.get('all_columns_y') not in fd.get('groupby')): - raise Exception( - "[Longitude] and [Latitude] columns must be present in [Group By]") + raise Exception(_( + "[Longitude] and [Latitude] columns must be present in [Group By]")) return d def get_data(self, df): From 51f6a81dfe39f99f7ca56af90b4cfd6530af9850 Mon Sep 17 00:00:00 2001 From: Rogan Date: Thu, 10 Aug 2017 11:44:27 +0800 Subject: [PATCH 2/2] fix error --- superset/viz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset/viz.py b/superset/viz.py index c1502975c9264..f37f8a74bfb69 100644 --- a/superset/viz.py +++ b/superset/viz.py @@ -392,7 +392,7 @@ def query_obj(self): if ( any(v in groupby for v in columns) or any(v in columns for v in groupby)): - raise Exception(_("'Group By' and 'Columns' can't overlap") + raise Exception(_("'Group By' and 'Columns' can't overlap")) return d def get_data(self, df):