Ejemplo n.º 1
0
    def op(self):
        """Return the operator of the node

        :return str: the operator to use in the filter
        """
        try:
            return self.filter_["op"]
        except KeyError:
            raise InvalidFilters("Can't find op of a filter")
Ejemplo n.º 2
0
    def name(self):
        """Return the name of the node or raise a BadRequest exception

        :return str: the name of the field to filter on
        """
        name = self.filter_.get("name")

        if name is None:
            raise InvalidFilters("Can't find name of a filter")

        if "__" in name:
            name = name.split("__")[0]

        if name not in self.schema._declared_fields:
            raise InvalidFilters(
                "{} has no attribute {}".format(self.schema.__name__, name)
            )

        return name
Ejemplo n.º 3
0
    def value(self):
        """Get the value to filter on

        :return: the value to filter on
        """
        if self.filter_.get("field") is not None:
            try:
                result = getattr(self.model, self.filter_["field"])
            except AttributeError:
                raise InvalidFilters(
                    "{} has no attribute {}".format(
                        self.model.__name__, self.filter_["field"]
                    )
                )
            else:
                return result
        else:
            if "val" not in self.filter_:
                raise InvalidFilters("Can't find value or field in a filter")

            return self.filter_["val"]
Ejemplo n.º 4
0
    def operator(self):
        """Get the function operator from his name

        :return callable: a callable to make operation on a column
        """
        operators = (self.op, self.op + "_", "__" + self.op + "__")

        for op in operators:
            if hasattr(self.column, op):
                return op

        raise InvalidFilters("{} has no operator {}".format(self.column.key, self.op))
Ejemplo n.º 5
0
    def related_schema(self):
        """Get the related schema of a relationship field

        :return Schema: the related schema
        """
        relationship_field = self.name

        if relationship_field not in get_relationships(self.schema):
            raise InvalidFilters(
                "{} has no relationship attribute {}".format(
                    self.schema.__name__, relationship_field
                )
            )

        return self.schema._declared_fields[relationship_field].schema.__class__
Ejemplo n.º 6
0
    def filters(self):
        """Return filters from query string.

        :return list: filter information
        """
        results = []
        filters = self.qs.get("filter")
        if filters is not None:
            try:
                results.extend(json.loads(filters))
            except (ValueError, TypeError):
                raise InvalidFilters("Parse error")
        if self._get_key_values("filter["):
            results.extend(
                self._simple_filters(self._get_key_values("filter[")))
        return results
Ejemplo n.º 7
0
    def related_model(self):
        """Get the related model of a relationship field

        :return DeclarativeMeta: the related model
        """
        relationship_field = self.name

        if relationship_field not in get_relationships(self.schema):
            raise InvalidFilters(
                "{} has no relationship attribute {}".format(
                    self.schema.__name__, relationship_field
                )
            )

        return getattr(
            self.model, get_model_field(self.schema, relationship_field)
        ).property.mapper.class_
Ejemplo n.º 8
0
    def column(self):
        """Get the column object

        :param DeclarativeMeta model: the model
        :param str field: the field
        :return InstrumentedAttribute: the column to filter on
        """
        field = self.name

        model_field = get_model_field(self.schema, field)

        try:
            return getattr(self.model, model_field)
        except AttributeError:
            raise InvalidFilters(
                "{} has no attribute {}".format(self.model.__name__, model_field)
            )