Esempio n. 1
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)
Esempio n. 2
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)