def _get_filtered_expression(self, expression, object_class, tgt_class, query): """Filter query according to expression.""" with benchmark("Parse filter query: _get_ids > _build_expression"): filter_expression = custom_operators.build_expression( expression, object_class, tgt_class, self.query) if filter_expression is not None: query = query.filter(filter_expression) return query
def _get_ids(self, object_query): """Get a set of ids of objects described in the filters.""" object_name = object_query["object_name"] expression = object_query.get("filters", {}).get("expression") if expression is None: return set() object_class = inflector.get_model(object_name) query = db.session.query(object_class.id) tgt_class = object_class if object_name == "Snapshot": child_type = self._get_snapshot_child_type(object_query) tgt_class = getattr(models.all_models, child_type, object_class) requested_permissions = object_query.get("permissions", "read") with benchmark("Get permissions: _get_ids > _get_type_query"): type_query = self._get_type_query(object_class, requested_permissions) if type_query is not None: query = query.filter(type_query) with benchmark("Parse filter query: _get_ids > _build_expression"): filter_expression = custom_operators.build_expression( expression, object_class, tgt_class, self.query ) if filter_expression is not None: query = query.filter(filter_expression) if object_query.get("order_by"): with benchmark("Sorting: _get_ids > order_by"): query = self._apply_order_by( object_class, query, object_query["order_by"], tgt_class, ) with benchmark("Apply limit"): limit = object_query.get("limit") if limit: ids, total = self._apply_limit(query, limit) else: ids = [obj.id for obj in query] total = len(ids) object_query["total"] = total if hasattr(flask.g, "similar_objects_query"): # delete similar_objects_query for the case when several queries are # POSTed in one request, the first one filters by similarity and the # second one doesn't but tries to sort by __similarity__ delattr(flask.g, "similar_objects_query") return ids
def _get_ids(self, object_query): """Get a set of ids of objects described in the filters.""" object_name = object_query["object_name"] expression = object_query.get("filters", {}).get("expression") if expression is None: return set() object_class = inflector.get_model(object_name) if object_class is None: return set() query = db.session.query(object_class.id) tgt_class = object_class if object_name == "Snapshot": child_type = self._get_snapshot_child_type(object_query) tgt_class = getattr(models.all_models, child_type, object_class) requested_permissions = object_query.get("permissions", "read") with benchmark("Get permissions: _get_ids > _get_type_query"): type_query = self._get_type_query(object_class, requested_permissions) if type_query is not None: query = query.filter(type_query) with benchmark("Parse filter query: _get_ids > _build_expression"): filter_expression = custom_operators.build_expression( expression, object_class, tgt_class, self.query ) if filter_expression is not None: query = query.filter(filter_expression) if object_query.get("order_by"): with benchmark("Sorting: _get_ids > order_by"): query = pagination.apply_order_by( object_class, query, object_query["order_by"], tgt_class, ) with benchmark("Apply limit"): limit = object_query.get("limit") if limit: limit_query = pagination.apply_limit(query, limit) total = pagination.get_total_count(query) ids = [obj.id for obj in limit_query] else: ids = [obj.id for obj in query] total = len(ids) object_query["total"] = total return ids
def _get_ids(self, object_query): """Get a set of ids of objects described in the filters.""" object_name = object_query["object_name"] expression = object_query.get("filters", {}).get("expression") if expression is None: return set() object_class = inflector.get_model(object_name) query = db.session.query(object_class.id) tgt_class = object_class if object_name == "Snapshot": child_type = self._get_snapshot_child_type(object_query) tgt_class = getattr(models.all_models, child_type, object_class) requested_permissions = object_query.get("permissions", "read") with benchmark("Get permissions: _get_ids > _get_type_query"): type_query = self._get_type_query(object_class, requested_permissions) if type_query is not None: query = query.filter(type_query) with benchmark("Parse filter query: _get_ids > _build_expression"): filter_expression = custom_operators.build_expression( expression, object_class, tgt_class, self.query) if filter_expression is not None: query = query.filter(filter_expression) if object_query.get("order_by"): with benchmark("Sorting: _get_ids > order_by"): query = self._apply_order_by( object_class, query, object_query["order_by"], tgt_class, ) with benchmark("Apply limit"): limit = object_query.get("limit") if limit: ids, total = self._apply_limit(query, limit) else: ids = [obj.id for obj in query] total = len(ids) object_query["total"] = total if hasattr(flask.g, "similar_objects_query"): # delete similar_objects_query for the case when several queries are # POSTed in one request, the first one filters by similarity and the # second one doesn't but tries to sort by __similarity__ delattr(flask.g, "similar_objects_query") return ids