2019-07-09 16:03:11 +03:00
|
|
|
import warnings
|
2016-09-18 02:29:00 +03:00
|
|
|
from collections import OrderedDict
|
2023-08-06 01:47:00 +03:00
|
|
|
from typing import Type # noqa: F401
|
|
|
|
|
|
|
|
from django.db.models import Model # noqa: F401
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2019-03-31 14:01:43 +03:00
|
|
|
import graphene
|
2017-07-25 08:27:26 +03:00
|
|
|
from graphene.relay import Connection, Node
|
|
|
|
from graphene.types.objecttype import ObjectType, ObjectTypeOptions
|
|
|
|
from graphene.types.utils import yank_fields_from_attrs
|
2016-09-18 03:09:56 +03:00
|
|
|
|
|
|
|
from .converter import convert_django_field_with_choices
|
|
|
|
from .registry import Registry, get_global_registry
|
2019-06-25 11:40:29 +03:00
|
|
|
from .settings import graphene_settings
|
|
|
|
from .utils import (
|
|
|
|
DJANGO_FILTER_INSTALLED,
|
|
|
|
camelize,
|
|
|
|
get_model_fields,
|
|
|
|
is_valid_django_model,
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2019-07-09 16:03:11 +03:00
|
|
|
ALL_FIELDS = "__all__"
|
|
|
|
|
|
|
|
|
2019-06-17 20:48:29 +03:00
|
|
|
def construct_fields(
|
|
|
|
model, registry, only_fields, exclude_fields, convert_choices_to_enum
|
|
|
|
):
|
2017-07-25 08:27:26 +03:00
|
|
|
_model_fields = get_model_fields(model)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
fields = OrderedDict()
|
2016-12-13 16:23:08 +03:00
|
|
|
for name, field in _model_fields:
|
2020-05-09 14:28:19 +03:00
|
|
|
is_not_in_only = (
|
|
|
|
only_fields is not None
|
|
|
|
and only_fields != ALL_FIELDS
|
|
|
|
and name not in only_fields
|
|
|
|
)
|
2017-07-25 08:27:26 +03:00
|
|
|
# is_already_created = name in options.fields
|
2020-05-09 14:28:19 +03:00
|
|
|
is_excluded = (
|
|
|
|
exclude_fields is not None and name in exclude_fields
|
|
|
|
) # or is_already_created
|
2016-11-10 10:56:14 +03:00
|
|
|
# https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ForeignKey.related_query_name
|
2018-07-20 02:51:33 +03:00
|
|
|
is_no_backref = str(name).endswith("+")
|
2016-11-10 10:56:14 +03:00
|
|
|
if is_not_in_only or is_excluded or is_no_backref:
|
2016-09-18 02:29:00 +03:00
|
|
|
# We skip this field if we specify only_fields and is not
|
2016-11-10 10:56:14 +03:00
|
|
|
# in there. Or when we exclude this field in exclude_fields.
|
|
|
|
# Or when there is no back reference.
|
2016-09-18 02:29:00 +03:00
|
|
|
continue
|
2019-06-17 20:48:29 +03:00
|
|
|
|
|
|
|
_convert_choices_to_enum = convert_choices_to_enum
|
|
|
|
if not isinstance(_convert_choices_to_enum, bool):
|
|
|
|
# then `convert_choices_to_enum` is a list of field names to convert
|
|
|
|
if name in _convert_choices_to_enum:
|
|
|
|
_convert_choices_to_enum = True
|
|
|
|
else:
|
|
|
|
_convert_choices_to_enum = False
|
|
|
|
|
|
|
|
converted = convert_django_field_with_choices(
|
|
|
|
field, registry, convert_choices_to_enum=_convert_choices_to_enum
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
fields[name] = converted
|
|
|
|
|
|
|
|
return fields
|
|
|
|
|
|
|
|
|
2020-02-07 12:55:38 +03:00
|
|
|
def validate_fields(type_, model, fields, only_fields, exclude_fields):
|
|
|
|
# Validate the given fields against the model's fields and custom fields
|
|
|
|
all_field_names = set(fields.keys())
|
2020-05-09 14:28:19 +03:00
|
|
|
only_fields = only_fields if only_fields is not ALL_FIELDS else ()
|
2020-02-17 14:08:45 +03:00
|
|
|
for name in only_fields or ():
|
|
|
|
if name in all_field_names:
|
2020-02-07 12:55:38 +03:00
|
|
|
continue
|
|
|
|
|
2020-02-17 14:08:45 +03:00
|
|
|
if hasattr(model, name):
|
|
|
|
warnings.warn(
|
|
|
|
(
|
|
|
|
'Field name "{field_name}" matches an attribute on Django model "{app_label}.{object_name}" '
|
|
|
|
"but it's not a model field so Graphene cannot determine what type it should be. "
|
|
|
|
'Either define the type of the field on DjangoObjectType "{type_}" or remove it from the "fields" list.'
|
|
|
|
).format(
|
|
|
|
field_name=name,
|
|
|
|
app_label=model._meta.app_label,
|
|
|
|
object_name=model._meta.object_name,
|
|
|
|
type_=type_,
|
2020-02-07 12:55:38 +03:00
|
|
|
)
|
2020-02-17 14:08:45 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
warnings.warn(
|
|
|
|
(
|
|
|
|
'Field name "{field_name}" doesn\'t exist on Django model "{app_label}.{object_name}". '
|
|
|
|
'Consider removing the field from the "fields" list of DjangoObjectType "{type_}" because it has no effect.'
|
|
|
|
).format(
|
|
|
|
field_name=name,
|
|
|
|
app_label=model._meta.app_label,
|
|
|
|
object_name=model._meta.object_name,
|
|
|
|
type_=type_,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Validate exclude fields
|
|
|
|
for name in exclude_fields or ():
|
|
|
|
if name in all_field_names:
|
|
|
|
# Field is a custom field
|
|
|
|
warnings.warn(
|
|
|
|
(
|
2020-02-23 12:47:41 +03:00
|
|
|
'Excluding the custom field "{field_name}" on DjangoObjectType "{type_}" has no effect. '
|
2020-02-17 14:08:45 +03:00
|
|
|
'Either remove the custom field or remove the field from the "exclude" list.'
|
2021-03-18 12:26:02 +03:00
|
|
|
).format(field_name=name, type_=type_)
|
2020-02-17 14:08:45 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
if not hasattr(model, name):
|
2020-02-07 12:55:38 +03:00
|
|
|
warnings.warn(
|
|
|
|
(
|
2020-02-17 14:08:45 +03:00
|
|
|
'Django model "{app_label}.{object_name}" does not have a field or attribute named "{field_name}". '
|
|
|
|
'Consider removing the field from the "exclude" list of DjangoObjectType "{type_}" because it has no effect'
|
2020-02-07 12:55:38 +03:00
|
|
|
).format(
|
|
|
|
field_name=name,
|
|
|
|
app_label=model._meta.app_label,
|
|
|
|
object_name=model._meta.object_name,
|
|
|
|
type_=type_,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-07-25 08:27:26 +03:00
|
|
|
class DjangoObjectTypeOptions(ObjectTypeOptions):
|
2022-09-24 16:00:12 +03:00
|
|
|
model = None # type: Type[Model]
|
2017-07-25 08:27:26 +03:00
|
|
|
registry = None # type: Registry
|
|
|
|
connection = None # type: Type[Connection]
|
|
|
|
|
|
|
|
filter_fields = ()
|
2019-03-25 06:42:06 +03:00
|
|
|
filterset_class = None
|
2017-07-25 08:27:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
class DjangoObjectType(ObjectType):
|
|
|
|
@classmethod
|
2018-07-20 02:51:33 +03:00
|
|
|
def __init_subclass_with_meta__(
|
|
|
|
cls,
|
|
|
|
model=None,
|
|
|
|
registry=None,
|
|
|
|
skip_registry=False,
|
2020-05-09 14:28:19 +03:00
|
|
|
only_fields=None, # deprecated in favour of `fields`
|
|
|
|
fields=None,
|
|
|
|
exclude_fields=None, # deprecated in favour of `exclude`
|
|
|
|
exclude=None,
|
2018-07-20 02:51:33 +03:00
|
|
|
filter_fields=None,
|
2019-03-25 06:42:06 +03:00
|
|
|
filterset_class=None,
|
2018-07-20 02:51:33 +03:00
|
|
|
connection=None,
|
|
|
|
connection_class=None,
|
|
|
|
use_connection=None,
|
|
|
|
interfaces=(),
|
2019-06-17 20:48:29 +03:00
|
|
|
convert_choices_to_enum=True,
|
2018-07-20 02:51:33 +03:00
|
|
|
_meta=None,
|
2023-08-06 01:47:00 +03:00
|
|
|
**options,
|
2018-07-20 02:51:33 +03:00
|
|
|
):
|
2017-07-25 08:27:26 +03:00
|
|
|
assert is_valid_django_model(model), (
|
2016-09-18 02:29:00 +03:00
|
|
|
'You need to pass a valid Django Model in {}.Meta, received "{}".'
|
2017-07-25 08:27:26 +03:00
|
|
|
).format(cls.__name__, model)
|
|
|
|
|
|
|
|
if not registry:
|
|
|
|
registry = get_global_registry()
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-07-25 08:27:26 +03:00
|
|
|
assert isinstance(registry, Registry), (
|
2023-08-09 20:48:42 +03:00
|
|
|
f"The attribute registry in {cls.__name__} needs to be an instance of "
|
|
|
|
f'Registry, received "{registry}".'
|
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2019-03-25 06:42:06 +03:00
|
|
|
if filter_fields and filterset_class:
|
|
|
|
raise Exception("Can't set both filter_fields and filterset_class")
|
2019-03-25 17:03:54 +03:00
|
|
|
|
2019-03-25 06:42:06 +03:00
|
|
|
if not DJANGO_FILTER_INSTALLED and (filter_fields or filterset_class):
|
2019-06-11 06:54:30 +03:00
|
|
|
raise Exception(
|
2022-10-19 17:10:30 +03:00
|
|
|
"Can only set filter_fields or filterset_class if "
|
|
|
|
"Django-Filter is installed"
|
2019-06-11 06:54:30 +03:00
|
|
|
)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2019-07-09 16:03:11 +03:00
|
|
|
assert not (fields and exclude), (
|
|
|
|
"Cannot set both 'fields' and 'exclude' options on "
|
2023-08-09 20:48:42 +03:00
|
|
|
f"DjangoObjectType {cls.__name__}."
|
2019-07-09 16:03:11 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# Alias only_fields -> fields
|
|
|
|
if only_fields and fields:
|
|
|
|
raise Exception("Can't set both only_fields and fields")
|
|
|
|
if only_fields:
|
|
|
|
warnings.warn(
|
|
|
|
"Defining `only_fields` is deprecated in favour of `fields`.",
|
2020-06-10 19:30:24 +03:00
|
|
|
DeprecationWarning,
|
2019-07-09 16:03:11 +03:00
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
fields = only_fields
|
|
|
|
if fields and fields != ALL_FIELDS and not isinstance(fields, (list, tuple)):
|
|
|
|
raise TypeError(
|
|
|
|
'The `fields` option must be a list or tuple or "__all__". '
|
|
|
|
"Got %s." % type(fields).__name__
|
|
|
|
)
|
|
|
|
|
|
|
|
# Alias exclude_fields -> exclude
|
|
|
|
if exclude_fields and exclude:
|
|
|
|
raise Exception("Can't set both exclude_fields and exclude")
|
|
|
|
if exclude_fields:
|
|
|
|
warnings.warn(
|
|
|
|
"Defining `exclude_fields` is deprecated in favour of `exclude`.",
|
2020-06-10 19:30:24 +03:00
|
|
|
DeprecationWarning,
|
2019-07-09 16:03:11 +03:00
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
exclude = exclude_fields
|
|
|
|
if exclude and not isinstance(exclude, (list, tuple)):
|
|
|
|
raise TypeError(
|
|
|
|
"The `exclude` option must be a list or tuple. Got %s."
|
|
|
|
% type(exclude).__name__
|
|
|
|
)
|
|
|
|
|
2020-06-11 13:09:52 +03:00
|
|
|
if fields is None and exclude is None:
|
|
|
|
warnings.warn(
|
|
|
|
"Creating a DjangoObjectType without either the `fields` "
|
|
|
|
"or the `exclude` option is deprecated. Add an explicit `fields "
|
2023-08-09 20:48:42 +03:00
|
|
|
f"= '__all__'` option on DjangoObjectType {cls.__name__} to use all "
|
|
|
|
"fields",
|
2020-06-11 13:09:52 +03:00
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
|
2017-07-25 08:27:26 +03:00
|
|
|
django_fields = yank_fields_from_attrs(
|
2019-07-09 16:03:11 +03:00
|
|
|
construct_fields(model, registry, fields, exclude, convert_choices_to_enum),
|
2021-03-18 12:26:02 +03:00
|
|
|
_as=graphene.Field,
|
2016-09-18 02:29:00 +03:00
|
|
|
)
|
|
|
|
|
2017-07-25 08:27:26 +03:00
|
|
|
if use_connection is None and interfaces:
|
2018-07-20 02:51:33 +03:00
|
|
|
use_connection = any(
|
2022-10-19 17:10:30 +03:00
|
|
|
issubclass(interface, Node) for interface in interfaces
|
2018-07-20 02:51:33 +03:00
|
|
|
)
|
2017-07-25 08:27:26 +03:00
|
|
|
|
|
|
|
if use_connection and not connection:
|
|
|
|
# We create the connection automatically
|
2017-10-31 00:35:29 +03:00
|
|
|
if not connection_class:
|
|
|
|
connection_class = Connection
|
|
|
|
|
|
|
|
connection = connection_class.create_type(
|
2020-08-07 12:15:35 +03:00
|
|
|
"{}Connection".format(options.get("name") or cls.__name__), node=cls
|
2018-07-20 02:51:33 +03:00
|
|
|
)
|
2017-07-25 08:27:26 +03:00
|
|
|
|
|
|
|
if connection is not None:
|
2023-08-06 01:47:00 +03:00
|
|
|
assert issubclass(
|
|
|
|
connection, Connection
|
|
|
|
), f"The connection must be a Connection. Received {connection.__name__}"
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-01-21 22:33:52 +03:00
|
|
|
if not _meta:
|
|
|
|
_meta = DjangoObjectTypeOptions(cls)
|
|
|
|
|
2017-07-25 08:27:26 +03:00
|
|
|
_meta.model = model
|
|
|
|
_meta.registry = registry
|
|
|
|
_meta.filter_fields = filter_fields
|
2019-03-25 06:42:06 +03:00
|
|
|
_meta.filterset_class = filterset_class
|
2017-07-25 08:27:26 +03:00
|
|
|
_meta.fields = django_fields
|
|
|
|
_meta.connection = connection
|
2023-05-24 15:58:50 +03:00
|
|
|
_meta.convert_choices_to_enum = convert_choices_to_enum
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
super().__init_subclass_with_meta__(
|
2018-07-20 02:51:33 +03:00
|
|
|
_meta=_meta, interfaces=interfaces, **options
|
|
|
|
)
|
2017-07-25 09:42:40 +03:00
|
|
|
|
2020-02-07 12:55:38 +03:00
|
|
|
# Validate fields
|
|
|
|
validate_fields(cls, model, _meta.fields, fields, exclude)
|
|
|
|
|
2017-07-25 08:27:26 +03:00
|
|
|
if not skip_registry:
|
|
|
|
registry.register(cls)
|
2016-09-18 03:09:56 +03:00
|
|
|
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve_id(self, info):
|
2016-10-13 09:52:58 +03:00
|
|
|
return self.pk
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
@classmethod
|
2017-07-28 19:43:27 +03:00
|
|
|
def is_type_of(cls, root, info):
|
2016-09-18 02:29:00 +03:00
|
|
|
if isinstance(root, cls):
|
|
|
|
return True
|
2020-05-09 19:35:09 +03:00
|
|
|
if not is_valid_django_model(root.__class__):
|
2023-08-06 01:47:00 +03:00
|
|
|
raise Exception(f'Received incompatible instance "{root}".')
|
2018-01-15 06:36:52 +03:00
|
|
|
|
2019-03-27 07:09:25 +03:00
|
|
|
if cls._meta.model._meta.proxy:
|
|
|
|
model = root._meta.model
|
|
|
|
else:
|
|
|
|
model = root._meta.model._meta.concrete_model
|
2019-03-27 07:24:13 +03:00
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
return model == cls._meta.model
|
|
|
|
|
2019-03-31 14:01:17 +03:00
|
|
|
@classmethod
|
|
|
|
def get_queryset(cls, queryset, info):
|
|
|
|
return queryset
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
@classmethod
|
2017-07-28 19:43:27 +03:00
|
|
|
def get_node(cls, info, id):
|
2019-03-31 14:01:17 +03:00
|
|
|
queryset = cls.get_queryset(cls._meta.model.objects, info)
|
2016-09-18 02:29:00 +03:00
|
|
|
try:
|
2019-03-31 14:01:17 +03:00
|
|
|
return queryset.get(pk=id)
|
2016-09-18 02:29:00 +03:00
|
|
|
except cls._meta.model.DoesNotExist:
|
|
|
|
return None
|
2019-03-31 14:01:43 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ErrorType(ObjectType):
|
|
|
|
field = graphene.String(required=True)
|
|
|
|
messages = graphene.List(graphene.NonNull(graphene.String), required=True)
|
2019-06-25 11:40:29 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_errors(cls, errors):
|
2019-07-09 00:22:08 +03:00
|
|
|
data = camelize(errors) if graphene_settings.CAMELCASE_ERRORS else errors
|
|
|
|
return [cls(field=key, messages=value) for key, value in data.items()]
|