def get_form_data( slice_id: Optional[int] = None, use_slice_data: bool = False ) -> Tuple[Dict[str, Any], Optional[Slice]]: form_data = {} request_form_data = request.form.get("form_data") request_args_data = request.args.get("form_data") if request_form_data: form_data.update(json.loads(request_form_data)) # request params can overwrite the body if request_args_data: form_data.update(json.loads(request_args_data)) # Fallback to using the Flask globals (used for cache warmup) if defined. if not form_data and hasattr(g, "form_data"): form_data = getattr(g, "form_data") url_id = request.args.get("r") if url_id: saved_url = db.session.query(models.Url).filter_by(id=url_id).first() if saved_url: url_str = parse.unquote_plus(saved_url.url.split("?")[1][10:], encoding="utf-8") url_form_data = json.loads(url_str) # allow form_date in request override saved url url_form_data.update(form_data) form_data = url_form_data form_data = { k: v for k, v in form_data.items() if k not in FORM_DATA_KEY_BLACKLIST } # When a slice_id is present, load from DB and override # the form_data from the DB with the other form_data provided slice_id = form_data.get("slice_id") or slice_id slc = None # Check if form data only contains slice_id, additional filters and viz type valid_keys = ["slice_id", "extra_filters", "adhoc_filters", "viz_type"] valid_slice_id = all(key in valid_keys for key in form_data) # Include the slice_form_data if request from explore or slice calls # or if form_data only contains slice_id and additional filters if slice_id and (use_slice_data or valid_slice_id): slc = db.session.query(Slice).filter_by(id=slice_id).one_or_none() if slc: slice_form_data = slc.form_data.copy() slice_form_data.update(form_data) form_data = slice_form_data update_time_range(form_data) if app.config["SIP_15_ENABLED"]: form_data["time_range_endpoints"] = get_time_range_endpoints( form_data, slc, slice_id) return form_data, slc
def get_form_data(slice_id=None, use_slice_data=False): form_data = {} post_data = request.form.get("form_data") request_args_data = request.args.get("form_data") # Supporting POST if post_data: form_data.update(json.loads(post_data)) # request params can overwrite post body if request_args_data: form_data.update(json.loads(request_args_data)) url_id = request.args.get("r") if url_id: saved_url = db.session.query(models.Url).filter_by(id=url_id).first() if saved_url: url_str = parse.unquote_plus(saved_url.url.split("?")[1][10:], encoding="utf-8", errors=None) url_form_data = json.loads(url_str) # allow form_date in request override saved url url_form_data.update(form_data) form_data = url_form_data form_data = { k: v for k, v in form_data.items() if k not in FORM_DATA_KEY_BLACKLIST } # When a slice_id is present, load from DB and override # the form_data from the DB with the other form_data provided slice_id = form_data.get("slice_id") or slice_id slc = None # Check if form data only contains slice_id, additional filters and viz type valid_keys = ["slice_id", "extra_filters", "adhoc_filters", "viz_type"] valid_slice_id = all(key in valid_keys for key in form_data) # Include the slice_form_data if request from explore or slice calls # or if form_data only contains slice_id and additional filters if slice_id and (use_slice_data or valid_slice_id): slc = db.session.query( models.Slice).filter_by(id=slice_id).one_or_none() if slc: slice_form_data = slc.form_data.copy() slice_form_data.update(form_data) form_data = slice_form_data update_time_range(form_data) if app.config["SIP_15_ENABLED"]: form_data["time_range_endpoints"] = get_time_range_endpoints( form_data, slc) return form_data, slc
def upgrade(): bind = op.get_bind() session = db.Session(bind=bind) for slc in session.query(Slice).all(): try: form_data = json.loads(slc.params) update_time_range(form_data) slc.params = json.dumps(form_data, sort_keys=True) except Exception as ex: logging.exception(ex) session.commit()
def get_form_data(slice_id=None, use_slice_data=False): form_data = {} post_data = request.form.get('form_data') request_args_data = request.args.get('form_data') # Supporting POST if post_data: form_data.update(json.loads(post_data)) # request params can overwrite post body if request_args_data: form_data.update(json.loads(request_args_data)) url_id = request.args.get('r') if url_id: saved_url = db.session.query(models.Url).filter_by(id=url_id).first() if saved_url: url_str = parse.unquote_plus( saved_url.url.split('?')[1][10:], encoding='utf-8', errors=None) url_form_data = json.loads(url_str) # allow form_date in request override saved url url_form_data.update(form_data) form_data = url_form_data form_data = { k: v for k, v in form_data.items() if k not in FORM_DATA_KEY_BLACKLIST } # When a slice_id is present, load from DB and override # the form_data from the DB with the other form_data provided slice_id = form_data.get('slice_id') or slice_id slc = None # Check if form data only contains slice_id, additional filters and viz type valid_keys = ['slice_id', 'extra_filters', 'adhoc_filters', 'viz_type'] valid_slice_id = all(key in valid_keys for key in form_data) # Include the slice_form_data if request from explore or slice calls # or if form_data only contains slice_id and additional filters if slice_id and (use_slice_data or valid_slice_id): slc = db.session.query(models.Slice).filter_by(id=slice_id).one_or_none() if slc: slice_form_data = slc.form_data.copy() slice_form_data.update(form_data) form_data = slice_form_data update_time_range(form_data) return form_data, slc
def query_form_data(self) -> FlaskResponse: """ Get the formdata stored in the database for existing slice. params: slice_id: integer """ form_data = {} slice_id = request.args.get("slice_id") if slice_id: slc = db.session.query(Slice).filter_by(id=slice_id).one_or_none() if slc: form_data = slc.form_data.copy() update_time_range(form_data) return json.dumps(form_data)
def form_data(self): form_data = {} try: form_data = json.loads(self.params) except Exception as e: logging.error("Malformed json in slice's params") logging.exception(e) form_data.update({ 'slice_id': self.id, 'viz_type': self.viz_type, 'datasource': '{}__{}'.format( self.datasource_id, self.datasource_type), }) if self.cache_timeout: form_data['cache_timeout'] = self.cache_timeout update_time_range(form_data) return form_data
def form_data(self): form_data = {} try: form_data = json.loads(self.params) except Exception as e: logging.error("Malformed json in slice's params") logging.exception(e) form_data.update( { "slice_id": self.id, "viz_type": self.viz_type, "datasource": "{}__{}".format(self.datasource_id, self.datasource_type), } ) if self.cache_timeout: form_data["cache_timeout"] = self.cache_timeout update_time_range(form_data) return form_data
def form_data(self) -> Dict[str, Any]: form_data: Dict[str, Any] = {} try: form_data = json.loads(self.params) except Exception as ex: # pylint: disable=broad-except logger.error("Malformed json in slice's params") logger.exception(ex) form_data.update( { "slice_id": self.id, "viz_type": self.viz_type, "datasource": "{}__{}".format(self.datasource_id, self.datasource_type), } ) if self.cache_timeout: form_data["cache_timeout"] = self.cache_timeout update_time_range(form_data) return form_data
def get_form_data( # pylint: disable=too-many-locals slice_id: Optional[int] = None, use_slice_data: bool = False) -> Tuple[Dict[str, Any], Optional[Slice]]: form_data: Dict[str, Any] = {} # chart data API requests are JSON request_json_data = (request.json["queries"][0] if request.is_json and "queries" in request.json else None) add_sqllab_custom_filters(form_data) request_form_data = request.form.get("form_data") request_args_data = request.args.get("form_data") if request_json_data: form_data.update(request_json_data) if request_form_data: parsed_form_data = loads_request_json(request_form_data) # some chart data api requests are form_data queries = parsed_form_data.get("queries") if isinstance(queries, list): form_data.update(queries[0]) else: form_data.update(parsed_form_data) # request params can overwrite the body if request_args_data: form_data.update(loads_request_json(request_args_data)) # Fallback to using the Flask globals (used for cache warmup) if defined. if not form_data and hasattr(g, "form_data"): form_data = getattr(g, "form_data") url_id = request.args.get("r") if url_id: saved_url = db.session.query(models.Url).filter_by(id=url_id).first() if saved_url: url_str = parse.unquote_plus(saved_url.url.split("?")[1][10:], encoding="utf-8") url_form_data = loads_request_json(url_str) # allow form_date in request override saved url url_form_data.update(form_data) form_data = url_form_data form_data = { k: v for k, v in form_data.items() if k not in REJECTED_FORM_DATA_KEYS } # When a slice_id is present, load from DB and override # the form_data from the DB with the other form_data provided slice_id = form_data.get("slice_id") or slice_id slc = None # Check if form data only contains slice_id, additional filters and viz type valid_keys = ["slice_id", "extra_filters", "adhoc_filters", "viz_type"] valid_slice_id = all(key in valid_keys for key in form_data) # Include the slice_form_data if request from explore or slice calls # or if form_data only contains slice_id and additional filters if slice_id and (use_slice_data or valid_slice_id): slc = db.session.query(Slice).filter_by(id=slice_id).one_or_none() if slc: slice_form_data = slc.form_data.copy() slice_form_data.update(form_data) form_data = slice_form_data update_time_range(form_data) if app.config["SIP_15_ENABLED"]: form_data["time_range_endpoints"] = get_time_range_endpoints( form_data, slc, slice_id) return form_data, slc