From 888544118680080ce080ebe3b2593446195cb973 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Wed, 15 Jun 2016 10:01:33 +0200 Subject: [PATCH] Make time grains translatable Fix #616 --- caravel/forms.py | 3 ++- caravel/models.py | 48 +++++++++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/caravel/forms.py b/caravel/forms.py index efce723bec9f8..e42a2a1d62e0a 100644 --- a/caravel/forms.py +++ b/caravel/forms.py @@ -835,10 +835,11 @@ def add_to_form(attrs): grains = viz.datasource.database.grains() if grains: + grains_choices = [(grain.name, grain.label) for grain in grains] time_fields = ('granularity_sqla', 'time_grain_sqla') self.field_dict['time_grain_sqla'] = SelectField( _('Time Grain'), - choices=self.choicify((grain.name for grain in grains)), + choices=grains_choices, default="Time Column", description=_( "The time granularity for the visualization. This " diff --git a/caravel/models.py b/caravel/models.py index aec3d11f57829..51d71876b40e0 100644 --- a/caravel/models.py +++ b/caravel/models.py @@ -23,7 +23,7 @@ from flask_appbuilder import Model from flask_appbuilder.models.mixins import AuditMixin from flask_appbuilder.models.decorators import renders -from flask_babelpkg import gettext as _ +from flask_babelpkg import lazy_gettext as _ from pydruid.client import PyDruid from pydruid.utils.filters import Dimension, Filter @@ -395,42 +395,42 @@ def grains(self): each database has slightly different but similar datetime functions, this allows a mapping between database engines and actual functions. """ - Grain = namedtuple('Grain', 'name function') + Grain = namedtuple('Grain', 'name label function') db_time_grains = { 'presto': ( - Grain('Time Column', '{col}'), - Grain('week', "date_trunc('week', CAST({col} AS DATE))"), - Grain('month', "date_trunc('month', CAST({col} AS DATE))"), - Grain("week_ending_saturday", "date_add('day', 5, " + Grain('Time Column', _('Time Column'), '{col}'), + Grain('week', _('week'), "date_trunc('week', CAST({col} AS DATE))"), + Grain('month', _('month'), "date_trunc('month', CAST({col} AS DATE))"), + Grain("week_ending_saturday", _('week_ending_saturday'), "date_add('day', 5, " "date_trunc('week', date_add('day', 1, CAST({col} AS DATE))))"), - Grain("week_start_sunday", "date_add('day', -1, " + Grain("week_start_sunday", _('week_start_sunday'), "date_add('day', -1, " "date_trunc('week', date_add('day', 1, CAST({col} AS DATE))))") ), 'mysql': ( - Grain('Time Column', '{col}'), - Grain("hour", "DATE_ADD(DATE({col}), " + Grain('Time Column', _('Time Column'), '{col}'), + Grain("hour", _('hour'), "DATE_ADD(DATE({col}), " "INTERVAL HOUR({col}) HOUR)"), - Grain('day', 'DATE({col})'), - Grain("week", "DATE(DATE_SUB({col}, " + Grain('day', _('day'), 'DATE({col})'), + Grain("week", _('week'), "DATE(DATE_SUB({col}, " "INTERVAL DAYOFWEEK({col}) - 1 DAY))"), - Grain("month", "DATE(DATE_SUB({col}, " + Grain("month", _('month'), "DATE(DATE_SUB({col}, " "INTERVAL DAYOFMONTH({col}) - 1 DAY))"), ), 'sqlite': ( - Grain('Time Column', '{col}'), - Grain('day', 'DATE({col})'), - Grain("week", "DATE({col}, -strftime('%w', {col}) || ' days')"), - Grain("month", "DATE({col}, -strftime('%d', {col}) || ' days')"), + Grain('Time Column', _('Time Column'), '{col}'), + Grain('day', _('day'), 'DATE({col})'), + Grain("week", _('week'), "DATE({col}, -strftime('%w', {col}) || ' days')"), + Grain("month", _('month'), "DATE({col}, -strftime('%d', {col}) || ' days')"), ), 'postgresql': ( - Grain("Time Column", "{col}"), - Grain("second", "DATE_TRUNC('second', {col})"), - Grain("minute", "DATE_TRUNC('minute', {col})"), - Grain("hour", "DATE_TRUNC('hour', {col})"), - Grain("day", "DATE_TRUNC('day', {col})"), - Grain("week", "DATE_TRUNC('week', {col})"), - Grain("month", "DATE_TRUNC('month', {col})"), - Grain("year", "DATE_TRUNC('year', {col})"), + Grain("Time Column", _('Time Column'), "{col}"), + Grain("second", _('second'), "DATE_TRUNC('second', {col})"), + Grain("minute", _('minute'), "DATE_TRUNC('minute', {col})"), + Grain("hour", _('hour'), "DATE_TRUNC('hour', {col})"), + Grain("day", _('day'), "DATE_TRUNC('day', {col})"), + Grain("week", _('week'), "DATE_TRUNC('week', {col})"), + Grain("month", _('month'), "DATE_TRUNC('month', {col})"), + Grain("year", _('year'), "DATE_TRUNC('year', {col})"), ), } db_time_grains['redshift'] = db_time_grains['postgresql']