graphene-django/graphene_django/utils/utils.py

109 lines
2.9 KiB
Python
Raw Normal View History

import inspect
import six
from django.db import connection, models, transaction
from django.db.models.manager import Manager
from django.utils.encoding import force_text
from django.utils.functional import Promise
from graphene.utils.str_converters import to_camel_case
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
def isiterable(value):
try:
iter(value)
except TypeError:
return False
return True
def _camelize_django_str(s):
if isinstance(s, Promise):
s = force_text(s)
return to_camel_case(s) if isinstance(s, six.string_types) else s
def camelize(data):
if isinstance(data, dict):
return {_camelize_django_str(k): camelize(v) for k, v in data.items()}
if isiterable(data) and not isinstance(data, (six.string_types, Promise)):
return [camelize(d) for d in data]
return data
2017-05-25 19:15:13 +03:00
def get_reverse_fields(model, local_field_names):
for name, attr in model.__dict__.items():
2017-05-25 19:15:13 +03:00
# Don't duplicate any local fields
if name in local_field_names:
continue
2019-06-09 22:15:46 +03:00
# "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 maybe_queryset(value):
if isinstance(value, Manager):
value = value.get_queryset()
return value
def get_model_fields(model):
2017-05-25 19:15:13 +03:00
local_fields = [
(field.name, field)
2018-07-20 02:51:33 +03:00
for field in sorted(
list(model._meta.fields) + list(model._meta.local_many_to_many)
)
]
# Make sure we don't duplicate local fields with "reverse" version
2017-05-25 19:15:13 +03:00
local_field_names = [field[0] for field in local_fields]
reverse_fields = get_reverse_fields(model, local_field_names)
2017-05-25 19:15:13 +03:00
all_fields = local_fields + list(reverse_fields)
return all_fields
def is_valid_django_model(model):
return inspect.isclass(model) and issubclass(model, models.Model)
def import_single_dispatch():
try:
from functools import singledispatch
except ImportError:
singledispatch = None
if not singledispatch:
try:
from singledispatch import singledispatch
except ImportError:
pass
if not singledispatch:
raise Exception(
"It seems your python version does not include "
"functools.singledispatch. Please install the 'singledispatch' "
"package. More information here: "
"https://pypi.python.org/pypi/singledispatch"
)
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)