src.utils_flask_sqla_geo.schema

Module Contents

Classes

GeometrySchema

Base schema class with which to define custom schemas.

FeatureSchema

Base schema class with which to define custom schemas.

FeatureCollectionSchema

Base schema class with which to define custom schemas.

GeometryField

Basic field from which other fields should extend. It applies no

GeoModelConverter

Model converter for models with geometric fields.

GeoAlchemyAutoSchemaOpts

Options class for GeoAlchemyAutoSchema.

GeoAlchemyAutoSchema

Auto schema with support for geometric fields and geojson generation.

class src.utils_flask_sqla_geo.schema.GeometrySchema(*, only: marshmallow.types.StrSequenceOrSet | None = None, exclude: marshmallow.types.StrSequenceOrSet = (), many: bool = False, context: dict | None = None, load_only: marshmallow.types.StrSequenceOrSet = (), dump_only: marshmallow.types.StrSequenceOrSet = (), partial: bool | marshmallow.types.StrSequenceOrSet | None = None, unknown: str | None = None)[source]

Bases: marshmallow.Schema

Base schema class with which to define custom schemas.

Example usage:

import datetime as dt
from dataclasses import dataclass

from marshmallow import Schema, fields


@dataclass
class Album:
    title: str
    release_date: dt.date


class AlbumSchema(Schema):
    title = fields.Str()
    release_date = fields.Date()


album = Album("Beggars Banquet", dt.date(1968, 12, 6))
schema = AlbumSchema()
data = schema.dump(album)
data  # {'release_date': '1968-12-06', 'title': 'Beggars Banquet'}
Paramètres:
  • only – Whitelist of the declared fields to select when instantiating the Schema. If None, all fields are used. Nested fields can be represented with dot delimiters.

  • exclude – Blacklist of the declared fields to exclude when instantiating the Schema. If a field appears in both only and exclude, it is not used. Nested fields can be represented with dot delimiters.

  • many – Should be set to True if obj is a collection so that the object will be serialized to a list.

  • context – Optional context passed to fields.Method and fields.Function fields.

  • load_only – Fields to skip during serialization (write-only fields)

  • dump_only – Fields to skip during deserialization (read-only fields)

  • partial – Whether to ignore missing fields and not require any fields declared. Propagates down to Nested fields as well. If its value is an iterable, only missing fields listed in that iterable will be ignored. Use dot delimiters to specify nested fields.

  • unknown – Whether to exclude, include, or raise an error for unknown fields in the data. Use EXCLUDE, INCLUDE or RAISE.

Modifié dans la version 3.0.0: prefix parameter removed.

Modifié dans la version 2.0.0: __validators__, __preprocessors__, and __data_handlers__ are removed in favor of marshmallow.decorators.validates_schema, marshmallow.decorators.pre_load and marshmallow.decorators.post_dump. __accessor__ and __error_handler__ are deprecated. Implement the handle_error and get_attribute methods instead.

schema_map[source]
type[source]
load(data, *, many=None, **kwargs)[source]

Deserialize a data structure to an object defined by this Schema’s fields.

Paramètres:
  • data – The data to deserialize.

  • many – Whether to deserialize data as a collection. If None, the value for self.many is used.

  • partial – Whether to ignore missing fields and not require any fields declared. Propagates down to Nested fields as well. If its value is an iterable, only missing fields listed in that iterable will be ignored. Use dot delimiters to specify nested fields.

  • unknown – Whether to exclude, include, or raise an error for unknown fields in the data. Use EXCLUDE, INCLUDE or RAISE. If None, the value for self.unknown is used.

Renvoie:

Deserialized data

Nouveau dans la version 1.0.0.

Modifié dans la version 3.0.0b7: This method returns the deserialized data rather than a (data, errors) duple. A ValidationError is raised if invalid data are passed.

class src.utils_flask_sqla_geo.schema.FeatureSchema(*, only: marshmallow.types.StrSequenceOrSet | None = None, exclude: marshmallow.types.StrSequenceOrSet = (), many: bool = False, context: dict | None = None, load_only: marshmallow.types.StrSequenceOrSet = (), dump_only: marshmallow.types.StrSequenceOrSet = (), partial: bool | marshmallow.types.StrSequenceOrSet | None = None, unknown: str | None = None)[source]

Bases: marshmallow.Schema

Base schema class with which to define custom schemas.

Example usage:

import datetime as dt
from dataclasses import dataclass

from marshmallow import Schema, fields


@dataclass
class Album:
    title: str
    release_date: dt.date


class AlbumSchema(Schema):
    title = fields.Str()
    release_date = fields.Date()


