From bd4a4c2753d9411c0032f4bd24ea36faba8cd3f5 Mon Sep 17 00:00:00 2001 From: Rogan Date: Fri, 11 Aug 2017 11:57:05 +0800 Subject: [PATCH] add `_()` to Exception messages (#3034) * add `_()` to Exception messages * fix error --- 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 | 73 +++++++++++++++--------------- 5 files changed, 44 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 b836a153e1..6dccdf874a 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -578,9 +578,9 @@ class SqlaTable(Model, BaseDatasource): 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 4aa577d7a5..6c9b23340f 100644 --- a/superset/connectors/sqla/views.py +++ b/superset/connectors/sqla/views.py @@ -239,11 +239,11 @@ class TableModelView(DatasourceModelView, DeleteMixin): # noqa 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 f2cbd9bd0e..d03625821c 100644 --- a/superset/views/base.py +++ b/superset/views/base.py @@ -5,6 +5,7 @@ import traceback 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 b2a3be4fbc..83acd8aff7 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -2034,7 +2034,7 @@ class Superset(BaseSupersetView): 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 b7b72ba3f5..c2d65e6c85 --- a/superset/viz.py +++ b/superset/viz.py @@ -46,7 +46,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") @@ -155,7 +155,7 @@ class BaseViz(object): 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 @@ -331,9 +331,9 @@ class TableViz(BaseViz): (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): @@ -341,9 +341,9 @@ class TableViz(BaseViz): 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') @@ -389,13 +389,13 @@ class PivotTableViz(BaseViz): 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(""""Group By" and "Columns" can't overlap""") + raise Exception(_("'Group By' and 'Columns' can't overlap")) return d def get_data(self, df): @@ -683,7 +683,7 @@ class BubbleViz(NVD3Viz): 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): @@ -735,7 +735,7 @@ class BulletViz(NVD3Viz): 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): @@ -766,7 +766,7 @@ class BigNumberViz(BaseViz): 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 @@ -795,7 +795,7 @@ class BigNumberTotalViz(BaseViz): 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 @@ -861,7 +861,7 @@ class NVD3TimeSeriesViz(NVD3Viz): 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, @@ -951,12 +951,12 @@ class NVD3DualLineViz(NVD3Viz): 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=''): @@ -998,7 +998,7 @@ class NVD3DualLineViz(NVD3Viz): 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') @@ -1069,7 +1069,7 @@ class HistogramViz(BaseViz): '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 @@ -1094,11 +1094,12 @@ class DistributionBarViz(DistributionPieViz): len(d['groupby']) < len(fd.get('groupby') or []) + len(fd.get('columns') or []) ): - raise Exception("Can't have overlap between Series and Breakdowns") + raise Exception( + _("Can't have overlap between Series and Breakdowns")) if not fd.get('metrics'): - raise Exception("Pick at least one metric") + raise Exception(_("Pick at least one metric")) if not fd.get('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): @@ -1191,7 +1192,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 @@ -1222,9 +1223,9 @@ class SankeyViz(BaseViz): 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 @@ -1240,7 +1241,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 @@ -1374,7 +1375,7 @@ class FilterBoxViz(BaseViz): 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 @@ -1520,8 +1521,8 @@ class MapboxViz(BaseViz): 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': @@ -1533,18 +1534,18 @@ class MapboxViz(BaseViz): 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):