Beispiel #1
0
    def __init_subclass_with_meta__(
        cls,
        description=None,
        permissions: Tuple = None,
        _meta=None,
        error_type_class=None,
        error_type_field=None,
        **options,
    ):
        if not _meta:
            _meta = MutationOptions(cls)

        if not description:
            raise ImproperlyConfigured("No description provided in Meta")

        if isinstance(permissions, str):
            permissions = (permissions,)

        if permissions and not isinstance(permissions, tuple):
            raise ImproperlyConfigured(
                "Permissions should be a tuple or a string in Meta"
            )

        _meta.permissions = permissions
        _meta.error_type_class = error_type_class
        _meta.error_type_field = error_type_field
        super().__init_subclass_with_meta__(
            description=description, _meta=_meta, **options
        )
        if error_type_class and error_type_field:
            cls._meta.fields.update(
                get_error_fields(error_type_class, error_type_field)
            )
Beispiel #2
0
    def __init_subclass_with_meta__(
        cls, permissions: Tuple = None, _meta=None, **options
    ):
        if not _meta:
            _meta = MutationOptions(cls)

        _meta.permissions = permissions
        super().__init_subclass_with_meta__(_meta=_meta, **options)
Beispiel #3
0
    def __init_subclass_with_meta__(cls,
                                    model_class=None,
                                    serializer_class=None,
                                    model_operation=None,
                                    login_required=True,
                                    _meta=None,
                                    **options):
        if not _meta:
            _meta = MutationOptions(cls)

        if not serializer_class:
            raise exceptions.SerializerMutationMetaAttrException(
                attribute="serializer_class")

        if not model_operation:
            raise exceptions.SerializerMutationMetaAttrException(
                attribute="model_operation")

        if model_class is None:
            serializer_meta = getattr(serializer_class, "Meta", None)
            if serializer_meta:
                model_class = getattr(serializer_meta, "model", None)

        if not model_class and model_operation in ["update", "delete"]:
            raise exceptions.SerializerMutationMetaAttrException(
                attribute="model_class")

        _meta.model_class = model_class
        _meta.serializer_class = serializer_class
        _meta.model_operation = model_operation
        _meta.login_required = login_required
        super().__init_subclass_with_meta__(_meta=_meta, **options)
Beispiel #4
0
    def __init_subclass_with_meta__(
        cls, description=None, permissions: Tuple = None, _meta=None, **options
    ):

        if not _meta:
            _meta = MutationOptions(cls)

        if not description:
            raise ImproperlyConfigured("No description provided in Meta")

        if isinstance(permissions, str):
            permissions = (permissions,)

        if permissions and not isinstance(permissions, tuple):
            raise ImproperlyConfigured(
                "Permissions should be a tuple or a string in Meta"
            )

        _meta.permissions = permissions
        super().__init_subclass_with_meta__(
            description=description, _meta=_meta, **options
        )
Beispiel #5
0
    def __init_subclass_with_meta__(
        cls, description=None, permissions: Tuple = None, _meta=None, **options
    ):

        if not _meta:
            _meta = MutationOptions(cls)

        if not description:
            raise ImproperlyConfigured("No description provided in Meta")

        if isinstance(permissions, str):
            permissions = (permissions,)

        if permissions and not isinstance(permissions, tuple):
            raise ImproperlyConfigured(
                "Permissions should be a tuple or a string in Meta"
            )

        _meta.permissions = permissions
        super().__init_subclass_with_meta__(
            description=description, _meta=_meta, **options
        )
    def __init_subclass_with_meta__(
        cls,
        interfaces: Iterable[Type[Interface]] = (),
        resolver: Callable = None,
        arguments: Dict[str, "Argument"] = None,
        _meta: Optional[ObjectTypeOptions] = None,
        **options: Any,
    ) -> None:
        if not _meta:
            _meta = MutationOptions(cls)

        fields = {}

        for interface in interfaces:
            assert issubclass(interface, Interface), (
                'All interfaces of {} must be a subclass of Interface. Received "{}".'
            ).format(cls.__name__, interface)
            fields.update(interface._meta.fields)

        fields = OrderedDict()
        for base in reversed(cls.__mro__):
            fields.update(yank_fields_from_attrs(base.__dict__, _as=Field))

        if not arguments:
            input_class = getattr(cls, "Arguments", None)

            if input_class:
                arguments = props(input_class)
            else:
                arguments = {}

        if not resolver:
            mutate = getattr(cls, "mutate", None)
            assert mutate, "All mutations must define a mutate method in it"
            resolver = get_unbound_function(mutate)

        if _meta.fields:
            _meta.fields.update(fields)
        else:
            _meta.fields = fields

        _meta.interfaces = interfaces
        _meta.resolver = resolver
        _meta.arguments = arguments

        super(SQLAlchemyMutation, cls).__init_subclass_with_meta__(_meta=_meta,
                                                                   **options)
