2019-03-10 00:39:04 +03:00
|
|
|
import six
|
2016-09-18 02:29:00 +03:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2019-03-10 00:39:04 +03:00
|
|
|
from django.db.models import Model
|
2016-10-16 03:40:12 +03:00
|
|
|
from django.utils.functional import SimpleLazyObject
|
2017-07-25 08:27:26 +03:00
|
|
|
from graphene import Field
|
|
|
|
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
|
2018-07-20 02:51:33 +03:00
|
|
|
from .utils import DJANGO_FILTER_INSTALLED, get_model_fields, is_valid_django_model
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2019-03-10 00:39:04 +03:00
|
|
|
if six.PY3:
|
|
|
|
from typing import Type
|
|
|
|
|
|
|
|
|
2017-07-25 08:27:26 +03:00
|
|
|
def construct_fields(model, registry, only_fields, exclude_fields):
|
|
|
|
_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:
|
2017-07-25 08:27:26 +03:00
|
|
|
is_not_in_only = only_fields and name not in only_fields
|
|
|
|
# is_already_created = name in options.fields
|
2017-07-25 09:42:40 +03:00
|
|
|
is_excluded = 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
|
2017-07-25 08:27:26 +03:00
|
|
|
converted = convert_django_field_with_choices(field, registry)
|
2016-09-18 02:29:00 +03:00
|
|
|
fields[name] = converted
|
|
|
|
|
|
|
|
return fields
|
|
|
|
|
|
|
|
|
2017-07-25 08:27:26 +03:00
|
|
|
class DjangoObjectTypeOptions(ObjectTypeOptions):
|
|
|
|
model = None # type: Model
|
|
|
|
registry = None # type: Registry
|
|
|
|
connection = None # type: Type[Connection]
|
|
|
|
|
|
|
|
filter_fields = ()
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
only_fields=(),
|
|
|
|
exclude_fields=(),
|
|
|
|
filter_fields=None,
|
|
|
|
connection=None,
|
|
|
|
connection_class=None,
|
|
|
|
use_connection=None,
|
|
|
|
interfaces=(),
|
|
|
|
_meta=None,
|
|
|
|
**options
|
|
|
|
):
|
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), (
|
2018-07-20 02:51:33 +03:00
|
|
|
"The attribute registry in {} needs to be an instance of "
|
2017-07-25 08:27:26 +03:00
|
|
|
'Registry, received "{}".'
|
|
|
|
).format(cls.__name__, registry)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-07-25 08:27:26 +03:00
|
|
|
if not DJANGO_FILTER_INSTALLED and filter_fields:
|
|
|
|
raise Exception("Can only set filter_fields if Django-Filter is installed")
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2017-07-25 08:27:26 +03:00
|
|
|
django_fields = yank_fields_from_attrs(
|
2018-07-20 02:51:33 +03:00
|
|
|
construct_fields(model, registry, only_fields, exclude_fields), _as=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(
|
|
|
|
(issubclass(interface, Node) for interface in interfaces)
|
|
|
|
)
|
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(
|
2018-07-20 02:51:33 +03:00
|
|
|
"{}Connection".format(cls.__name__), node=cls
|
|
|
|
)
|
2017-07-25 08:27:26 +03:00
|
|
|
|
|
|
|
if connection is not None:
|
2017-07-25 09:42:40 +03:00
|
|
|
assert issubclass(connection, Connection), (
|
|
|
|
"The connection must be a Connection. Received {}"
|
|
|
|
).format(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
|
|
|
|
_meta.fields = django_fields
|
|
|
|
_meta.connection = connection
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2018-07-20 02:51:33 +03:00
|
|
|
super(DjangoObjectType, cls).__init_subclass_with_meta__(
|
|
|
|
_meta=_meta, interfaces=interfaces, **options
|
|
|
|
)
|
2017-07-25 09:42:40 +03:00
|
|
|
|
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-10-16 03:40:12 +03:00
|
|
|
if isinstance(root, SimpleLazyObject):
|
|
|
|
root._setup()
|
|
|
|
root = root._wrapped
|
2016-09-18 02:29:00 +03:00
|
|
|
if isinstance(root, cls):
|
|
|
|
return True
|
|
|
|
if not is_valid_django_model(type(root)):
|
2018-07-20 02:51:33 +03:00
|
|
|
raise Exception(('Received incompatible instance "{}".').format(root))
|
2018-01-15 06:36:52 +03:00
|
|
|
|
|
|
|
model = root._meta.model._meta.concrete_model
|
2016-09-18 02:29:00 +03:00
|
|
|
return model == cls._meta.model
|
|
|
|
|
|
|
|
@classmethod
|
2017-07-28 19:43:27 +03:00
|
|
|
def get_node(cls, info, id):
|
2016-09-18 02:29:00 +03:00
|
|
|
try:
|
2016-10-13 09:52:58 +03:00
|
|
|
return cls._meta.model.objects.get(pk=id)
|
2016-09-18 02:29:00 +03:00
|
|
|
except cls._meta.model.DoesNotExist:
|
|
|
|
return None
|