Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
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, SQLAlchemyObjectTypeMeta):
            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,
            abstract=False,
        )

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

        if options.abstract:
            return cls

        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_class(
            options.model), ('You need to pass a valid SQLAlchemy Model in '
                             '{}.Meta, received "{}".').format(
                                 name, options.model)

        options.registry.register(cls)

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

        return cls
Ejemplo n.º 4
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, 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
Ejemplo n.º 5
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, 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
Ejemplo n.º 6
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
Ejemplo n.º 7
0
    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
Ejemplo n.º 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
Ejemplo n.º 9
0
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of
        # DjangoObjectType
        if not is_base_type(bases, SpecialObjectTypeMeta):
            return type.__new__(cls, name, bases, attrs)

        options = Options(
            attrs.pop('Meta', None),
            other_attr='default',
        )

        cls = ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options))
        assert cls._meta is options
        return cls
Ejemplo n.º 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
Ejemplo n.º 11
0
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of
        # Union
        if not is_base_type(bases, SQLAlchemyUnionMeta):
            return type.__new__(cls, name, bases, attrs)

        options = Options(attrs.pop('Meta', None),
                          name=name,
                          description=attrs.get('__doc__'),
                          types=(),
                          model=None)

        assert (isinstance(options.types,
                           (list, tuple)) and len(options.types) > 0
                ), 'Must provide types for Union {}.'.format(options.name)

        return type.__new__(cls, name, bases, dict(attrs, _meta=options))
Ejemplo n.º 12
0
 def __init__(self, type, *args, **kwargs):
     options = Options(
         meta=None,  # attrs.pop('Meta', None),
         name=type._meta.name,  # name,
         description=None,  # attrs.pop('__doc__', None),
         model=type._meta.model,  # None,
         local_fields=None,
         only_fields=(),
         exclude_fields=('password', 'search_vector'),
         id='id',
         interfaces=(),
         registry=get_global_registry(),
         fields=(),
     )
     fields = construct_fields(options)
     for field_name, field_type in fields.items():
         if hasattr(field_type,
                    'kwargs') and 'required' in field_type.kwargs:
             field_type.kwargs['required'] = False
         if not isinstance(field_type, Dynamic):  # and field_name != 'id':
             kwargs.setdefault(field_name, field_type)
     super(FilterableConnectionField, self).__init__(type, *args, **kwargs)
Ejemplo n.º 13
0
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of
        # DeferredUnion
        if not is_base_type(bases, DeferredUnionMeta):
            return type.__new__(cls, name, bases, attrs)

        meta = attrs.pop('Meta', None)
        if meta is None:
            raise AssertionError('Meta is required for union type.')

        if hasattr(meta, 'types') and isinstance(meta.types, DeferredAttr):

            class DeferredOptions(Options):
                _types = meta.types

                @property
                def types(self):
                    return self._types()

            delattr(meta, 'types')
            options = DeferredOptions(meta,
                                      name=name,
                                      description=trim_docstring(
                                          attrs.get('__doc__')))
        else:
            options = Options(
                meta,
                name=name,
                description=trim_docstring(attrs.get('__doc__')),
                types=(),
            )

            assert (isinstance(options.types,
                               (list, tuple)) and len(options.types) > 0
                    ), 'Must provide types for Union {}.'.format(options.name)

        return type.__new__(cls, name, bases, dict(attrs, _meta=options))
Ejemplo n.º 14
0
    def __new__(cls, name, bases, attrs):
        # Also ensure initialization is only performed for subclasses of
        # Mutation
        if not is_base_type(bases, ClientIDMutationMeta):
            return type.__new__(cls, name, bases, attrs)

        defaults = dict(
            name=name,
            description=attrs.pop('__doc__', None),
            interfaces=(),
            local_fields=None,
            permission_classes=(),
            throttle_classes=(),

            # for Django Model Permissions
            model=None,

            # for permissions
            method=None,  # 'CREATE', 'UPDATE', 'DELETE' only this 3 accepted
            form_class=None,

            lookup_field='pk',
            lookup_kwarg=None
        )

        options = Options(
            attrs.pop('Config', None),
            **defaults
        )

        if options.model is not None:
            assert is_valid_django_model(options.model), (
                'You need to pass a valid Django Model in {}.Meta, received "{}".'
            ).format(name, options.model)

        input_class = attrs.pop('Input', None)
        base_name = re.sub('Payload$', '', name)
        if 'client_mutation_id' not in attrs:
            attrs['client_mutation_id'] = String(name='clientMutationId')
        cls = ObjectTypeMeta.__new__(cls, '{}Payload'.format(base_name), bases, dict(attrs, _meta=options))
        mutate_and_get_payload = getattr(cls, 'mutate_and_get_payload', None)
        if cls.mutate and cls.mutate.__func__ == ClientIDMutation.mutate.__func__:
            assert mutate_and_get_payload, (
                "{}.mutate_and_get_payload method is required"
                " in a ClientIDMutation."
            ).format(name)
        input_attrs = {}
        bases = ()
        if not input_class:
            input_attrs = {}
        elif not issubclass(input_class, AbstractType):
            input_attrs = props(input_class)
        else:
            bases += (input_class, )
        input_attrs['client_mutation_id'] = String(name='clientMutationId')

        # If method is update and delete add id to input attrs
        if options.method is not None and options.method.upper() in ['UPDATE', 'DELETE']:
            input_attrs['id'] = String(name='id', required=True)

        cls.Input = type('{}Input'.format(base_name), bases + (InputObjectType,), input_attrs)
        cls.Field = partial(Field, cls, resolver=cls.mutate, input=Argument(cls.Input, required=True))

        return cls