You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by gr...@apache.org on 2018/11/06 00:30:12 UTC

[incubator-superset] branch master updated: Adding backwards compatable check to add ago to since if it doesn't exist (#6269)

This is an automated email from the ASF dual-hosted git repository.

graceguo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new c7f8abc  Adding backwards compatable check to add ago to since if it doesn't exist (#6269)
c7f8abc is described below

commit c7f8abc6c51f2e8204f88d516bdb73006d011d16
Author: michellethomas <mi...@gmail.com>
AuthorDate: Mon Nov 5 16:30:04 2018 -0800

    Adding backwards compatable check to add ago to since if it doesn't exist (#6269)
---
 superset/utils/core.py | 23 +++++++++++++++++++----
 superset/views/core.py | 11 +++++------
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/superset/utils/core.py b/superset/utils/core.py
index a15f382..cecd082 100644
--- a/superset/utils/core.py
+++ b/superset/utils/core.py
@@ -923,6 +923,8 @@ def get_since_until(form_data):
         time_range = form_data['time_range']
         if separator in time_range:
             since, until = time_range.split(separator, 1)
+            if since and since not in common_time_frames:
+                since = add_ago_to_since(since)
             since = parse_human_datetime(since)
             until = parse_human_datetime(until)
         elif time_range in common_time_frames:
@@ -940,16 +942,29 @@ def get_since_until(form_data):
     else:
         since = form_data.get('since', '')
         if since:
-            since_words = since.split(' ')
-            grains = ['days', 'years', 'hours', 'day', 'year', 'weeks']
-            if len(since_words) == 2 and since_words[1] in grains:
-                since += ' ago'
+            since = add_ago_to_since(since)
         since = parse_human_datetime(since)
         until = parse_human_datetime(form_data.get('until', 'now'))
 
     return since, until
 
 
+def add_ago_to_since(since):
+    """
+    Backwards compatibility hack. Without this slices with since: 7 days will
+    be treated as 7 days in the future.
+
+    :param str since:
+    :returns: Since with ago added if necessary
+    :rtype: str
+    """
+    since_words = since.split(' ')
+    grains = ['days', 'years', 'hours', 'day', 'year', 'weeks']
+    if (len(since_words) == 2 and since_words[1] in grains):
+        since += ' ago'
+    return since
+
+
 def convert_legacy_filters_into_adhoc(fd):
     mapping = {'having': 'having_filters', 'where': 'filters'}
 
diff --git a/superset/views/core.py b/superset/views/core.py
index 2af8514..a16bceb 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -1036,12 +1036,11 @@ class Superset(BaseSupersetView):
             # special treat for since/until and time_range parameter:
             # we need to breakdown time_range into since/until so request parameters
             # has precedence over slice parameters for time fields.
-            if 'time_range' in form_data:
-                form_data['since'], separator, form_data['until'] = \
-                    form_data['time_range'].partition(' : ')
-            if 'time_range' in slice_form_data:
-                slice_form_data['since'], separator, slice_form_data['until'] = \
-                    slice_form_data['time_range'].partition(' : ')
+            if 'since' in form_data or 'until' in form_data:
+                form_data['since'], form_data['until'] = \
+                    utils.get_since_until(form_data)
+                slice_form_data['since'], slice_form_data['until'] = \
+                    utils.get_since_until(slice_form_data)
             slice_form_data.update(form_data)
             form_data = slice_form_data