示例#1
0
文件: types.py 项目: assembl/assembl
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of
        # SQLAlchemyInterface (excluding SQLAlchemyInterface class itself).
        if not is_base_type(bases, SQLAlchemyInterfaceMeta):
            return type.__new__(cls, name, bases, attrs)

        options = Options(
            attrs.pop('Meta', None),
            name=name,
            description=attrs.pop('__doc__', None),
            model=None,
            local_fields=None,
            only_fields=(),
            exclude_fields=(),
            # id='id',
            registry=None
        )

        if not options.registry:
            options.registry = get_global_registry()
        assert isinstance(options.registry, Registry), (
            'The attribute registry in {}.Meta needs to be an'
            ' instance of Registry, received "{}".'
        ).format(name, options.registry)
        assert is_mapped(options.model), (
            'You need to pass a valid SQLAlchemy Model in '
            '{}.Meta, received "{}".'
        ).format(name, options.model)

        cls = type.__new__(cls, name, bases, dict(attrs, _meta=options))

        options.base_fields = ()
        options.base_fields = get_base_fields(bases, _as=Field)
        if not options.local_fields:
            options.local_fields = yank_fields_from_attrs(attrs, _as=Field)

        # options.registry.register(cls)

        options.fields = merge(
            options.base_fields,
            options.local_fields
        )

        options.sqlalchemy_fields = yank_fields_from_attrs(
            construct_fields(options),
            _as=Field,
        )
        options.fields = merge(
            options.sqlalchemy_fields,
            options.base_fields,
            options.local_fields
        )

        return cls
示例#2
0
文件: types.py 项目: n0izn0iz/assembl
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of SQLAlchemyInterface
        # (excluding SQLAlchemyInterface class itself).
        if not is_base_type(bases, SQLAlchemyInterfaceMeta):
            return type.__new__(cls, name, bases, attrs)

        options = Options(
            attrs.pop('Meta', None),
            name=name,
            description=attrs.pop('__doc__', None),
            model=None,
            local_fields=None,
            only_fields=(),
            exclude_fields=(),
            # id='id',
            registry=None)

        if not options.registry:
            options.registry = get_global_registry()
        assert isinstance(
            options.registry,
            Registry), ('The attribute registry in {}.Meta needs to be an'
                        ' instance of Registry, received "{}".').format(
                            name, options.registry)
        assert is_mapped(
            options.model), ('You need to pass a valid SQLAlchemy Model in '
                             '{}.Meta, received "{}".').format(
                                 name, options.model)

        cls = type.__new__(cls, name, bases, dict(attrs, _meta=options))

        options.base_fields = ()
        options.base_fields = get_base_fields(bases, _as=Field)
        if not options.local_fields:
            options.local_fields = yank_fields_from_attrs(attrs, _as=Field)

        # options.registry.register(cls)

        options.fields = merge(options.base_fields, options.local_fields)

        options.sqlalchemy_fields = yank_fields_from_attrs(
            construct_fields(options),
            _as=Field,
        )
        options.fields = merge(options.sqlalchemy_fields, options.base_fields,
                               options.local_fields)

        return cls
示例#3
0
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of
        # DjangoObjectType
        if not is_base_type(bases, DjangoObjectTypeMeta):
            return type.__new__(cls, name, bases, attrs)

        defaults = dict(
            name=name,
            description=attrs.pop('__doc__', None),
            model=None,
            local_fields=None,
            only_fields=(),
            exclude_fields=(),
            interfaces=(),
            registry=None
        )
        if DJANGO_FILTER_INSTALLED:
            # In case Django filter is available, then
            # we allow more attributes in Meta
            defaults.update(
                filter_fields=(),
                filter_order_by=(),
            )

        options = Options(
            attrs.pop('Meta', None),
            **defaults
        )
        if not options.registry:
            options.registry = get_global_registry()
        assert isinstance(options.registry, Registry), (
            'The attribute registry in {}.Meta needs to be an instance of '
            'Registry, received "{}".'
        ).format(name, options.registry)
        assert is_valid_django_model(options.model), (
            'You need to pass a valid Django Model in {}.Meta, received "{}".'
        ).format(name, options.model)

        cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options))

        options.registry.register(cls)

        options.django_fields = yank_fields_from_attrs(
            construct_fields(options),
            _as=Field,
        )
        options.fields = merge(
            options.interface_fields,
            options.django_fields,
            options.base_fields,
            options.local_fields
        )

        return cls
