Source code for parseur.schemas.paserfield

from enum import Enum

from marshmallow import RAISE, fields, validate

from parseur.schemas import BaseSchema


[docs]class FieldFormat(str, Enum): """ Enumeration of the data formats a parser field can have. Used as the ``format`` of a field when creating or updating a mailbox's fields (``parser_object_set``). """
[docs] TEXT = "TEXT"
[docs] ONELINE = "ONELINE"
[docs] DATE = "DATE"
[docs] TIME = "TIME"
[docs] DATETIME = "DATETIME"
[docs] NUMBER = "NUMBER"
[docs] NAME = "NAME"
[docs] ADDRESS = "ADDRESS"
[docs] TABLE = "TABLE"
[docs]class TableFieldReadSchema(BaseSchema): """A table field as summarised in a mailbox's ``table_set`` (id + name)."""
[docs] id = fields.String(required=True)
[docs] name = fields.String(required=True)
[docs]class ParserFieldBaseSchema(BaseSchema): """ Properties shared by the read and write representations of a parser field. Both serializing API responses and validating create/update request bodies rely on these common fields, so they are declared once here. """
[docs] name = fields.String(required=True)
[docs] format = fields.String( required=True, validate=validate.OneOf( [e.value for e in FieldFormat], error="Must be one of: " + ", ".join(e.value for e in FieldFormat) + ".", ), )
[docs] query = fields.String(allow_none=True)
[docs] choice_set = fields.List(fields.String(), allow_none=True)
[docs]class ParserFieldReadSchema(ParserFieldBaseSchema): """Read schema for a parser field returned by the API."""
[docs] id = fields.String(required=True)
[docs] type = fields.String(required=True)
[docs] is_required = fields.Boolean(required=True)
[docs] used_by_ai = fields.Boolean(required=True)
[docs] csv_download = fields.String(required=True)
[docs] json_download = fields.String(required=True)
[docs] xls_download = fields.String(required=True)
[docs] parser_object_set = fields.List(fields.Nested("ParserFieldReadSchema"))
[docs]class ParserFieldWriteSchema(ParserFieldBaseSchema): """ Write schema for a parser field. Used to validate and serialize each entry of a mailbox's ``parser_object_set``. Unknown fields are rejected so read-only properties (``type``, ``csv_download``, ...) and typos never reach the API silently. Pass ``id`` to update an existing field; omit it to create a new one. """
[docs] class Meta:
[docs] unknown = RAISE
[docs] ordered = True
# Present when updating an existing field, omitted when creating one.
[docs] id = fields.String()
[docs] is_required = fields.Boolean()
[docs] used_by_ai = fields.Boolean()
# Set to True to delete this field (matched by name) on the next save.
[docs] _destroy = fields.Boolean()
# Nested columns, used when ``format`` is TABLE.
[docs] parser_object_set = fields.Nested( "ParserFieldWriteSchema", many=True, allow_none=True )