Ejemplo n.º 1
0
    def as_marshmallow_schema(self,
                              params=None,
                              base_schema_cls=MaSchema,
                              check_unknown_fields=True,
                              mongo_world=False):
        """
        Return a pure-marshmallow version of this schema class.

        :param params: Per-field dict to pass parameters to their field creation.
        :param base_schema_cls: Class the schema will inherit from (
            default: :class:`marshmallow.Schema`).
        :param check_unknown_fields: Unknown fields are considered as errors (default: True).
        :param mongo_world: If True the schema will work against the mongo world
            instead of the OO world (default: False).
        """
        params = params or {}
        nmspc = {
            name: field.as_marshmallow_field(params=params.get(name),
                                             mongo_world=mongo_world)
            for name, field in self.fields.items()
        }
        name = 'Marshmallow%s' % type(self).__name__
        if check_unknown_fields:
            nmspc['_%s__check_unknown_fields' % name] = validates_schema(
                pass_original=True)(schema_validator_check_unknown_fields)
        # By default OO world returns `missing` fields as `None`,
        # disable this behavior here to let marshmallow deals with it
        if not mongo_world:
            nmspc['get_attribute'] = schema_from_umongo_get_attribute
        return type(name, (base_schema_cls, ), nmspc)
Ejemplo n.º 2
0
class BaseSchema(MaSchema):
    """
    All schema used in umongo should inherit from this base schema
    """

    __check_unknown_fields = validates_schema(
        pass_original=True)(schema_validator_check_unknown_fields)

    def map_to_field(self, func):
        """
        Apply a function to every field in the schema

        >>> def func(mongo_path, path, field):
        ...     pass
        """
        for name, field in self.fields.items():
            mongo_path = field.attribute or name
            func(mongo_path, name, field)
            if hasattr(field, 'map_to_field'):
                field.map_to_field(mongo_path, name, func)

    def as_marshmallow_schema(self,
                              params=None,
                              base_schema_cls=MaSchema,
                              check_unknown_fields=True,
                              mongo_world=False):
        """
        Return a pure-marshmallow version of this schema class.

        :param params: Per-field dict to pass parameters to their field creation.
        :param base_schema_cls: Class the schema will inherit from (
            default: :class:`marshmallow.Schema`).
        :param check_unknown_fields: Unknown fields are considered as errors (default: True).
        :param mongo_world: If True the schema will work against the mongo world
            instead of the OO world (default: False).
        """
        params = params or {}
        nmspc = {
            name: field.as_marshmallow_field(params=params.get(name),
                                             mongo_world=mongo_world)
            for name, field in self.fields.items()
        }
        name = 'Marshmallow%s' % type(self).__name__
        if check_unknown_fields:
            nmspc['_%s__check_unknown_fields' % name] = validates_schema(
                pass_original=True)(schema_validator_check_unknown_fields)

        if hasattr(self, '_schema_model'):
            nmspc['_schema_model'] = self._schema_model

        # By default OO world returns `missing` fields as `None`,
        # disable this behavior here to let marshmallow deals with it
        if not mongo_world:
            nmspc['get_attribute'] = schema_from_umongo_get_attribute
        return type(name, (base_schema_cls, ), nmspc)
Ejemplo n.º 3
0
class SchemaFromUmongo(MaSchema):
    """
    Custom :class:`marshmallow.Schema` subclass providing unknown fields
    checking and custom get_attribute for umongo documents.

    .. note: It is not mandatory to use this schema with umongo document.
        This is just a helper providing usefull behaviors.
    """

    __check_unknown_fields = validates_schema(pass_original=True)(
        schema_validator_check_unknown_fields)
    get_attribute = schema_from_umongo_get_attribute
Ejemplo n.º 4
0
    def as_marshmallow_schema(self,
                              params=None,
                              base_schema_cls=MaSchema,
                              check_unknown_fields=True,
                              mongo_world=False,
                              meta=None):
        """
        Return a pure-marshmallow version of this schema class.

        :param params: Per-field dict to pass parameters to their field creation.
        :param base_schema_cls: Class the schema will inherit from (
            default: :class:`marshmallow.Schema`).
        :param check_unknown_fields: Unknown fields are considered as errors (default: True).
        :param mongo_world: If True the schema will work against the mongo world
            instead of the OO world (default: False).
        :param meta: Optional dict with attributes for the schema's Meta class.
        """
        params = params or {}
        # Use hashable parameters as cache dict key and dict parameters for manual comparison
        cache_key = (self.__class__, base_schema_cls, check_unknown_fields,
                     mongo_world)
        cache_modifiers = (params, meta)
        if cache_key in self._marshmallow_schemas_cache:
            for modifiers, ma_schema in self._marshmallow_schemas_cache[
                    cache_key]:
                if modifiers == cache_modifiers:
                    return ma_schema
        nmspc = {
            name: field.as_marshmallow_field(
                params=params.get(name),
                base_schema_cls=base_schema_cls,
                check_unknown_fields=check_unknown_fields,
                mongo_world=mongo_world)
            for name, field in self.fields.items()
        }
        name = 'Marshmallow%s' % type(self).__name__
        if check_unknown_fields:
            nmspc['_%s__check_unknown_fields' % name] = validates_schema(
                pass_original=True)(schema_validator_check_unknown_fields)
        # By default OO world returns `missing` fields as `None`,
        # disable this behavior here to let marshmallow deal with it
        if not mongo_world:
            nmspc['get_attribute'] = schema_from_umongo_get_attribute
        if meta:
            nmspc['Meta'] = type('Meta', (base_schema_cls.Meta, ), meta)
        ma_schema = type(name, (base_schema_cls, ), nmspc)
        self._marshmallow_schemas_cache.setdefault(cache_key, []).append(
            (cache_modifiers, ma_schema))
        return ma_schema
Ejemplo n.º 5
0
 class CheckUnknownSchema(marshmallow.Schema):
     __check_unknown_fields = marshmallow.validates_schema(
         pass_original=True)(schema_validator_check_unknown_fields)
     a = marshmallow.fields.Int()