示例#4
0
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of
        # DjangoObjectType
        if not is_base_type(bases, DjangoObjectTypeMeta):
            return type.__new__(cls, name, bases, attrs)

        defaults = dict(
            name=name,
            description=attrs.pop('__doc__', None),
            model=None,
            local_fields=None,
            only_fields=(),
            exclude_fields=(),
            interfaces=(),
            registry=None
        )
        if DJANGO_FILTER_INSTALLED:
            # In case Django filter is available, then
            # we allow more attributes in Meta
            defaults.update(
                filter_fields=(),
                filter_order_by=(),
            )

        options = Options(
            attrs.pop('Meta', None),
            **defaults
        )
        if not options.registry:
            options.registry = get_global_registry()
        assert isinstance(options.registry, Registry), (
            'The attribute registry in {}.Meta needs to be an instance of '
            'Registry, received "{}".'
        ).format(name, options.registry)
        assert is_valid_django_model(options.model), (
            'You need to pass a valid Django Model in {}.Meta, received "{}".'
        ).format(name, options.model)

        cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options))

        options.registry.register(cls)

        options.django_fields = yank_fields_from_attrs(
            construct_fields(options),
            _as=Field,
        )
        options.fields = merge(
            options.interface_fields,
            options.django_fields,
            options.base_fields,
            options.local_fields
        )

        return cls
示例#5
0
文件: types.py 项目: wowngasb/pykl
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of Model
        # (excluding Model class itself).
        if not is_base_type(bases, SQLAlchemyObjectTypeMeta):
            return type.__new__(cls, name, bases, attrs)

        meta = attrs.pop('Meta', None)
        local_fields = OrderedDict(
            {k: v
             for k, v in attrs.items() if _is_graphql(v)})
        tbl_doc = attrs.pop('__doc__', getattr(meta.model, '__doc__', None))
        options = Options(meta,
                          name=name,
                          description=tbl_doc if not tbl_doc else
                          (tbl_doc if isinstance(tbl_doc, unicode) else
                           tbl_doc.decode('utf8')),
                          model=None,
                          local_fields=local_fields,
                          exclude_fields=set(),
                          interfaces=(),
                          registry=None)

        if not options.registry:
            options.registry = get_global_registry()
        assert isinstance(
            options.registry,
            Registry), ('The attribute registry in {}.Meta needs to be an'
                        ' instance of Registry, received "{}".').format(
                            name, options.registry)
        assert is_mapped(
            options.model), ('You need to pass a valid SQLAlchemy Model in '
                             '{}.Meta, received "{}".').format(
                                 name, options.model)

        cls = ObjectTypeMeta.__new__(cls, name, bases,
                                     dict(attrs, _meta=options))

        options.registry.register(cls)

        options.sqlalchemy_fields = yank_fields_from_attrs(
            construct_fields(options),
            _as=graphene.Field,
        )
        options.fields = merge(options.interface_fields,
                               options.sqlalchemy_fields, options.base_fields,
                               options.local_fields)

        return cls
示例#6
0
文件: types.py 项目: wowngasb/kl_tool
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of Model
        # (excluding Model class itself).
        if not is_base_type(bases, SQLAlchemyObjectTypeMeta):
            return type.__new__(cls, name, bases, attrs)

        meta = attrs.pop('Meta', None)
        local_fields = OrderedDict({k:v for k,v in attrs.items() if _is_graphql(v)})

        options = Options(
            meta,
            name=name,
            description=attrs.pop('__doc__', getattr(meta.model, '__doc__', None)),
            model=None,
            local_fields=local_fields,
            exclude_fields=set(),
            interfaces=(),
            registry=None
        )

        if not options.registry:
            options.registry = get_global_registry()
        assert isinstance(options.registry, Registry), (
            'The attribute registry in {}.Meta needs to be an'
            ' instance of Registry, received "{}".'
        ).format(name, options.registry)
        assert is_mapped(options.model), (
            'You need to pass a valid SQLAlchemy Model in '
            '{}.Meta, received "{}".'
        ).format(name, options.model)

        cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options))

        options.registry.register(cls)

        options.sqlalchemy_fields = yank_fields_from_attrs(
            construct_fields(options),
            _as = graphene.Field,
        )
        options.fields = merge(
            options.interface_fields,
            options.sqlalchemy_fields,
            options.base_fields,
            options.local_fields
        )

        return cls
