rubypollev commented on issue #8183: How to pass time filters to SQL Lab queries
URL: 
https://github.com/apache/incubator-superset/issues/8183#issuecomment-536097091
 
 
   @GiannisDimitriou Sure. 
   ```
   from superset.utils import core as utils
   ...
   
   def time_filter(default: Optional[str] = None) -> Optional[Any]:
       form_data = request.form.get("form_data")
   
       if isinstance(form_data, str):
           form_data = json.loads(form_data)
           extra_filters = form_data.get("extra_filters") or {}
           time_range = [f["val"] for f in extra_filters if f["col"] == 
"__time_range"]
           time_range = time_range[0] if time_range else None
   
           since, until = utils.get_since_until(time_range)
           time_format = '%Y-%m-%d %H:%M:%S'
   
           until = until.strftime(time_format)
           if not since:
               return '<= \'{}\''.format(until)
           since = since.strftime(time_format)
           return 'BETWEEN \'{}\' AND \'{}\''.format(since, until)
       return default
   
   
   JINJA_CONTEXT_ADDONS = {
       'time_filter': time_filter
   }
   ```
   
   This is what I do, and it works for me. I do make some assumptions (taking 
the first time range filter if there are multiple, for example). 
   
   @villebro & other maintainers: do we want this? I can open a PR with 
`time_filter` as a default Jinja function if we do.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to