Beispiel #7
0
    def __init_subclass_with_meta__(
        cls,
        auto_permission_message=True,
        description=None,
        permissions: Tuple = None,
        _meta=None,
        error_type_class=None,
        error_type_field=None,
        errors_mapping=None,
        **options,
    ):
        if not _meta:
            _meta = MutationOptions(cls)

        if not description:
            raise ImproperlyConfigured("No description provided in Meta")

        if not error_type_class:
            raise ImproperlyConfigured("No error_type_class provided in Meta.")

        cls._validate_permissions(permissions)

        _meta.auto_permission_message = auto_permission_message
        _meta.permissions = permissions
        _meta.error_type_class = error_type_class
        _meta.error_type_field = error_type_field
        _meta.errors_mapping = errors_mapping

        if permissions and auto_permission_message:
            permissions_msg = message_one_of_permissions_required(permissions)
            description = f"{description} {permissions_msg}"

        super().__init_subclass_with_meta__(
            description=description, _meta=_meta, **options
        )

        if error_type_field:
            deprecated_msg = f"{DEPRECATED_IN_3X_FIELD} Use `errors` field instead."
            cls._meta.fields.update(
                get_error_fields(
                    error_type_class,
                    error_type_field,
                    deprecated_msg,
                )
            )
        cls._meta.fields.update(get_error_fields(error_type_class, "errors"))
Beispiel #8
0
    def __init_subclass_with_meta__(
        cls,
        description=None,
        permissions: Tuple = None,
        _meta=None,
        error_type_class=None,
        error_type_field=None,
        errors_mapping=None,
        **options,
    ):
        if not _meta:
            _meta = MutationOptions(cls)

        if not description:
            raise ImproperlyConfigured("No description provided in Meta")

        if not error_type_class:
            raise ImproperlyConfigured("No error_type_class provided in Meta.")

        if isinstance(permissions, str):
            permissions = (permissions,)

        if permissions and not isinstance(permissions, tuple):
            raise ImproperlyConfigured(
                "Permissions should be a tuple or a string in Meta"
            )

        _meta.permissions = permissions
        _meta.error_type_class = error_type_class
        _meta.error_type_field = error_type_field
        _meta.errors_mapping = errors_mapping
        super().__init_subclass_with_meta__(
            description=description, _meta=_meta, **options
        )
        if error_type_field:
            deprecated_msg = (
                "Use errors field instead. This field will be removed in Saleor 4.0."
            )
            cls._meta.fields.update(
                get_error_fields(
                    error_type_class,
                    error_type_field,
                    deprecated_msg,
                )
            )
        cls._meta.fields.update(get_error_fields(error_type_class, "errors"))
Beispiel #9
0
    def __init_subclass_with_meta__(cls,
                                    lookup_field=None,
                                    lookup_input_kwarg=None,
                                    serializer_class=None,
                                    model_class=None,
                                    model_operations=["create", "update"],
                                    fields=(),
                                    exclude=(),
                                    return_field_name=None,
                                    return_field_type=None,
                                    **options):
        if not serializer_class:
            raise Exception("serializer_class is required for the Mutation")

        if "update" not in model_operations and "create" not in model_operations:
            raise Exception(
                'model_operations must contain "create" and/or "update"')

        serializer = serializer_class()
        if model_class is None:
            serializer_meta = getattr(serializer_class, "Meta", None)
            if serializer_meta:
                model_class = getattr(serializer_meta, "model", None)

        if lookup_field is None and model_class:
            lookup_field = model_class._meta.pk.name
        if lookup_input_kwarg is None:
            lookup_input_kwarg = lookup_field

        input_fields = fields_for_serializer(serializer,
                                             fields,
                                             exclude,
                                             is_input=True)

        if return_field_name is None:
            model_name = model_class.__name__
            return_field_name = model_name[:1].lower() + model_name[1:]

        if not return_field_type:
            registry = get_global_registry()
            return_field_type = registry.get_type_for_model(model_class)

        output_fields = OrderedDict()
        if return_field_name:
            output_fields[return_field_name] = graphene.Field(
                return_field_type)

        _meta = MutationOptions(cls)
        _meta.lookup_field = lookup_field
        _meta.lookup_input_kwarg = lookup_input_kwarg
        _meta.model_operations = model_operations
        _meta.serializer_class = serializer_class
        _meta.model_class = model_class
        _meta.fields = yank_fields_from_attrs(output_fields, _as=Field)
        _meta.return_field_name = return_field_name
        _meta.return_field_type = return_field_type

        input_fields = yank_fields_from_attrs(input_fields, _as=InputField)
        super().__init_subclass_with_meta__(_meta=_meta,
                                            input_fields=input_fields,
                                            **options)