示例#7
0
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of Model
        # (excluding Model class itself).
        if not is_base_type(bases, PynamoObjectTypeMeta):
            return type.__new__(cls, name, bases, attrs)

        options = Options(
            attrs.pop('Meta', None),
            name=name,
            description=attrs.pop('__doc__', None),
            model=None,
            local_fields=None,
            only_fields=(),
            exclude_fields=(),
            id='id',
            interfaces=(),
            registry=None
        )

        if not options.registry:
            options.registry = get_global_registry()
        assert isinstance(options.registry, Registry), (
            'The attribute registry in {}.Meta needs to be an'
            ' instance of Registry, received "{}".'
        ).format(name, options.registry)

        assert (inspect.isclass(options.model) and issubclass(options.model, Model)), (
            'You need to pass a valid PynamoDB Model in '
            '{}.Meta, received "{}".'
        ).format(name, options.model)

        cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options))

        options.registry.register(cls)

        options.pynamo_fields = yank_fields_from_attrs(
            construct_fields(options),
            _as=Field,
        )
        options.fields = merge(
            options.interface_fields,
            options.pynamo_fields,
            options.base_fields,
            options.local_fields
        )

        return cls
示例#8
0
    def __new__(cls, name, bases, attrs):
        if not is_base_type(bases, SerializerMutationMeta):
            return type.__new__(cls, name, bases, attrs)

        options = Options(
            attrs.pop('Meta', None),
            name=name,
            description=attrs.pop('__doc__', None),
            serializer_class=None,
            local_fields=None,
            only_fields=(),
            exclude_fields=(),
            interfaces=(),
            registry=None
        )

        if not options.serializer_class:
            raise Exception('Missing serializer_class')

        cls = ObjectTypeMeta.__new__(
            cls, name, bases, dict(attrs, _meta=options)
        )

        serializer_fields = cls.fields_for_serializer(options)
        options.serializer_fields = yank_fields_from_attrs(
            serializer_fields,
            _as=Field,
        )

        options.fields = merge(
            options.interface_fields, options.serializer_fields,
            options.base_fields, options.local_fields,
            {'errors': get_field_as(cls.errors, Field)}
        )

        cls.Input = convert_serializer_to_input_type(options.serializer_class)

        cls.Field = partial(
            Field,
            cls,
            resolver=cls.mutate,
            input=Argument(cls.Input, required=True)
        )

        return cls
示例#9
0
    def __new__(cls, name, bases, attrs):
        if not is_base_type(bases, SerializerMutationMeta):
            return type.__new__(cls, name, bases, attrs)

        options = Options(attrs.pop('Meta', None),
                          name=name,
                          description=attrs.pop('__doc__', None),
                          serializer_class=None,
                          local_fields=None,
                          only_fields=(),
                          exclude_fields=(),
                          interfaces=(),
                          registry=None)

        if not options.serializer_class:
            raise Exception('Missing serializer_class')

        cls = ObjectTypeMeta.__new__(cls, name, bases,
                                     dict(attrs, _meta=options))

        serializer_fields = cls.fields_for_serializer(options)
        options.serializer_fields = yank_fields_from_attrs(
            serializer_fields,
            _as=Field,
        )

        options.fields = merge(options.interface_fields,
                               options.serializer_fields, options.base_fields,
                               options.local_fields,
                               {'errors': get_field_as(cls.errors, Field)})

        cls.Input = convert_serializer_to_input_type(options.serializer_class)

        cls.Field = partial(Field,
                            cls,
                            resolver=cls.mutate,
                            input=Argument(cls.Input, required=True))

        return cls
示例#10
0
    def __new__(mcs, name, bases, attrs):
        if not is_base_type(bases, NdbObjectTypeMeta):
            return type.__new__(mcs, name, bases, attrs)

        options = Options(attrs.pop('Meta', None),
                          name=name,
                          description=attrs.pop('__doc__', None),
                          model=None,
                          local_fields=None,
                          only_fields=(),
                          exclude_fields=(),
                          interfaces=(),
                          registry=None)

        if not options.model:
            raise Exception(
                'NdbObjectType %s must have a model in the Meta class attr' %
                name)

        if not inspect.isclass(options.model) or not issubclass(
                options.model, ndb.Model):
            raise Exception('Provided model in %s is not an NDB model' % name)

        new_cls = ObjectTypeMeta.__new__(mcs, name, bases,
                                         dict(attrs, _meta=options))
        mcs.register(new_cls)

        ndb_fields = mcs.fields_for_ndb_model(options)
        options.ndb_fields = yank_fields_from_attrs(
            ndb_fields,
            _as=Field,
        )
        options.fields = merge(options.interface_fields, options.ndb_fields,
                               options.base_fields, options.local_fields)

        return new_cls