album = Album("Beggars Banquet", dt.date(1968, 12, 6))
schema = AlbumSchema()
data = schema.dump(album)
data  # {'release_date': '1968-12-06', 'title': 'Beggars Banquet'}
Paramètres:
  • only – Whitelist of the declared fields to select when instantiating the Schema. If None, all fields are used. Nested fields can be represented with dot delimiters.

  • exclude – Blacklist of the declared fields to exclude when instantiating the Schema. If a field appears in both only and exclude, it is not used. Nested fields can be represented with dot delimiters.

  • many – Should be set to True if obj is a collection so that the object will be serialized to a list.

  • context – Optional context passed to fields.Method and fields.Function fields.

  • load_only – Fields to skip during serialization (write-only fields)

  • dump_only – Fields to skip during deserialization (read-only fields)

  • partial – Whether to ignore missing fields and not require any fields declared. Propagates down to Nested fields as well. If its value is an iterable, only missing fields listed in that iterable will be ignored. Use dot delimiters to specify nested fields.

  • unknown – Whether to exclude, include, or raise an error for unknown fields in the data. Use EXCLUDE, INCLUDE or RAISE.

Modifié dans la version 3.0.0: prefix parameter removed.

Modifié dans la version 2.0.0: __validators__, __preprocessors__, and __data_handlers__ are removed in favor of marshmallow.decorators.validates_schema, marshmallow.decorators.pre_load and marshmallow.decorators.post_dump. __accessor__ and __error_handler__ are deprecated. Implement the handle_error and get_attribute methods instead.

id[source]
type[source]
geometry[source]
properties[source]
class src.utils_flask_sqla_geo.schema.FeatureCollectionSchema(*, only: marshmallow.types.StrSequenceOrSet | None = None, exclude: marshmallow.types.StrSequenceOrSet = (), many: bool = False, context: dict | None = None, load_only: marshmallow.types.StrSequenceOrSet = (), dump_only: marshmallow.types.StrSequenceOrSet = (), partial: bool | marshmallow.types.StrSequenceOrSet | None = None, unknown: str | None = None)[source]

Bases: marshmallow.Schema

Base schema class with which to define custom schemas.

Example usage:

import datetime as dt
from dataclasses import dataclass

from marshmallow import Schema, fields


@dataclass
class Album:
    title: str
    release_date: dt.date


class AlbumSchema(Schema):
    title = fields.Str()
    release_date = fields.Date()


album = Album("Beggars Banquet", dt.date(1968, 12, 6))
schema = AlbumSchema()
data = schema.dump(album)
data  # {'release_date': '1968-12-06', 'title': 'Beggars Banquet'}
Paramètres:
  • only – Whitelist of the declared fields to select when instantiating the Schema. If None, all fields are used. Nested fields can be represented with dot delimiters.

  • exclude – Blacklist of the declared fields to exclude when instantiating the Schema. If a field appears in both only and exclude, it is not used. Nested fields can be represented with dot delimiters.

  • many – Should be set to True if obj is a collection so that the object will be serialized to a list.

  • context – Optional context passed to fields.Method and fields.Function fields.

  • load_only – Fields to skip during serialization (write-only fields)

  • dump_only – Fields to skip during deserialization (read-only fields)

  • partial – Whether to ignore missing fields and not require any fields declared. Propagates down to Nested fields as well. If its value is an iterable, only missing fields listed in that iterable will be ignored. Use dot delimiters to specify nested fields.

  • unknown – Whether to exclude, include, or raise an error for unknown fields in the data. Use EXCLUDE, INCLUDE or RAISE.

Modifié dans la version 3.0.0: prefix parameter removed.

Modifié dans la version 2.0.0: __validators__, __preprocessors__, and __data_handlers__ are removed in favor of marshmallow.decorators.validates_schema, marshmallow.decorators.pre_load and marshmallow.decorators.post_dump. __accessor__ and __error_handler__ are deprecated. Implement the handle_error and get_attribute methods instead.

type[source]
features[source]
class src.utils_flask_sqla_geo.schema.GeometryField(*, load_default: Any = missing_, missing: Any = missing_, dump_default: Any = missing_, default: Any = missing_, data_key: str | None = None, attribute: str | None = None, validate: None | Callable[[Any], Any] | Iterable[Callable[[Any], Any]] = None, required: bool = False, allow_none: bool | None = None, load_only: bool = False, dump_only: bool = False, error_messages: dict[str, str] | None = None, metadata: Mapping[str, Any] | None = None, **additional_metadata)[source]

Bases: marshmallow.fields.Field

Basic field from which other fields should extend. It applies no formatting by default, and should only be used in cases where data does not need to be formatted before being serialized or deserialized. On error, the name of the field will be returned.

