2016-09-18 02:29:00 +03:00
|
|
|
import inspect
|
2016-09-18 03:09:56 +03:00
|
|
|
|
2023-09-18 18:23:53 +03:00
|
|
|
import django
|
2020-12-31 02:37:57 +03:00
|
|
|
from django.db import connection, models, transaction
|
2016-09-18 02:29:00 +03:00
|
|
|
from django.db.models.manager import Manager
|
2020-04-06 15:21:07 +03:00
|
|
|
from django.utils.encoding import force_str
|
2019-06-25 11:40:29 +03:00
|
|
|
from django.utils.functional import Promise
|
2016-09-18 02:29:00 +03:00
|
|
|
|
2019-06-25 11:40:29 +03:00
|
|
|
from graphene.utils.str_converters import to_camel_case
|
2016-09-18 03:09:56 +03:00
|
|
|
|
2017-06-26 17:31:34 +03:00
|
|
|
try:
|
|
|
|
import django_filters # noqa
|
2018-07-20 02:51:33 +03:00
|
|
|
|
2017-06-26 17:31:34 +03:00
|
|
|
DJANGO_FILTER_INSTALLED = True
|
|
|
|
except ImportError:
|
|
|
|
DJANGO_FILTER_INSTALLED = False
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
2019-06-25 11:40:29 +03:00
|
|
|
def isiterable(value):
|
|
|
|
try:
|
|
|
|
iter(value)
|
|
|
|
except TypeError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def _camelize_django_str(s):
|
|
|
|
if isinstance(s, Promise):
|
2020-04-06 15:21:07 +03:00
|
|
|
s = force_str(s)
|
|
|
|
return to_camel_case(s) if isinstance(s, str) else s
|
2019-06-25 11:40:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
def camelize(data):
|
|
|
|
if isinstance(data, dict):
|
|
|
|
return {_camelize_django_str(k): camelize(v) for k, v in data.items()}
|
2020-04-06 15:21:07 +03:00
|
|
|
if isiterable(data) and not isinstance(data, (str, Promise)):
|
2019-06-25 11:40:29 +03:00
|
|
|
return [camelize(d) for d in data]
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2023-07-18 20:17:45 +03:00
|
|
|
def _get_model_ancestry(model):
|
|
|
|
model_ancestry = [model]
|
|
|
|
|
|
|
|
for base in model.__bases__:
|
|
|
|
if is_valid_django_model(base) and getattr(base, "_meta", False):
|
|
|
|
model_ancestry.append(base)
|
|
|
|
return model_ancestry
|
|
|
|
|
|
|
|
|
2017-05-25 19:15:13 +03:00
|
|
|
def get_reverse_fields(model, local_field_names):
|
2023-07-18 20:17:45 +03:00
|
|
|
"""
|
|
|
|
Searches through the model's ancestry and gets reverse relationships the models
|
|
|
|
Yields a tuple of (field.name, field)
|
|
|
|
"""
|
|
|
|
model_ancestry = _get_model_ancestry(model)
|
|
|
|
|
|
|
|
for _model in model_ancestry:
|
|
|
|
for name, attr in _model.__dict__.items():
|
|
|
|
# Don't duplicate any local fields
|
|
|
|
if name in local_field_names:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# "rel" for FK and M2M relations and "related" for O2O Relations
|
|
|
|
related = getattr(attr, "rel", None) or getattr(attr, "related", None)
|
|
|
|
if isinstance(related, models.ManyToOneRel):
|
|
|
|
yield (name, related)
|
|
|
|
elif isinstance(related, models.ManyToManyRel) and not related.symmetrical:
|
|
|
|
yield (name, related)
|
|
|
|
|
|
|
|
|
|
|
|
def get_local_fields(model):
|
|
|
|
"""
|
|
|
|
Searches through the model's ancestry and gets the fields on the models
|
|
|
|
Returns a dict of {field.name: field}
|
|
|
|
"""
|
|
|
|
model_ancestry = _get_model_ancestry(model)
|
|
|
|
|
|
|
|
local_fields_dict = {}
|
|
|
|
for _model in model_ancestry:
|
|
|
|
for field in sorted(
|
|
|
|
list(_model._meta.fields) + list(_model._meta.local_many_to_many)
|
|
|
|
):
|
|
|
|
if field.name not in local_fields_dict:
|
|
|
|
local_fields_dict[field.name] = field
|
2017-05-25 19:15:13 +03:00
|
|
|
|
2023-07-18 20:17:45 +03:00
|
|
|
return list(local_fields_dict.items())
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
def maybe_queryset(value):
|
|
|
|
if isinstance(value, Manager):
|
|
|
|
value = value.get_queryset()
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def get_model_fields(model):
|
2023-07-18 20:17:45 +03:00
|
|
|
"""
|
|
|
|
Gets all the fields and relationships on the Django model and its ancestry.
|
|
|
|
Prioritizes local fields and relationships over the reverse relationships of the same name
|
|
|
|
Returns a tuple of (field.name, field)
|
|
|
|
"""
|
|
|
|
local_fields = get_local_fields(model)
|
|
|
|
local_field_names = {field[0] for field in local_fields}
|
2017-05-25 19:15:13 +03:00
|
|
|
reverse_fields = get_reverse_fields(model, local_field_names)
|
|
|
|
all_fields = local_fields + list(reverse_fields)
|
2016-09-18 02:29:00 +03:00
|
|
|
|
|
|
|
return all_fields
|
|
|
|
|
|
|
|
|
|
|
|
def is_valid_django_model(model):
|
|
|
|
return inspect.isclass(model) and issubclass(model, models.Model)
|
2020-12-31 02:37:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
def import_single_dispatch():
|
2024-09-15 17:50:15 +03:00
|
|
|
from functools import singledispatch
|
2020-12-31 02:37:57 +03:00
|
|
|
|
|
|
|
return singledispatch
|
|
|
|
|
|
|
|
|
|
|
|
def set_rollback():
|
|
|
|
atomic_requests = connection.settings_dict.get("ATOMIC_REQUESTS", False)
|
|
|
|
if atomic_requests and connection.in_atomic_block:
|
|
|
|
transaction.set_rollback(True)
|
2023-07-18 15:16:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
def bypass_get_queryset(resolver):
|
|
|
|
"""
|
|
|
|
Adds a bypass_get_queryset attribute to the resolver, which is used to
|
|
|
|
bypass any custom get_queryset method of the DjangoObjectType.
|
|
|
|
"""
|
|
|
|
resolver._bypass_get_queryset = True
|
|
|
|
return resolver
|
2023-09-18 18:23:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
_DJANGO_VERSION_AT_LEAST_4_2 = django.VERSION[0] > 4 or (
|
|
|
|
django.VERSION[0] >= 4 and django.VERSION[1] >= 2
|
|
|
|
)
|