mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-24 10:34:10 +03:00
4517e32224
* 🔧 Add pre-commit config Similar to graphene and graphene-sqlalchemy * ⬆ Bump black * 👷 Lint on CI * ⬆ Bump flake8-black * 🔧 Keep excluding migrations * ⬆ Bump flake8 * 🔧 Remove black and flake8 from tox config * ⬆ Update pre-commit versions * Upgrade syntax to python 3.7+ * Format with pre-commit dedent docs/schema.py to allow formatting * Fix tests on python 3.7
44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
class Registry:
|
|
def __init__(self):
|
|
self._registry = {}
|
|
self._field_registry = {}
|
|
|
|
def register(self, cls):
|
|
from .types import DjangoObjectType
|
|
|
|
assert issubclass(
|
|
cls, DjangoObjectType
|
|
), 'Only DjangoObjectTypes can be registered, received "{}"'.format(
|
|
cls.__name__
|
|
)
|
|
assert cls._meta.registry == self, "Registry for a Model have to match."
|
|
# assert self.get_type_for_model(cls._meta.model) == cls, (
|
|
# 'Multiple DjangoObjectTypes registered for "{}"'.format(cls._meta.model)
|
|
# )
|
|
if not getattr(cls._meta, "skip_registry", False):
|
|
self._registry[cls._meta.model] = cls
|
|
|
|
def get_type_for_model(self, model):
|
|
return self._registry.get(model)
|
|
|
|
def register_converted_field(self, field, converted):
|
|
self._field_registry[field] = converted
|
|
|
|
def get_converted_field(self, field):
|
|
return self._field_registry.get(field)
|
|
|
|
|
|
registry = None
|
|
|
|
|
|
def get_global_registry():
|
|
global registry
|
|
if not registry:
|
|
registry = Registry()
|
|
return registry
|
|
|
|
|
|
def reset_global_registry():
|
|
global registry
|
|
registry = None
|