def register_schema(schema, definition_name=None): definition_name = definition_name or re.sub(r'Schema$', '', schema.__name__) spec.definition(definition_name, schema=schema())
import re import functools import marshmallow as ma from marshmallow_sqlalchemy import ModelSchema from marshmallow_pagination import schemas as paging_schemas from webservices import utils from webservices.spec import spec from webservices.common import models from webservices import __API_VERSION__ spec.definition('OffsetInfo', schema=paging_schemas.OffsetInfoSchema) spec.definition('SeekInfo', schema=paging_schemas.SeekInfoSchema) def register_schema(schema, definition_name=None): definition_name = definition_name or re.sub(r'Schema$', '', schema.__name__) spec.definition(definition_name, schema=schema) def make_schema(model, class_name=None, fields=None, options=None): class_name = class_name or '{0}Schema'.format(model.__name__) Meta = type( 'Meta', (object, ), utils.extend( { 'model': model,
import marshmallow as ma from marshmallow_sqlalchemy import ModelSchema from marshmallow_pagination import schemas as paging_schemas from webservices import utils, decoders from webservices.spec import spec from webservices.common import models from webservices.common.models import db from webservices import __API_VERSION__ from webservices.calendar import format_start_date, format_end_date from marshmallow import pre_dump, post_dump from sqlalchemy import func import sqlalchemy as sa spec.definition('OffsetInfo', schema=paging_schemas.OffsetInfoSchema) spec.definition('SeekInfo', schema=paging_schemas.SeekInfoSchema) # A namedtuple used to help capture any additional columns that should be # included with exported data: # field: the field object definining the relationship on a model, e.g., # models.ScheduleA.committee (an object) # column: the column object found in the related model, e.g., # models.CommitteeHistory.name (an object) # label: the label to use for the column in the query that will appear in the # header row of the output, e.g., # 'committee_name' (a string) # position: the spot within the list of columns that this should be inserted # at; defaults to -1 (end of the list), e.g., # 1 (an integer, in this case the second spot in a list)
) return klass class BaseInfoSchema(ma.Schema): count = ma.fields.Integer() pages = ma.fields.Integer() per_page = ma.fields.Integer() class OffsetInfoSchema(BaseInfoSchema): page = ma.fields.Integer() class SeekInfoSchema(BaseInfoSchema): last_indexes = ma.fields.Raw() class OffsetPageSchema(ma.Schema, metaclass=PageMeta): OPTIONS_CLASS = PageSchemaOpts pagination = ma.fields.Nested(OffsetInfoSchema, ref='#/definitions/OffsetInfo', attribute='info') class SeekPageSchema(ma.Schema, metaclass=PageMeta): OPTIONS_CLASS = PageSchemaOpts pagination = ma.fields.Nested(SeekInfoSchema, ref='#/definitions/SeekInfo', attribute='info') spec.definition('OffsetInfo', schema=OffsetInfoSchema) spec.definition('SeekInfo', schema=SeekInfoSchema)
def __new__(mcs, name, bases, attrs): klass = super().__new__(mcs, name, bases, attrs) opts = klass.OPTIONS_CLASS(klass.Meta) klass._declared_fields[opts.results_field_name] = ma.fields.Nested( opts.results_schema_class, attribute='results', many=True, **opts.results_schema_options ) return klass class PageSchema(ma.Schema, metaclass=PageMeta): OPTIONS_CLASS = PageSchemaOpts pagination = ma.fields.Nested(PaginationSchema, ref='#/definitions/Pagination') class SqlalchemyPaginator(Paginator): def _count(self): return self.cursor.order_by(None).count() def _slice(self, offset, limit): offset += (self.cursor._offset or 0) if self.cursor._limit: limit = min(limit, self.cursor._limit - offset) return self.cursor.offset(offset).limit(limit).all() spec.definition('Pagination', schema=PaginationSchema)
def register_schema(schema, definition_name=None): definition_name = definition_name or re.sub(r"Schema$", "", schema.__name__) spec.definition(definition_name, schema=schema)
import re import functools import marshmallow as ma from marshmallow_sqlalchemy import ModelSchema from marshmallow_pagination import schemas as paging_schemas from webservices import utils from webservices.spec import spec from webservices.common import models from webservices import __API_VERSION__ spec.definition("OffsetInfo", schema=paging_schemas.OffsetInfoSchema) spec.definition("SeekInfo", schema=paging_schemas.SeekInfoSchema) def register_schema(schema, definition_name=None): definition_name = definition_name or re.sub(r"Schema$", "", schema.__name__) spec.definition(definition_name, schema=schema) def make_schema(model, class_name=None, fields=None, options=None): class_name = class_name or "{0}Schema".format(model.__name__) Meta = type( "Meta", (object,), utils.extend({"model": model, "sqla_session": models.db.session, "exclude": ("idx",)}, options or {}), )
class BaseInfoSchema(ma.Schema): count = ma.fields.Integer() pages = ma.fields.Integer() per_page = ma.fields.Integer() class OffsetInfoSchema(BaseInfoSchema): page = ma.fields.Integer() class SeekInfoSchema(BaseInfoSchema): last_indexes = ma.fields.Raw() class OffsetPageSchema(ma.Schema, metaclass=PageMeta): OPTIONS_CLASS = PageSchemaOpts pagination = ma.fields.Nested(OffsetInfoSchema, ref='#/definitions/OffsetInfo', attribute='info') class SeekPageSchema(ma.Schema, metaclass=PageMeta): OPTIONS_CLASS = PageSchemaOpts pagination = ma.fields.Nested(SeekInfoSchema, ref='#/definitions/SeekInfo', attribute='info') spec.definition('OffsetInfo', schema=OffsetInfoSchema) spec.definition('SeekInfo', schema=SeekInfoSchema)