Paramètres:
  • dump_default – If set, this value will be used during serialization if the input value is missing. If not set, the field will be excluded from the serialized output if the input value is missing. May be a value or a callable.

  • load_default – Default deserialization value for the field if the field is not found in the input data. May be a value or a callable.

  • data_key – The name of the dict key in the external representation, i.e. the input of load and the output of dump. If None, the key will match the name of the field.

  • attribute – The name of the attribute to get the value from when serializing. If None, assumes the attribute has the same name as the field. Note: This should only be used for very specific use cases such as outputting multiple fields for a single attribute. In most cases, you should use data_key instead.

  • validate – Validator or collection of validators that are called during deserialization. Validator takes a field’s input value as its only parameter and returns a boolean. If it returns False, an ValidationError is raised.

  • required – Raise a ValidationError if the field value is not supplied during deserialization.

  • allow_none – Set this to True if None should be considered a valid value during validation/deserialization. If load_default=None and allow_none is unset, will default to True. Otherwise, the default is False.

  • load_only – If True skip this field during serialization, otherwise its value will be present in the serialized data.

  • dump_only – If True skip this field during deserialization, otherwise its value will be present in the deserialized object. In the context of an HTTP API, this effectively marks the field as « read-only ».

  • error_messages (dict) – Overrides for Field.default_error_messages.

  • metadata – Extra information to be stored as field metadata.

Modifié dans la version 2.0.0: Removed error parameter. Use error_messages instead.

Modifié dans la version 2.0.0: Added allow_none parameter, which makes validation/deserialization of None consistent across fields.

Modifié dans la version 2.0.0: Added load_only and dump_only parameters, which allow field skipping during the (de)serialization process.

Modifié dans la version 2.0.0: Added missing parameter, which indicates the value for a field if the field is not found during deserialization.

Modifié dans la version 2.0.0: default value is only used if explicitly set. Otherwise, missing values inputs are excluded from serialized output.

Modifié dans la version 3.0.0b8: Add data_key parameter for the specifying the key in the input and output data. This parameter replaced both load_from and dump_to.

geometry_schema[source]
_serialize_wkt(value, attr, obj)[source]
_serialize_geojson(value, attr, obj)[source]
_deserialize_wkt(value, attr, data, **kwargs)[source]
_deserialize_geojson(value, attr, data, **kwargs)[source]
_bind_to_schema(field_name, schema)[source]

Update field with values from its parent schema. Called by Schema._bind_field.

Paramètres:
  • field_name (str) – Field name set in schema.

  • schema (Schema|Field) – Parent object.

class src.utils_flask_sqla_geo.schema.GeoModelConverter(schema_cls=None)[source]

Bases: marshmallow_sqlalchemy.convert.ModelConverter

Model converter for models with geometric fields.

SQLA_TYPE_MAPPING[source]
class src.utils_flask_sqla_geo.schema.GeoAlchemyAutoSchemaOpts(meta, *args, **kwargs)[source]

Bases: marshmallow_sqlalchemy.schema.SQLAlchemyAutoSchemaOpts

Options class for GeoAlchemyAutoSchema. Adds the following options:

  • geometry_fields: List of Geometry columns.

  • feature_id: Identity field to use when generating features.

  • feature_geometry: Geometry field to use when generating features.

Thus, this options class define GeoModelConverter as default model converter.

class src.utils_flask_sqla_geo.schema.GeoAlchemyAutoSchema(*args, as_geojson=False, feature_id=None, feature_geometry=None, only=None, exclude=(), **kwargs)[source]

Bases: marshmallow_sqlalchemy.schema.SQLAlchemyAutoSchema

Auto schema with support for geometric fields and geojson generation.

Paramètres:
  • as_geojson – If true, serialize and deserialize geojson instead of json.

  • feature_id – Identity field to use when generating features. If None, use feature_id specified on class Meta if any, otherwise features are generated without id.

  • feature_geometry – Geometry field to use when generating features. If None, use feature_geometry specified on class Meta. If not specified on class Meta either, auto-detect the geometry field. If none or several geometric fields are detected, raise a TypeError.

Geometric fields are automatically removed from serialization.

OPTIONS_CLASS[source]
to_feature(properties)[source]
from_feature(feature)[source]
_serialize(obj, *, many=None)[source]

Serialize obj.

Paramètres:
  • obj – The object(s) to serialize.

  • many (bool) – True if data should be serialized as a collection.

Renvoie:

A dictionary of the serialized data

Modifié dans la version 1.0.0: Renamed from marshal.

to_geojson(data, many, **kwargs)[source]
from_geojson(data, many, **kwargs)[source]