def __new__(mcs, name, bases, attrs): fields = _get_fields(attrs, base.FieldABC, pop=True) klass = super(SchemaMeta, mcs).__new__(mcs, name, bases, attrs) fields = _get_fields_by_mro(klass, base.FieldABC) + fields klass._declared_fields = OrderedDict(fields) class_registry.register(name, klass) return klass
def __new__(mcs, name, bases, attrs): meta = attrs.get('Meta') ordered = getattr(meta, 'ordered', False) if not ordered: # Inherit 'ordered' option # Warning: We loop through bases in reverse order rather than # looping through the MRO because we don't yet have access to the # class object (i.e. can't call super before we have fields) ordered = next( (base for base in bases[::-1] if hasattr(base, 'Meta') and getattr(base.Meta, 'ordered', False)), False ) fields = _get_fields(attrs, base.FieldABC, pop=True, ordered=ordered) klass = super(SchemaMeta, mcs).__new__(mcs, name, bases, attrs) fields = _get_fields_by_mro(klass, base.FieldABC) + fields dict_cls = OrderedDict if ordered else dict klass._declared_fields = dict_cls(fields) class_registry.register(name, klass) # Need to copy validators, data handlers, and preprocessors lists # so they are not shared among subclasses and ancestors for attr in mcs.FUNC_LISTS: attr_copy = copy.copy(getattr(klass, attr)) setattr(klass, attr, attr_copy) return klass
def __new__(mcs, name, bases, attrs): meta = attrs.get('Meta') ordered = getattr(meta, 'ordered', False) if not ordered: # Inherit 'ordered' option # Warning: We loop through bases in reverse order rather than # looping through the MRO because we don't yet have access to the # class object (i.e. can't call super before we have fields) ordered = next((base for base in bases[::-1] if hasattr(base, 'Meta') and getattr(base.Meta, 'ordered', False)), False) include = list(getattr(meta, 'include', {}).items()) fields = _get_fields(attrs, base.FieldABC, pop=True, ordered=ordered) + include klass = super(SchemaMeta, mcs).__new__(mcs, name, bases, attrs) fields = _get_fields_by_mro(klass, base.FieldABC) + fields dict_cls = OrderedDict if ordered else dict klass._declared_fields = dict_cls(fields) class_registry.register(name, klass) # Need to copy validators, data handlers, and preprocessors lists # so they are not shared among subclasses and ancestors for attr in mcs.FUNC_LISTS: attr_copy = copy.copy(getattr(klass, attr)) setattr(klass, attr, attr_copy) return klass
def __new__(mcs, name, bases, attrs): meta = attrs.get('Meta') ordered = getattr(meta, 'ordered', False) fields = _get_fields(attrs, base.FieldABC, pop=True, ordered=ordered) klass = super(SchemaMeta, mcs).__new__(mcs, name, bases, attrs) fields = _get_fields_by_mro(klass, base.FieldABC) + fields dict_cls = OrderedDict if ordered else dict klass._declared_fields = dict_cls(fields) class_registry.register(name, klass) return klass
def process_objects(self, app: Flask, objects): for name, serializer in objects.items(): self.bundle.serializers[name] = serializer class_registry.register(name, serializer) model = serializer.Meta.model model_name = model if isinstance(model, str) else model.__name__ kind = getattr(serializer, '__kind__', 'all') if kind == 'all': self.bundle.serializers_by_model[model_name] = serializer api.register_serializer(serializer, name=title_case(model_name)) elif kind == 'create': self.bundle.create_by_model[model_name] = serializer elif kind == 'many': self.bundle.many_by_model[model_name] = serializer
def __init__(cls, name, bases, attrs): super().__init__(name, bases, attrs) if name and cls.opts.register: class_registry.register(name, cls) cls._hooks = cls.resolve_hooks()
def __init__(self, name, bases, attrs): super(SchemaMeta, self).__init__(name, bases, attrs) if name: class_registry.register(name, self) self._hooks = self.resolve_hooks()
def __init__(self, name, bases, attrs): super().__init__(name, bases, attrs) if name and self.opts.register: class_registry.register(name, self) self._hooks = self.resolve_hooks()
def __init__(self, name, bases, attrs): super(SchemaMeta, self).__init__(name, bases, attrs) class_registry.register(name, self) self._copy_func_attrs() self._resolve_processors()
def __call__(self, cls): def dump(cls, obj, many=None): with_meta = dict(attributes=obj, type=cls.__name__) return super(Data, self).dump(with_meta, many=None) cls_instance = deepcopy(cls()) # Check if schema is an inheritance of another decorated schema with JsonApificator # Eg: # @JsonApificator(attributes={"required": True}) # class Father(Schema): # name = fields.Str(required=True) # age = fields.Int(required=True) # cpf = fields.Str() # # @JsonApificator(attributes={"required": True}) # class Child(Person): # name = fields.Str(required=False) # age = fields.Int(required=False) # In this case it needs overwrite the attributes with the new attributes if 'data' in cls_instance.fields: nested = (cls_instance.fields.pop('data')).nested() # deepcopy is necessary to not overwrite the Father attributes schema old_fields = deepcopy(nested.fields['attributes'].schema.fields) old_fields.update(cls_instance.fields) cls_instance.fields = old_fields json_api_fields = { "id": ma.fields.Str(**self._id), "type": ma.fields.Str(**self._type), "attributes": ma.fields.Nested(cls_instance, **self._attributes) } # If any relationship is informed, then it will be necessary to set them into Relatioship if self._relationships: relationships_fields = {} for relation in self._relationships: # Convert camel case class name to underscore pattern and then add itself in Nested field relationships_fields[camel_case_to_underscore( relation['relationships'].__name__)] = ma.fields.Nested( relation['relationships'], **relation.get('extra_kwargs', {})) # Create Relatioship class with its attributes Relationships = type('Relationships', (ma.Schema, ), relationships_fields) json_api_fields['relationships'] = ma.fields.Nested( Relationships()) if self._many: attr_pop = json_api_fields.pop('attributes') json_api_fields.update(attr_pop.schema.fields) # Create JsonApi class when many is True JsonApi = type('JsonApi', (ma.Schema, ), json_api_fields) else: # Create JsonApi class with its attributes JsonApi = type('JsonApi', (ma.Schema, ), json_api_fields) JsonApi.dump = classmethod(dump) class Data(ma.Schema): _required = {"required": False} if self._type.get('required') or self._attributes.get( 'required') or self._id.get('required'): _required['required'] = True data = ma.fields.Nested(JsonApi, many=self._many, **_required) # Include other attributes into Data schema for _attr in self._other_attributes: vars()[_attr['name']] = ma.fields.Nested( _attr['schema'], **_attr.get('attrs', {})) # Add other attributes into schema Data.dump = classmethod(dump) Data.orig = cls_instance Data.__name__ = 'JsonApi_' + cls.__name__ Data.description = 'JsonApi_' + cls.__name__ Data.children = [] Data.params_description = self._params_description register(Data.__name__, Data) return Data
def __new__(mcs, name, bases, attrs): attrs['_declared_fields'] = mcs.get_declared_fields( bases, attrs, base.FieldABC) new_class = super(SerializerMeta, mcs).__new__(mcs, name, bases, attrs) class_registry.register(name, new_class) return new_class
def __init__(self, name, bases, attrs): super(SchemaMeta, self).__init__(name, bases, attrs) if name: class_registry.register(name, self) self._resolve_processors()
def __new__(mcs, name, bases, attrs): attrs['_declared_fields'] = mcs.get_declared_fields(bases, attrs, base.FieldABC) new_class = super(SerializerMeta, mcs).__new__(mcs, name, bases, attrs) class_registry.register(name, new_class) return new_class
def register_schema(name, schema): register(name, schema) spec.definition(name, schema=schema)